Search in sources :

Example 6 with MockHttpOutputMessage

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

the class MappingJackson2XmlHttpMessageConverterTests 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</string>"));
    assertTrue(result.contains("<number>42</number>"));
    assertTrue(result.contains("<fraction>42.0</fraction>"));
    assertTrue(result.contains("<array><array>Foo</array><array>Bar</array></array>"));
    assertTrue(result.contains("<bool>true</bool>"));
    assertTrue(result.contains("<bytes>AQI=</bytes>"));
    assertEquals("Invalid content-type", new MediaType("application", "xml", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 7 with MockHttpOutputMessage

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

the class MarshallingHttpMessageConverterTests method write.

@Test
public void write() throws Exception {
    String body = "<root>Hello World</root>";
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    Marshaller marshaller = mock(Marshaller.class);
    willDoNothing().given(marshaller).marshal(eq(body), isA(Result.class));
    MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
    converter.write(body, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
}
Also used : Marshaller(org.springframework.oxm.Marshaller) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Result(javax.xml.transform.Result) Test(org.junit.Test)

Example 8 with MockHttpOutputMessage

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

the class SourceHttpMessageConverterTests method writeSAXSource.

@Test
public void writeSAXSource() throws Exception {
    String xml = "<root>Hello World</root>";
    SAXSource saxSource = new SAXSource(new InputSource(new StringReader(xml)));
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(saxSource, null, outputMessage);
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>"));
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 9 with MockHttpOutputMessage

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

the class SourceHttpMessageConverterTests method writeStreamSource.

@Test
public void writeStreamSource() throws Exception {
    String xml = "<root>Hello World</root>";
    StreamSource streamSource = new StreamSource(new StringReader(xml));
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(streamSource, null, outputMessage);
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>"));
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) StringReader(java.io.StringReader) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 10 with MockHttpOutputMessage

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

the class ResourceHttpMessageConverterTests method shouldWriteImageResource.

@Test
public void shouldWriteImageResource() throws IOException {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    Resource body = new ClassPathResource("logo.jpg", getClass());
    converter.write(body, null, outputMessage);
    assertEquals("Invalid content-type", MediaType.IMAGE_JPEG, outputMessage.getHeaders().getContentType());
    assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) 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