Search in sources :

Example 1 with MockHttpInputMessage

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

the class FormHttpMessageConverterTests method readForm.

@Test
public void readForm() throws Exception {
    String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.ISO_8859_1));
    inputMessage.getHeaders().setContentType(new MediaType("application", "x-www-form-urlencoded", StandardCharsets.ISO_8859_1));
    MultiValueMap<String, String> result = this.converter.read(null, inputMessage);
    assertThat(result.size()).as("Invalid result").isEqualTo(3);
    assertThat(result.getFirst("name 1")).as("Invalid result").isEqualTo("value 1");
    List<String> values = result.get("name 2");
    assertThat(values.size()).as("Invalid result").isEqualTo(2);
    assertThat(values.get(0)).as("Invalid result").isEqualTo("value 2+1");
    assertThat(values.get(1)).as("Invalid result").isEqualTo("value 2+2");
    assertThat(result.getFirst("name 3")).as("Invalid result").isNull();
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 2 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readNonUnicode.

@Test
@SuppressWarnings("unchecked")
public void readNonUnicode() throws Exception {
    String body = "{\"føø\":\"bår\"}";
    Charset charset = StandardCharsets.ISO_8859_1;
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json", charset));
    HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);
    assertThat(result).containsExactly(entry("føø", "bår"));
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 3 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readValidJsonWithUnknownProperty.

@Test
public void readValidJsonWithUnknownProperty() throws IOException {
    String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    converter.read(MyBean.class, inputMessage);
// Assert no HttpMessageNotReadableException is thrown
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 4 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readAndWriteGenerics.

@Test
@SuppressWarnings("unchecked")
public void readAndWriteGenerics() throws Exception {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() {

        @Override
        protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
            if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
                return new ObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, MyBean.class);
            } else {
                return super.getJavaType(type, contextClass);
            }
        }
    };
    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"));
    List<MyBean> results = (List<MyBean>) converter.read(List.class, 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, new MediaType("application", "json"), outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JavaType(com.fasterxml.jackson.databind.JavaType) Type(java.lang.reflect.Type) MediaType(cn.taketoday.http.MediaType) MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MediaType(cn.taketoday.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Nullable(cn.taketoday.lang.Nullable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 5 with MockHttpInputMessage

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

the class GsonHttpMessageConverterTests method readAndWriteParameterizedType.

@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
    TypeReference<List<MyBean>> beansList = new TypeReference<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"));
    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, beansList.getType(), new MediaType("application", "json"), outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MediaType(cn.taketoday.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) 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