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