Example usage for org.springframework.boot.test.web.client TestRestTemplate getForEntity

List of usage examples for org.springframework.boot.test.web.client TestRestTemplate getForEntity

Introduction

In this page you can find the example usage for org.springframework.boot.test.web.client TestRestTemplate getForEntity.

Prototype

public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException 

Source Link

Document

Retrieve a representation by doing a GET on the URL .

Usage

From source file:edu.infsci2560.LoginHelper.java

private static HttpHeaders getHeaders(TestRestTemplate template, String route) {
    // todo : write getHeaders test
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<String> page = template.getForEntity(route, String.class);
    assertThat(page.getStatusCode()).isEqualTo(HttpStatus.OK);

    String cookie = page.getHeaders().getFirst("Set-Cookie");
    headers.set("Cookie", cookie);

    Pattern pattern = Pattern.compile("(?s).*name=\"_csrf\".*?value=\"([^\"]+).*");
    Matcher matcher = pattern.matcher(page.getBody());
    assertThat(matcher.matches()).as(page.getBody()).isTrue();
    headers.set("X-CSRF-TOKEN", matcher.group(1));
    return headers;
}