Search in sources :

Example 31 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method exchangeGetCallbackWithLambdas.

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

Example 32 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method exchangePostCallback.

@Test
public void exchangePostCallback() throws Exception {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyHeader", "MyValue");
    requestHeaders.setContentType(MediaType.TEXT_PLAIN);
    HttpEntity<String> requestEntity = new HttpEntity<>(helloWorld, requestHeaders);
    ListenableFuture<ResponseEntity<Void>> resultFuture = template.exchange(baseUrl + "/{method}", HttpMethod.POST, requestEntity, Void.class, "post");
    final URI expected = new URI(baseUrl + "/post/1");
    resultFuture.addCallback(new ListenableFutureCallback<ResponseEntity<Void>>() {

        @Override
        public void onSuccess(ResponseEntity<Void> result) {
            assertEquals("Invalid location", expected, result.getHeaders().getLocation());
            assertFalse(result.hasBody());
        }

        @Override
        public void onFailure(Throwable ex) {
            fail(ex.getMessage());
        }
    });
    waitTillDone(resultFuture);
}
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 33 with HttpEntity

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

the class RestTemplateIntegrationTests method jsonPostForObjectWithJacksonView.

@Test
public void jsonPostForObjectWithJacksonView() throws URISyntaxException {
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
    MySampleBean bean = new MySampleBean("with", "with", "without");
    MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
    jacksonValue.setSerializationView(MyJacksonView1.class);
    HttpEntity<MappingJacksonValue> entity = new HttpEntity<>(jacksonValue, entityHeaders);
    String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class);
    assertTrue(s.contains("\"with1\":\"with\""));
    assertFalse(s.contains("\"with2\":\"with\""));
    assertFalse(s.contains("\"without\":\"without\""));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) MediaType(org.springframework.http.MediaType) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) Test(org.junit.Test)

Example 34 with HttpEntity

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

the class RestTemplateIntegrationTests method exchangeGet.

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

Example 35 with HttpEntity

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

the class RestTemplateTests method postForLocationEntityCustomHeader.

@Test
public void postForLocationEntityCustomHeader() throws Exception {
    given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(request);
    String helloWorld = "Hello World";
    given(converter.canWrite(String.class, null)).willReturn(true);
    HttpHeaders requestHeaders = new HttpHeaders();
    given(request.getHeaders()).willReturn(requestHeaders);
    converter.write(helloWorld, null, request);
    given(request.execute()).willReturn(response);
    given(errorHandler.hasError(response)).willReturn(false);
    HttpHeaders responseHeaders = new HttpHeaders();
    URI expected = new URI("http://example.com/hotels");
    responseHeaders.setLocation(expected);
    given(response.getHeaders()).willReturn(responseHeaders);
    HttpStatus status = HttpStatus.OK;
    given(response.getStatusCode()).willReturn(status);
    given(response.getStatusText()).willReturn(status.getReasonPhrase());
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.set("MyHeader", "MyValue");
    HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders);
    URI result = template.postForLocation("http://example.com", entity);
    assertEquals("Invalid POST result", expected, result);
    assertEquals("No custom header set", "MyValue", requestHeaders.getFirst("MyHeader"));
    verify(response).close();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus) 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