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 });
}
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");
}
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"));
}
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);
}
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);
}
Aggregations