Search in sources :

Example 71 with HttpEntity

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

the class SampleHateoasApplicationTests method producesJsonWhenXmlIsPreferred.

@Test
public void producesJsonWhenXmlIsPreferred() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT, "application/xml;q=0.9,application/json;q=0.8");
    HttpEntity<?> request = new HttpEntity<>(headers);
    ResponseEntity<String> response = this.restTemplate.exchange("/customers/1", HttpMethod.GET, request, String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(response.getHeaders().getContentType()).isEqualTo(MediaType.parseMediaType("application/json;charset=UTF-8"));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 72 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method exchangePost.

@Test
public void exchangePost() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyHeader", "MyValue");
    requestHeaders.setContentType(MediaType.TEXT_PLAIN);
    HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders);
    Future<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
    ResponseEntity<Void> result = resultFuture.get();
    assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation());
    assertFalse(result.hasBody());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) URI(java.net.URI) Test(org.junit.Test)

Example 73 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method exchangeGet.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void exchangeGet() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyHeader", "MyValue");
    HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
    Future<ResponseEntity<String>> responseFuture = template.exchange(baseUrl + "/{method}", HttpMethod.GET, requestEntity, String.class, "get");
    ResponseEntity<String> response = responseFuture.get();
    assertEquals("Invalid content", helloWorld, response.getBody());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) Test(org.junit.Test)

Example 74 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method postForLocationCallback.

@Test
public void postForLocationCallback() throws Exception {
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
    HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
    final URI expected = new URI(baseUrl + "/post/1");
    ListenableFuture<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post");
    locationFuture.addCallback(new ListenableFutureCallback<URI>() {

        @Override
        public void onSuccess(URI result) {
            assertEquals("Invalid location", expected, result);
        }

        @Override
        public void onFailure(Throwable ex) {
            fail(ex.getMessage());
        }
    });
    waitTillDone(locationFuture);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) MediaType(org.springframework.http.MediaType) URI(java.net.URI) Test(org.junit.Test)

Example 75 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method postForLocationCallbackWithLambdas.

@Test
public void postForLocationCallbackWithLambdas() throws Exception {
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1));
    HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
    final URI expected = new URI(baseUrl + "/post/1");
    ListenableFuture<URI> locationFuture = template.postForLocation(baseUrl + "/{method}", entity, "post");
    locationFuture.addCallback(result -> assertEquals("Invalid location", expected, result), ex -> fail(ex.getMessage()));
    waitTillDone(locationFuture);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) MediaType(org.springframework.http.MediaType) URI(java.net.URI) Test(org.junit.Test)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)104 HttpHeaders (org.springframework.http.HttpHeaders)81 Test (org.junit.Test)46 RestTemplate (org.springframework.web.client.RestTemplate)17 URI (java.net.URI)15 ArrayList (java.util.ArrayList)13 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Map (java.util.Map)11 MediaType (org.springframework.http.MediaType)11 HashMap (java.util.HashMap)10 List (java.util.List)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 ByteArrayInputStream (java.io.ByteArrayInputStream)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