Last active
November 18, 2016 19:38
-
-
Save geapi/e790cdbd9a7e6a5ea81aef82f3b81e43 to your computer and use it in GitHub Desktop.
example of using basic auth and csrf in RestTemplate call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tips.cloudnative.todotest; | |
// imports | |
import static tips.cloudnative.testauthentication.TestAuthentication.csrfHeaders; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | |
public class TodoTest { | |
@Test | |
public void testCreateTodo() { | |
RestTemplate restTemplate = new RestTemplate(); | |
HttpEntity<Todo> requestEntity = new HttpEntity<>(new Todo(null, "renew Washington Post subscription", false), | |
csrfHeaders()); | |
ResponseEntity<Todo> response = restTemplate.exchange( | |
"http://localhost:" + port + "/todos", | |
HttpMethod.POST, | |
requestEntity, | |
Todo.class); | |
Todo returnedTodo = response.getBody(); | |
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED); | |
assertThat(returnedTodo.getId()).isNotNull(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment