Search in sources :

Example 46 with MockHttpInputMessage

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

the class RssChannelHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    InputStream is = getClass().getResourceAsStream("rss.xml");
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(is);
    inputMessage.getHeaders().setContentType(new MediaType("application", "rss+xml", StandardCharsets.UTF_8));
    Channel result = converter.read(Channel.class, inputMessage);
    assertEquals("title", result.getTitle());
    assertEquals("http://example.com", result.getLink());
    assertEquals("description", result.getDescription());
    List<?> items = result.getItems();
    assertEquals(2, items.size());
    Item item1 = (Item) items.get(0);
    assertEquals("title1", item1.getTitle());
    Item item2 = (Item) items.get(1);
    assertEquals("title2", item2.getTitle());
}
Also used : Item(com.rometools.rome.feed.rss.Item) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputStream(java.io.InputStream) Channel(com.rometools.rome.feed.rss.Channel) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 47 with MockHttpInputMessage

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

the class MappingJackson2XmlHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    String body = "<MyBean><string>Foo</string><number>42</number><fraction>42.0</fraction><array><array>Foo</array><array>Bar</array></array><bool>true</bool><bytes>AQI=</bytes></MyBean>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
    MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
    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 : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 48 with MockHttpInputMessage

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

the class MappingJackson2XmlHttpMessageConverterTests method readWithXmlBomb.

@Test
public void readWithXmlBomb() throws IOException {
    // https://en.wikipedia.org/wiki/Billion_laughs
    // https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
    String body = "<?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" + "<MyBean>&lol9;</MyBean>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
    this.thrown.expect(HttpMessageNotReadableException.class);
    this.converter.read(MyBean.class, inputMessage);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.Test)

Example 49 with MockHttpInputMessage

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

the class GsonHttpMessageConverterTests method readGenerics.

@Test
@SuppressWarnings("unchecked")
public void readGenerics() throws IOException {
    GsonHttpMessageConverter converter = new GsonHttpMessageConverter() {

        @Override
        protected TypeToken<?> getTypeToken(Type type) {
            if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
                return new TypeToken<ArrayList<MyBean>>() {
                };
            } else {
                return super.getTypeToken(type);
            }
        }
    };
    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(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) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) TypeToken(com.google.gson.reflect.TypeToken) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 50 with MockHttpInputMessage

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

the class GsonHttpMessageConverterTests method readParameterizedType.

@Test
@SuppressWarnings("unchecked")
public void readParameterizedType() throws IOException {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
    };
    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"));
    GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, 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 : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)56 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)56 MediaType (org.springframework.http.MediaType)31 ClassPathResource (org.springframework.core.io.ClassPathResource)8 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)7 Resource (org.springframework.core.io.Resource)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 InputStream (java.io.InputStream)4 StreamSource (javax.xml.transform.stream.StreamSource)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 SAXSource (javax.xml.transform.sax.SAXSource)3 StAXSource (javax.xml.transform.stax.StAXSource)3 Unmarshaller (org.springframework.oxm.Unmarshaller)3 InputSource (org.xml.sax.InputSource)3 Message (com.google.protobuf.Message)2 InputStreamReader (java.io.InputStreamReader)2 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2