Search in sources :

Example 36 with MockHttpOutputMessage

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

the class AtomFeedHttpMessageConverterTests method writeOtherCharset.

@Test
public void writeOtherCharset() throws IOException, SAXException {
    Feed feed = new Feed("atom_1.0");
    feed.setTitle("title");
    String encoding = "ISO-8859-1";
    feed.setEncoding(encoding);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(feed, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "atom+xml", Charset.forName(encoding)), outputMessage.getHeaders().getContentType());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Feed(com.rometools.rome.feed.atom.Feed) Test(org.junit.Test)

Example 37 with MockHttpOutputMessage

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

the class RssChannelHttpMessageConverterTests method writeOtherCharset.

@Test
public void writeOtherCharset() throws IOException, SAXException {
    Channel channel = new Channel("rss_2.0");
    channel.setTitle("title");
    channel.setLink("http://example.com");
    channel.setDescription("description");
    String encoding = "ISO-8859-1";
    channel.setEncoding(encoding);
    Item item1 = new Item();
    item1.setTitle("title1");
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(channel, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "rss+xml", Charset.forName(encoding)), outputMessage.getHeaders().getContentType());
}
Also used : Item(com.rometools.rome.feed.rss.Item) Channel(com.rometools.rome.feed.rss.Channel) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 38 with MockHttpOutputMessage

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

the class Jaxb2RootElementHttpMessageConverterTests method customizeMarshaller.

// SPR-11488
@Test
public void customizeMarshaller() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
    myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
    DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>").withDifferenceEvaluator(ev));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) DifferenceEvaluator(org.xmlunit.diff.DifferenceEvaluator) Test(org.junit.Test)

Example 39 with MockHttpOutputMessage

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

the class Jaxb2RootElementHttpMessageConverterTests method writeXmlRootElementSubclass.

@Test
public void writeXmlRootElementSubclass() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(rootElementCglib, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType());
    DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
    assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) DifferenceEvaluator(org.xmlunit.diff.DifferenceEvaluator) Test(org.junit.Test)

Example 40 with MockHttpOutputMessage

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

the class MappingJackson2XmlHttpMessageConverterTests method jsonView.

@Test
public void jsonView() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    JacksonViewBean bean = new JacksonViewBean();
    bean.setWithView1("with");
    bean.setWithView2("with");
    bean.setWithoutView("without");
    MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
    jacksonValue.setSerializationView(MyJacksonView1.class);
    this.writeInternal(jacksonValue, outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertThat(result, containsString("<withView1>with</withView1>"));
    assertThat(result, not(containsString("<withView2>with</withView2>")));
    assertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) 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