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\"")));
}
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));
}
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));
}
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));
}
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));
}
Aggregations