Search in sources :

Example 11 with MockHttpInputMessage

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

the class MappingJackson2SmileHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    MyBean body = new MyBean();
    body.setString("Foo");
    body.setNumber(42);
    body.setFraction(42F);
    body.setArray(new String[] { "Foo", "Bar" });
    body.setBool(true);
    body.setBytes(new byte[] { 0x1, 0x2 });
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(mapper.writeValueAsBytes(body));
    inputMessage.getHeaders().setContentType(new MediaType("application", "x-jackson-smile"));
    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 12 with MockHttpInputMessage

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

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(RSS_XML_UTF8);
    Channel result = converter.read(Channel.class, inputMessage);
    assertThat(result.getTitle()).isEqualTo("title");
    assertThat(result.getLink()).isEqualTo("https://example.com");
    assertThat(result.getDescription()).isEqualTo("description");
    List<?> items = result.getItems();
    assertThat(items.size()).isEqualTo(2);
    Item item1 = (Item) items.get(0);
    assertThat(item1.getTitle()).isEqualTo("title1");
    Item item2 = (Item) items.get(1);
    assertThat(item2.getTitle()).isEqualTo("title2");
}
Also used : Item(com.rometools.rome.feed.rss.Item) MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) InputStream(java.io.InputStream) Channel(com.rometools.rome.feed.rss.Channel) Test(org.junit.jupiter.api.Test)

Example 13 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readAscii.

@Test
@SuppressWarnings("unchecked")
public void readAscii() throws Exception {
    String body = "{\"foo\":\"bar\"}";
    Charset charset = StandardCharsets.US_ASCII;
    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("foo", "bar"));
}
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 14 with MockHttpInputMessage

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

the class MappingJackson2HttpMessageConverterTests method readAndWriteParameterizedType.

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

Example 15 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure 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)

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