Search in sources :

Example 6 with HttpEntity

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

the class SampleTomcatApplicationTests method testCompression.

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
    ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, requestEntity, byte[].class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {
        assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with HttpEntity

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

the class SampleJettyApplicationTests method testCompression.

@Test
public void testCompression() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("Accept-Encoding", "gzip");
    HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
    ResponseEntity<byte[]> entity = this.restTemplate.exchange("/", HttpMethod.GET, requestEntity, byte[].class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
    GZIPInputStream inflater = new GZIPInputStream(new ByteArrayInputStream(entity.getBody()));
    try {
        assertThat(StreamUtils.copyToString(inflater, Charset.forName("UTF-8"))).isEqualTo("Hello World");
    } finally {
        inflater.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with HttpEntity

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

the class SampleActuatorApplicationTests method testHtmlErrorPage.

@Test
public void testHtmlErrorPage() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    HttpEntity<?> request = new HttpEntity<Void>(headers);
    ResponseEntity<String> entity = this.restTemplate.withBasicAuth("user", getPassword()).exchange("/foo", HttpMethod.GET, request, String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    String body = entity.getBody();
    assertThat(body).as("Body was null").isNotNull();
    assertThat(body).contains("This application has no explicit mapping for /error");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with HttpEntity

use of org.springframework.http.HttpEntity 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 10 with HttpEntity

use of org.springframework.http.HttpEntity 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)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)85 HttpHeaders (org.springframework.http.HttpHeaders)62 Test (org.junit.Test)46 URI (java.net.URI)15 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 ArrayList (java.util.ArrayList)11 MediaType (org.springframework.http.MediaType)11 RestTemplate (org.springframework.web.client.RestTemplate)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 List (java.util.List)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5