Search in sources :

Example 6 with MockHttpInputMessage

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

the class GsonHttpMessageConverterTests 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 7 with MockHttpInputMessage

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

the class GsonHttpMessageConverterTests method writeParameterizedBaseType.

@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
    TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
    };
    TypeReference<List<MyBase>> baseList = new TypeReference<List<MyBase>>() {
    };
    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(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)

Example 8 with MockHttpInputMessage

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

the class JsonbHttpMessageConverterTests method readInvalidJson.

@Test
public void readInvalidJson() {
    String body = "FooBar";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.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 9 with MockHttpInputMessage

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

the class JsonbHttpMessageConverterTests method writeParameterizedBaseType.

@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
    TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
    };
    TypeReference<List<MyBase>> baseList = new TypeReference<List<MyBase>>() {
    };
    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(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)

Example 10 with MockHttpInputMessage

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

the class ProtobufHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    byte[] body = this.testMsg.toByteArray();
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
    Message result = this.converter.read(Msg.class, inputMessage);
    assertThat(result).isEqualTo(this.testMsg);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Message(com.google.protobuf.Message) 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