use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementList.
@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementList() throws Exception {
String content = "<list><rootElement><type s=\"1\"/></rootElement><rootElement><type s=\"2\"/></rootElement></list>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
List<RootElement> result = (List<RootElement>) converter.read(rootElementListType, null, inputMessage);
assertEquals("Invalid result", 2, result.size());
assertEquals("Invalid result", "1", result.get(0).type.s);
assertEquals("Invalid result", "2", result.get(1).type.s);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method readGenerics.
@Test
@SuppressWarnings("unchecked")
public void readGenerics() throws IOException {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() {
@Override
protected JavaType getJavaType(Type type, Class<?> contextClass) {
if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
return new ObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, MyBean.class);
} else {
return super.getJavaType(type, contextClass);
}
}
};
String body = "[{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
List<MyBean> results = (List<MyBean>) converter.read(List.class, inputMessage);
assertEquals(1, results.size());
MyBean result = results.get(0);
assertEquals("Foo", result.getString());
assertEquals(42, result.getNumber());
assertEquals(42F, result.getFraction(), 0F);
assertArrayEquals(new String[] { "Foo", "Bar" }, result.getArray());
assertTrue(result.isBool());
assertArrayEquals(new byte[] { 0x1, 0x2 }, result.getBytes());
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class SourceHttpMessageConverterTests method readSource.
@Test
public void readSource() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
converter.read(Source.class, inputMessage);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class SourceHttpMessageConverterTests method readStreamSource.
@Test
public void readStreamSource() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage);
String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream()));
assertThat(XmlContent.of(s)).isSimilarTo(BODY);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class SourceHttpMessageConverterTests method readSAXSourceWithXmlBomb.
@Test
public void readSAXSourceWithXmlBomb() throws Exception {
// https://en.wikipedia.org/wiki/Billion_laughs
// https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
String content = "<?xml version=\"1.0\"?>\n" + "<!DOCTYPE lolz [\n" + " <!ENTITY lol \"lol\">\n" + " <!ELEMENT lolz (#PCDATA)>\n" + " <!ENTITY lol1 \"&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;\">\n" + " <!ENTITY lol2 \"&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;\">\n" + " <!ENTITY lol3 \"&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;\">\n" + " <!ENTITY lol4 \"&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;\">\n" + " <!ENTITY lol5 \"&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;\">\n" + " <!ENTITY lol6 \"&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;\">\n" + " <!ENTITY lol7 \"&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;\">\n" + " <!ENTITY lol8 \"&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;\">\n" + " <!ENTITY lol9 \"&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;\">\n" + "]>\n" + "<root>&lol9;</root>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
SAXSource result = (SAXSource) this.converter.read(SAXSource.class, inputMessage);
InputSource inputSource = result.getInputSource();
XMLReader reader = result.getXMLReader();
assertThatExceptionOfType(SAXException.class).isThrownBy(() -> reader.parse(inputSource)).withMessageContaining("DOCTYPE");
}
Aggregations