Search in sources :

Example 21 with MockHttpInputMessage

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

the class Jaxb2RootElementHttpMessageConverterTests 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" + "<rootElement><external>&lol9;</external></rootElement>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
    assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(RootElement.class, inputMessage)).withMessageContaining("DOCTYPE");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 22 with MockHttpInputMessage

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

the class Jaxb2RootElementHttpMessageConverterTests method customizeUnmarshaller.

@Test
public void customizeUnmarshaller() throws Exception {
    byte[] body = "<myRootElement><element>a|||b</element></myRootElement>".getBytes(StandardCharsets.UTF_8);
    MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    MyRootElement result = (MyRootElement) myConverter.read(MyRootElement.class, inputMessage);
    assertThat(result.getElement().getField1()).isEqualTo("a");
    assertThat(result.getElement().getField2()).isEqualTo("b");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 23 with MockHttpInputMessage

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

the class Jaxb2RootElementHttpMessageConverterTests method readXmlRootElementSubclass.

@Test
public void readXmlRootElementSubclass() throws Exception {
    byte[] body = "<rootElement><type s=\"Hello World\"/></rootElement>".getBytes(StandardCharsets.UTF_8);
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    RootElementSubclass result = (RootElementSubclass) converter.read(RootElementSubclass.class, inputMessage);
    assertThat(result.getType().s).as("Invalid result").isEqualTo("Hello World");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 24 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readWithNoDefaultConstructor.

@Test
public void readWithNoDefaultConstructor() throws Exception {
    String body = "{\"property1\":\"foo\",\"property2\":\"bar\"}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    assertThatExceptionOfType(HttpMessageConversionException.class).isThrownBy(() -> converter.read(BeanWithNoDefaultConstructor.class, inputMessage)).withMessageStartingWith("Type definition error:");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 25 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readUntyped.

@Test
@SuppressWarnings("unchecked")
public void readUntyped() throws IOException {
    String body = "{" + "\"bytes\":\"AQI=\"," + "\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42," + "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    HashMap<String, Object> result = (HashMap<String, Object>) converter.read(HashMap.class, inputMessage);
    assertThat(result.get("string")).isEqualTo("Foo");
    assertThat(result.get("number")).isEqualTo(42);
    assertThat((Double) result.get("fraction")).isCloseTo(42D, within(0D));
    List<String> array = new ArrayList<>();
    array.add("Foo");
    array.add("Bar");
    assertThat(result.get("array")).isEqualTo(array);
    assertThat(result.get("bool")).isEqualTo(Boolean.TRUE);
    assertThat(result.get("bytes")).isEqualTo("AQI=");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) 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