Search in sources :

Example 41 with MockHttpOutputMessage

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

the class GsonHttpMessageConverterTests method prefixJsonCustom.

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

Example 42 with MockHttpOutputMessage

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

the class GsonHttpMessageConverterTests method writeUTF16.

@Test
public void writeUTF16() throws IOException {
    MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    String body = "Héllo Wörld";
    this.converter.write(body, contentType, outputMessage);
    assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(StandardCharsets.UTF_16BE));
    assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 43 with MockHttpOutputMessage

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

the class ProtobufHttpMessageConverterTests method getContentLength.

@Test
public void getContentLength() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
    this.converter.write(this.testMsg, contentType, outputMessage);
    assertEquals(-1, outputMessage.getHeaders().getContentLength());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 44 with MockHttpOutputMessage

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

the class ProtobufHttpMessageConverterTests method write.

@Test
public void write() throws IOException {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF;
    this.converter.write(this.testMsg, contentType, outputMessage);
    assertEquals(contentType, outputMessage.getHeaders().getContentType());
    assertTrue(outputMessage.getBodyAsBytes().length > 0);
    Message result = Msg.parseFrom(outputMessage.getBodyAsBytes());
    assertEquals(this.testMsg, result);
    String messageHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER);
    assertEquals("Msg", messageHeader);
    String schemaHeader = outputMessage.getHeaders().getFirst(ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER);
    assertEquals("sample.proto", schemaHeader);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Message(com.google.protobuf.Message) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 45 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)

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