Search in sources :

Example 21 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.

the class MappingJackson2XmlHttpMessageConverterTests method readValidXmlWithUnknownProperty.

@Test
public void readValidXmlWithUnknownProperty() throws IOException {
    String body = "<MyBean><string>string</string><unknownProperty>value</unknownProperty></MyBean>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    converter.read(MyBean.class, inputMessage);
// Assert no HttpMessageNotReadableException is thrown
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 22 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.

the class MappingJackson2XmlHttpMessageConverterTests method readNonUnicode.

@Test
@SuppressWarnings("unchecked")
public void readNonUnicode() throws Exception {
    String body = "<MyBean>" + "<string>føø bår</string>" + "</MyBean>";
    Charset charset = StandardCharsets.ISO_8859_1;
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
    inputMessage.getHeaders().setContentType(new MediaType("application", "xml", charset));
    MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
    assertThat(result.getString()).isEqualTo("føø bår");
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Charset(java.nio.charset.Charset) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 23 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.

the class MappingJackson2XmlHttpMessageConverterTests method readWithExternalReference.

@Test
public void readWithExternalReference() throws IOException {
    String body = "<!DOCTYPE MyBean SYSTEM \"https://192.168.28.42/1.jsp\" [" + "  <!ELEMENT root ANY >\n" + "  <!ENTITY ext SYSTEM \"" + new ClassPathResource("external.txt", getClass()).getURI() + "\" >]><MyBean><string>&ext;</string></MyBean>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage));
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) ClassPathResource(cn.taketoday.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 24 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.

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(cn.taketoday.http.MockHttpInputMessage) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) Test(org.junit.jupiter.api.Test)

Example 25 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.

the class GsonHttpMessageConverterTests method readUntyped.

@Test
@SuppressWarnings("unchecked")
public void readUntyped() throws IOException {
    String body = "{\"bytes\":[1,2],\"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"));
    HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);
    assertThat(result.get("string")).isEqualTo("Foo");
    Number n = (Number) result.get("number");
    assertThat(n.longValue()).isEqualTo(42);
    n = (Number) result.get("fraction");
    assertThat(n.doubleValue()).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);
    byte[] bytes = new byte[2];
    List<Number> resultBytes = (ArrayList<Number>) result.get("bytes");
    for (int i = 0; i < 2; i++) {
        bytes[i] = resultBytes.get(i).byteValue();
    }
    assertThat(bytes).isEqualTo(new byte[] { 0x1, 0x2 });
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpInputMessage (cn.taketoday.http.MockHttpInputMessage)82 Test (org.junit.jupiter.api.Test)82 MediaType (cn.taketoday.http.MediaType)56 MockHttpOutputMessage (cn.taketoday.http.MockHttpOutputMessage)26 ArrayList (java.util.ArrayList)24 List (java.util.List)18 TypeReference (cn.taketoday.core.TypeReference)12 HashMap (java.util.HashMap)10 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)9 Message (com.google.protobuf.Message)8 InputStream (java.io.InputStream)8 ClassPathResource (cn.taketoday.core.io.ClassPathResource)7 InputStreamResource (cn.taketoday.core.io.InputStreamResource)6 Resource (cn.taketoday.core.io.Resource)6 Type (java.lang.reflect.Type)6 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)6 Charset (java.nio.charset.Charset)5 ByteArrayResource (cn.taketoday.core.io.ByteArrayResource)4 Field (java.lang.reflect.Field)4 Nullable (cn.taketoday.lang.Nullable)2