Search in sources :

Example 16 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests method filters.

@Test
public void filters() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    JacksonFilteredBean bean = new JacksonFilteredBean();
    bean.setProperty1("value");
    bean.setProperty2("value");
    MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
    FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2"));
    jacksonValue.setFilters(filters);
    this.converter.writeInternal(jacksonValue, null, outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertThat(result, containsString("\"property1\":\"value\""));
    assertThat(result, not(containsString("\"property2\":\"value\"")));
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) Test(org.junit.Test)

Example 17 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests 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 18 with MockHttpOutputMessage

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

the class MappingJackson2HttpMessageConverterTests method jsonp.

@Test
public void jsonp() throws Exception {
    MappingJacksonValue jacksonValue = new MappingJacksonValue("foo");
    jacksonValue.setSerializationView(MyJacksonView1.class);
    jacksonValue.setJsonpFunction("callback");
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    this.converter.writeInternal(jacksonValue, null, outputMessage);
    assertEquals("/**/callback(\"foo\");", outputMessage.getBodyAsString(StandardCharsets.UTF_8));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) Test(org.junit.Test)

Example 19 with MockHttpOutputMessage

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

the class AtomFeedHttpMessageConverterTests method write.

@Test
public void write() throws IOException, SAXException {
    Feed feed = new Feed("atom_1.0");
    feed.setTitle("title");
    Entry entry1 = new Entry();
    entry1.setId("id1");
    entry1.setTitle("title1");
    Entry entry2 = new Entry();
    entry2.setId("id2");
    entry2.setTitle("title2");
    List<Entry> entries = new ArrayList<>(2);
    entries.add(entry1);
    entries.add(entry2);
    feed.setEntries(entries);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(feed, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "atom+xml", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType());
    String expected = "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + "<title>title</title>" + "<entry><id>id1</id><title>title1</title></entry>" + "<entry><id>id2</id><title>title2</title></entry></feed>";
    NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
    assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo(expected).ignoreWhitespace().withNodeMatcher(nm));
}
Also used : Entry(com.rometools.rome.feed.atom.Entry) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) DefaultNodeMatcher(org.xmlunit.diff.DefaultNodeMatcher) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) Feed(com.rometools.rome.feed.atom.Feed) DefaultNodeMatcher(org.xmlunit.diff.DefaultNodeMatcher) NodeMatcher(org.xmlunit.diff.NodeMatcher) Test(org.junit.Test)

Example 20 with MockHttpOutputMessage

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

the class RssChannelHttpMessageConverterTests method write.

@Test
public void write() throws IOException, SAXException {
    Channel channel = new Channel("rss_2.0");
    channel.setTitle("title");
    channel.setLink("http://example.com");
    channel.setDescription("description");
    Item item1 = new Item();
    item1.setTitle("title1");
    Item item2 = new Item();
    item2.setTitle("title2");
    List<Item> items = new ArrayList<>(2);
    items.add(item1);
    items.add(item2);
    channel.setItems(items);
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(channel, null, outputMessage);
    assertEquals("Invalid content-type", new MediaType("application", "rss+xml", StandardCharsets.UTF_8), outputMessage.getHeaders().getContentType());
    String expected = "<rss version=\"2.0\">" + "<channel><title>title</title><link>http://example.com</link><description>description</description>" + "<item><title>title1</title></item>" + "<item><title>title2</title></item>" + "</channel></rss>";
    assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo(expected));
}
Also used : Item(com.rometools.rome.feed.rss.Item) Channel(com.rometools.rome.feed.rss.Channel) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) 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