Search in sources :

Example 6 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) List(java.util.List) Test(org.junit.Test)

Example 7 with MockHttpInputMessage

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());
}
Also used : MediaType(org.springframework.http.MediaType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 8 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 9 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputStreamReader(java.io.InputStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.jupiter.api.Test)

Example 10 with MockHttpInputMessage

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");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)76 Test (org.junit.jupiter.api.Test)65 MediaType (org.springframework.http.MediaType)31 ArrayList (java.util.ArrayList)16 List (java.util.List)15 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)13 Test (org.junit.Test)11 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)8 ClassPathResource (org.springframework.core.io.ClassPathResource)8 Resource (org.springframework.core.io.Resource)7 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)7 Type (java.lang.reflect.Type)5 HashMap (java.util.HashMap)5 Message (com.google.protobuf.Message)4 InputStream (java.io.InputStream)4 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)4 StreamSource (javax.xml.transform.stream.StreamSource)4 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)4 XmlRootElement (jakarta.xml.bind.annotation.XmlRootElement)3 Charset (java.nio.charset.Charset)3