Search in sources :

Example 31 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests 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)

Example 32 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests method prettyPrintWithSse.

@Test
public void prettyPrintWithSse() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    outputMessage.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM);
    PrettyPrintBean bean = new PrettyPrintBean();
    bean.setName("Jason");
    this.converter.setPrettyPrint(true);
    this.converter.writeInternal(bean, null, outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertEquals("{\ndata:  \"name\" : \"Jason\"\ndata:}", result);
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Example 33 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests 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);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    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\":\"AQI=\""));
    assertEquals("Invalid content-type", new MediaType("application", "json", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 34 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests method prettyPrint.

@Test
public void prettyPrint() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    PrettyPrintBean bean = new PrettyPrintBean();
    bean.setName("Jason");
    this.converter.setPrettyPrint(true);
    this.converter.writeInternal(bean, null, outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertEquals("{" + NEWLINE_SYSTEM_PROPERTY + "  \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}", result);
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Example 35 with MockHttpOutputMessage

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

the class StringHttpMessageConverterTests method setUp.

@Before
public void setUp() {
    this.converter = new StringHttpMessageConverter();
    this.outputMessage = new MockHttpOutputMessage();
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Before(org.junit.Before)

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