Search in sources :

Example 21 with MockHttpOutputMessage

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

the class BufferedImageHttpMessageConverterTests method writeDefaultContentType.

@Test
public void writeDefaultContentType() throws IOException {
    Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
    MediaType contentType = new MediaType("image", "png");
    converter.setDefaultContentType(contentType);
    BufferedImage body = ImageIO.read(logo.getFile());
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(body, new MediaType("*", "*"), outputMessage);
    assertEquals("Invalid content type", contentType, outputMessage.getWrittenHeaders().getContentType());
    assertTrue("Invalid size", outputMessage.getBodyAsBytes().length > 0);
    BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
    assertEquals("Invalid height", 500, result.getHeight());
    assertEquals("Invalid width", 750, result.getWidth());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 22 with MockHttpOutputMessage

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

the class BufferedImageHttpMessageConverterTests method write.

@Test
public void write() throws IOException {
    Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
    BufferedImage body = ImageIO.read(logo.getFile());
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    MediaType contentType = new MediaType("image", "png");
    converter.write(body, contentType, outputMessage);
    assertEquals("Invalid content type", contentType, outputMessage.getWrittenHeaders().getContentType());
    assertTrue("Invalid size", outputMessage.getBodyAsBytes().length > 0);
    BufferedImage result = ImageIO.read(new ByteArrayInputStream(outputMessage.getBodyAsBytes()));
    assertEquals("Invalid height", 500, result.getHeight());
    assertEquals("Invalid width", 750, result.getWidth());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 23 with MockHttpOutputMessage

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

the class ByteArrayHttpMessageConverterTests method write.

@Test
public void write() throws IOException {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    byte[] body = new byte[] { 0x1, 0x2 };
    converter.write(body, null, outputMessage);
    assertArrayEquals("Invalid result", body, outputMessage.getBodyAsBytes());
    assertEquals("Invalid content-type", new MediaType("application", "octet-stream"), outputMessage.getHeaders().getContentType());
    assertEquals("Invalid content-length", 2, outputMessage.getHeaders().getContentLength());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 24 with MockHttpOutputMessage

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

the class FormHttpMessageConverterTests method writeMultipartOrder.

// SPR-13309
@Test
public void writeMultipartOrder() throws Exception {
    MyBean myBean = new MyBean();
    myBean.setString("foo");
    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
    parts.add("part1", myBean);
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.setContentType(MediaType.TEXT_XML);
    HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders);
    parts.add("part2", 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(2, items.size());
    FileItem item = items.get(0);
    assertTrue(item.isFormField());
    assertEquals("part1", item.getFieldName());
    assertEquals("{\"string\":\"foo\"}", item.getString());
    item = items.get(1);
    assertTrue(item.isFormField());
    assertEquals("part2", item.getFieldName());
    // With developer builds we get: <MyBean><string>foo</string></MyBean>
    // But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
    // So... we make a compromise:
    assertThat(item.getString(), allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) MediaType(org.springframework.http.MediaType) RequestContext(org.apache.commons.fileupload.RequestContext) FileUpload(org.apache.commons.fileupload.FileUpload) Test(org.junit.Test)

Example 25 with MockHttpOutputMessage

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

the class FormHttpMessageConverterTests method writeForm.

@Test
public void writeForm() throws IOException {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.write(body, MediaType.APPLICATION_FORM_URLENCODED, outputMessage);
    assertEquals("Invalid result", "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
    assertEquals("Invalid content-type", new MediaType("application", "x-www-form-urlencoded"), outputMessage.getHeaders().getContentType());
    assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length, outputMessage.getHeaders().getContentLength());
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) 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