Search in sources :

Example 31 with MockHttpInputMessage

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

the class MappingJackson2XmlHttpMessageConverterTests method readInvalidXml.

@Test
public void readInvalidXml() throws IOException {
    String body = "FooBar";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(MyBean.class, inputMessage));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) Test(org.junit.jupiter.api.Test)

Example 32 with MockHttpInputMessage

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

the class SourceHttpMessageConverterTests method readStAXSourceWithXmlBomb.

@Test
public void readStAXSourceWithXmlBomb() 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));
    StAXSource result = (StAXSource) this.converter.read(StAXSource.class, inputMessage);
    XMLStreamReader streamReader = result.getXMLStreamReader();
    assertThat(streamReader.hasNext()).isTrue();
    streamReader.next();
    streamReader.next();
    String s = streamReader.getLocalName();
    assertThat(s).isEqualTo("root");
    assertThatExceptionOfType(XMLStreamException.class).isThrownBy(streamReader::getElementText).withMessageContaining("\"lol9\"");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.jupiter.api.Test)

Example 33 with MockHttpInputMessage

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

the class SourceHttpMessageConverterTests method readStAXSource.

@Test
public void readStAXSource() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
    XMLStreamReader streamReader = result.getXMLStreamReader();
    assertThat(streamReader.hasNext()).isTrue();
    streamReader.nextTag();
    String s = streamReader.getLocalName();
    assertThat(s).isEqualTo("root");
    s = streamReader.getElementText();
    assertThat(s).isEqualTo("Hello World");
    streamReader.close();
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXSource(javax.xml.transform.stax.StAXSource) Test(org.junit.jupiter.api.Test)

Example 34 with MockHttpInputMessage

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

the class SourceHttpMessageConverterTests method readDOMSourceExternal.

@Test
public void readDOMSourceExternal() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    converter.setSupportDtd(true);
    DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
    Document document = (Document) result.getNode();
    assertThat(document.getDocumentElement().getLocalName()).as("Invalid result").isEqualTo("root");
    assertThat(document.getDocumentElement().getTextContent()).as("Invalid result").isNotEqualTo("Foo Bar");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) DOMSource(javax.xml.transform.dom.DOMSource) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

Example 35 with MockHttpInputMessage

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

the class JsonbHttpMessageConverterTests method writeParameterizedBaseType.

@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
    };
    ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<>() {
    };
    String body = "[{\"bytes\":[1,2],\"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"));
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
    assertThat(results.size()).isEqualTo(1);
    MyBean result = results.get(0);
    assertThat(result.getString()).isEqualTo("Foo");
    assertThat(result.getNumber()).isEqualTo(42);
    assertThat(result.getFraction()).isCloseTo(42F, within(0F));
    assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
    assertThat(result.isBool()).isTrue();
    assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) 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