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());
}
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());
}
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());
}
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>")));
}
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());
}
Aggregations