Search in sources :

Example 41 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.

the class SampleWebMustacheApplicationTests method testMustacheErrorTemplate.

@Test
public void testMustacheErrorTemplate() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    HttpEntity<String> requestEntity = new HttpEntity<>(headers);
    ResponseEntity<String> responseEntity = this.restTemplate.exchange("/does-not-exist", HttpMethod.GET, requestEntity, String.class);
    assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertThat(responseEntity.getBody()).contains("Something went wrong: 404 Not Found");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 42 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.

the class SampleWebMustacheApplicationTests method test5xxHtmlResource.

@Test
public void test5xxHtmlResource() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    HttpEntity<String> requestEntity = new HttpEntity<>(headers);
    ResponseEntity<String> entity = this.restTemplate.exchange("/bang", HttpMethod.GET, requestEntity, String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    assertThat(entity.getBody()).contains("I'm a 5xx");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 43 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.

the class SampleWebSecureJdbcApplicationTests method getHeaders.

private HttpHeaders getHeaders() {
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<String> page = this.restTemplate.getForEntity("/login", 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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 44 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.

the class SampleSecureApplicationTests method getHeaders.

private HttpHeaders getHeaders() {
    HttpHeaders headers = new HttpHeaders();
    ResponseEntity<String> page = this.restTemplate.getForEntity("/login", 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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 45 with HttpHeaders

use of org.springframework.http.HttpHeaders in project spring-boot by spring-projects.

the class SampleSecureApplicationTests method testLoginPage.

@Test
public void testLoginPage() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    ResponseEntity<String> entity = this.restTemplate.exchange("/login", HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(entity.getBody()).contains("_csrf");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)484 Test (org.junit.Test)209 ResponseEntity (org.springframework.http.ResponseEntity)90 HttpEntity (org.springframework.http.HttpEntity)81 URI (java.net.URI)53 MediaType (org.springframework.http.MediaType)50 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)42 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)36 ByteArrayInputStream (java.io.ByteArrayInputStream)34 HttpStatus (org.springframework.http.HttpStatus)30 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)30 Map (java.util.Map)25 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)22 List (java.util.List)20 RestTemplate (org.springframework.web.client.RestTemplate)20 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)19 MultiValueMap (org.springframework.util.MultiValueMap)18 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)18 HttpInputMessage (org.springframework.http.HttpInputMessage)17