Search in sources :

Example 26 with MockHttpInputMessage

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

the class GsonHttpMessageConverterTests method readInvalidJson.

@Test
public void readInvalidJson() throws IOException {
    String body = "FooBar";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage));
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 27 with MockHttpInputMessage

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

the class JsonbHttpMessageConverterTests method readAndWriteGenerics.

@Test
@SuppressWarnings("unchecked")
public void readAndWriteGenerics() throws Exception {
    Field beansList = ListHolder.class.getField("listField");
    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"));
    Type genericType = beansList.getGenericType();
    List<MyBean> results = (List<MyBean>) converter.read(genericType, MyBeanListHolder.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, genericType, new MediaType("application", "json"), outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : Field(java.lang.reflect.Field) MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Type(java.lang.reflect.Type) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MediaType(cn.taketoday.http.MediaType) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MediaType(cn.taketoday.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 28 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readTyped.

@Test
public void readTyped() throws IOException {
    String body = "{" + "\"bytes\":\"AQI=\"," + "\"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"));
    MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
    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 });
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 29 with MockHttpInputMessage

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

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(cn.taketoday.http.MockHttpInputMessage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 30 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readInvalidJson.

@Test
public void readInvalidJson() throws IOException {
    String body = "FooBar";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(MyBean.class, inputMessage));
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) HttpMessageNotReadableException(cn.taketoday.http.converter.HttpMessageNotReadableException) 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