Search in sources :

Example 1 with MockHttpOutputMessage

use of org.springframework.http.MockHttpOutputMessage 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 2 with MockHttpOutputMessage

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

the class MappingJackson2SmileHttpMessageConverterTests method write.

@Test
public void write() throws IOException {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    MyBean body = new MyBean();
    body.setString("Foo");
    body.setNumber(42);
    body.setFraction(42F);
    body.setArray(new String[] { "Foo", "Bar" });
    body.setBool(true);
    body.setBytes(new byte[] { 0x1, 0x2 });
    converter.write(body, null, outputMessage);
    assertArrayEquals(mapper.writeValueAsBytes(body), outputMessage.getBodyAsBytes());
    assertEquals("Invalid content-type", new MediaType("application", "x-jackson-smile", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 3 with MockHttpOutputMessage

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

the class Jaxb2RootElementHttpMessageConverterTests method writeXmlRootElement.

@Test
public void writeXmlRootElement() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(rootElement, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
    DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) DifferenceEvaluator(org.xmlunit.diff.DifferenceEvaluator) Test(org.junit.Test)

Example 4 with MockHttpOutputMessage

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

the class GsonHttpMessageConverterTests method write.

@Test
public void write() throws IOException {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    MyBean body = new MyBean();
    body.setString("Foo");
    body.setNumber(42);
    body.setFraction(42F);
    body.setArray(new String[] { "Foo", "Bar" });
    body.setBool(true);
    body.setBytes(new byte[] { 0x1, 0x2 });
    this.converter.write(body, null, outputMessage);
    Charset utf8 = StandardCharsets.UTF_8;
    String result = outputMessage.getBodyAsString(utf8);
    assertTrue(result.contains("\"string\":\"Foo\""));
    assertTrue(result.contains("\"number\":42"));
    assertTrue(result.contains("fraction\":42.0"));
    assertTrue(result.contains("\"array\":[\"Foo\",\"Bar\"]"));
    assertTrue(result.contains("\"bool\":true"));
    assertTrue(result.contains("\"bytes\":[1,2]"));
    assertEquals("Invalid content-type", new MediaType("application", "json", utf8), outputMessage.getHeaders().getContentType());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 5 with MockHttpOutputMessage

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

the class GsonHttpMessageConverterTests method prefixJson.

@Test
public void prefixJson() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.setPrefixJson(true);
    this.converter.writeInternal("foo", null, outputMessage);
    assertEquals(")]}', \"foo\"", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Aggregations

MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)49 Test (org.junit.Test)48 MediaType (org.springframework.http.MediaType)24 ClassPathResource (org.springframework.core.io.ClassPathResource)12 Resource (org.springframework.core.io.Resource)11 ByteArrayResource (org.springframework.core.io.ByteArrayResource)5 InputStreamResource (org.springframework.core.io.InputStreamResource)5 HttpHeaders (org.springframework.http.HttpHeaders)5 ArrayList (java.util.ArrayList)4 ResourceRegion (org.springframework.core.io.support.ResourceRegion)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 StringReader (java.io.StringReader)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 DifferenceEvaluator (org.xmlunit.diff.DifferenceEvaluator)3 Feed (com.rometools.rome.feed.atom.Feed)2 Channel (com.rometools.rome.feed.rss.Channel)2 Item (com.rometools.rome.feed.rss.Item)2 BufferedImage (java.awt.image.BufferedImage)2 InputStream (java.io.InputStream)2 Result (javax.xml.transform.Result)2