Skip to content

Instantly share code, notes, and snippets.

@geapi
Last active November 18, 2016 19:38
Show Gist options
  • Save geapi/e790cdbd9a7e6a5ea81aef82f3b81e43 to your computer and use it in GitHub Desktop.
Save geapi/e790cdbd9a7e6a5ea81aef82f3b81e43 to your computer and use it in GitHub Desktop.
example of using basic auth and csrf in RestTemplate call
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