Search in sources :

Example 11 with HttpEntity

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

the class FormHttpMessageConverterTests method writeMultipart.

@Test
public void writeMultipart() throws Exception {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    parts.add("name 3", null);
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);
    // SPR-12108
    Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") {

        @Override
        public String getFilename() {
            return "Hallöle.jpg";
        }
    };
    parts.add("utf8", utf8);
    Source xml = new StreamSource(new StringReader("<root><child/></root>"));
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(MediaType.TEXT_XML);
    HttpEntity<Source> entity = new HttpEntity<>(xml, entityHeaders);
    parts.add("xml", entity);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.setMultipartCharset(StandardCharsets.UTF_8);
    this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage);
    final MediaType contentType = outputMessage.getHeaders().getContentType();
    assertNotNull("No boundary found", contentType.getParameter("boundary"));
    // see if Commons FileUpload can read what we wrote
    FileItemFactory fileItemFactory = new DiskFileItemFactory();
    FileUpload fileUpload = new FileUpload(fileItemFactory);
    RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage);
    List<FileItem> items = fileUpload.parseRequest(requestContext);
    assertEquals(6, items.size());
    FileItem item = items.get(0);
    assertTrue(item.isFormField());
    assertEquals("name 1", item.getFieldName());
    assertEquals("value 1", item.getString());
    item = items.get(1);
    assertTrue(item.isFormField());
    assertEquals("name 2", item.getFieldName());
    assertEquals("value 2+1", item.getString());
    item = items.get(2);
    assertTrue(item.isFormField());
    assertEquals("name 2", item.getFieldName());
    assertEquals("value 2+2", item.getString());
    item = items.get(3);
    assertFalse(item.isFormField());
    assertEquals("logo", item.getFieldName());
    assertEquals("logo.jpg", item.getName());
    assertEquals("image/jpeg", item.getContentType());
    assertEquals(logo.getFile().length(), item.getSize());
    item = items.get(4);
    assertFalse(item.isFormField());
    assertEquals("utf8", item.getFieldName());
    assertEquals("Hallöle.jpg", item.getName());
    assertEquals("image/jpeg", item.getContentType());
    assertEquals(logo.getFile().length(), item.getSize());
    item = items.get(5);
    assertEquals("xml", item.getFieldName());
    assertEquals("text/xml", item.getContentType());
    verify(outputMessage.getBody(), never()).close();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) StreamSource(javax.xml.transform.stream.StreamSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) RequestContext(org.apache.commons.fileupload.RequestContext) FileUpload(org.apache.commons.fileupload.FileUpload) Test(org.junit.Test)

Example 12 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method multipart.

@Test
public void multipart() throws Exception {
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("name 1", "value 1");
    parts.add("name 2", "value 2+1");
    parts.add("name 2", "value 2+2");
    Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
    parts.add("logo", logo);
    HttpEntity<MultiValueMap<String, Object>> requestBody = new HttpEntity<>(parts);
    Future<URI> future = template.postForLocation(baseUrl + "/multipart", requestBody);
    future.get();
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) URI(java.net.URI) ClassPathResource(org.springframework.core.io.ClassPathResource) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 13 with HttpEntity

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

the class AsyncRestTemplateIntegrationTests method exchangePostCallbackWithLambdas.

@Test
public void exchangePostCallbackWithLambdas() 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(result -> {
        assertEquals("Invalid location", expected, result.getHeaders().getLocation());
        assertFalse(result.hasBody());
    }, 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 14 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 15 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)

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