Search in sources :

Example 1 with MockHttpInputMessage

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

the class Jaxb2CollectionHttpMessageConverterTests method readXmlTypeList.

@Test
@SuppressWarnings("unchecked")
public void readXmlTypeList() throws Exception {
    String content = "<list><foo s=\"1\"/><bar s=\"2\"/></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    List<TestType> result = (List<TestType>) converter.read(typeListType, null, inputMessage);
    assertEquals("Invalid result", 2, result.size());
    assertEquals("Invalid result", "1", result.get(0).s);
    assertEquals("Invalid result", "2", result.get(1).s);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) List(java.util.List) Test(org.junit.Test)

Example 2 with MockHttpInputMessage

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

the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementExternalEntityDisabled.

@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementExternalEntityDisabled() throws Exception {
    Resource external = new ClassPathResource("external.txt", getClass());
    String content = "<!DOCTYPE root [" + "  <!ELEMENT external ANY >\n" + "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + "  <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    converter = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() {

        @Override
        protected XMLInputFactory createXmlInputFactory() {
            XMLInputFactory inputFactory = super.createXmlInputFactory();
            inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true);
            return inputFactory;
        }
    };
    try {
        Collection<RootElement> result = converter.read(rootElementListType, null, inputMessage);
        assertEquals(1, result.size());
        assertEquals("", result.iterator().next().external);
    } catch (HttpMessageNotReadableException ex) {
    // Some parsers raise an exception
    }
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Collection(java.util.Collection) ClassPathResource(org.springframework.core.io.ClassPathResource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 3 with MockHttpInputMessage

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

the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementExternalEntityEnabled.

@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementExternalEntityEnabled() throws Exception {
    Resource external = new ClassPathResource("external.txt", getClass());
    String content = "<!DOCTYPE root [" + "  <!ELEMENT external ANY >\n" + "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + "  <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    Jaxb2CollectionHttpMessageConverter<?> c = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() {

        @Override
        protected XMLInputFactory createXmlInputFactory() {
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
            return inputFactory;
        }
    };
    Collection<RootElement> result = c.read(rootElementListType, null, inputMessage);
    assertEquals(1, result.size());
    assertEquals("Foo Bar", result.iterator().next().external);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 4 with MockHttpInputMessage

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

the class Jaxb2CollectionHttpMessageConverterTests method testXmlBomb.

@Test
public void testXmlBomb() 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" + "<list><rootElement><external>&lol9;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    this.thrown.expect(HttpMessageNotReadableException.class);
    this.thrown.expectMessage("\"lol9\"");
    this.converter.read(this.rootElementListType, null, inputMessage);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Test(org.junit.Test)

Example 5 with MockHttpInputMessage

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

the class Jaxb2CollectionHttpMessageConverterTests method readXmlTypeSet.

@Test
@SuppressWarnings("unchecked")
public void readXmlTypeSet() throws Exception {
    String content = "<set><foo s=\"1\"/><bar s=\"2\"/></set>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    Set<TestType> result = (Set<TestType>) converter.read(typeSetType, null, inputMessage);
    assertEquals("Invalid result", 2, result.size());
    assertTrue("Invalid result", result.contains(new TestType("1")));
    assertTrue("Invalid result", result.contains(new TestType("2")));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Set(java.util.Set) Test(org.junit.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