use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.
the class MappingJackson2XmlHttpMessageConverterTests method readValidXmlWithUnknownProperty.
@Test
public void readValidXmlWithUnknownProperty() throws IOException {
String body = "<MyBean><string>string</string><unknownProperty>value</unknownProperty></MyBean>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
converter.read(MyBean.class, inputMessage);
// Assert no HttpMessageNotReadableException is thrown
}
use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.
the class MappingJackson2XmlHttpMessageConverterTests method readNonUnicode.
@Test
@SuppressWarnings("unchecked")
public void readNonUnicode() throws Exception {
String body = "<MyBean>" + "<string>føø bår</string>" + "</MyBean>";
Charset charset = StandardCharsets.ISO_8859_1;
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(charset));
inputMessage.getHeaders().setContentType(new MediaType("application", "xml", charset));
MyBean result = (MyBean) converter.read(MyBean.class, inputMessage);
assertThat(result.getString()).isEqualTo("føø bår");
}
use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.
the class MappingJackson2XmlHttpMessageConverterTests method readWithExternalReference.
@Test
public void readWithExternalReference() throws IOException {
String body = "<!DOCTYPE MyBean SYSTEM \"https://192.168.28.42/1.jsp\" [" + " <!ELEMENT root ANY >\n" + " <!ENTITY ext SYSTEM \"" + new ClassPathResource("external.txt", getClass()).getURI() + "\" >]><MyBean><string>&ext;</string></MyBean>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage));
}
use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.
the class MappingJackson2XmlHttpMessageConverterTests method readInvalidXml.
@Test
public void readInvalidXml() throws IOException {
String body = "FooBar";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(MyBean.class, inputMessage));
}
use of cn.taketoday.http.MockHttpInputMessage in project today-framework by TAKETODAY.
the class GsonHttpMessageConverterTests method readUntyped.
@Test
@SuppressWarnings("unchecked")
public void readUntyped() throws IOException {
String body = "{\"bytes\":[1,2],\"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"));
HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);
assertThat(result.get("string")).isEqualTo("Foo");
Number n = (Number) result.get("number");
assertThat(n.longValue()).isEqualTo(42);
n = (Number) result.get("fraction");
assertThat(n.doubleValue()).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);
byte[] bytes = new byte[2];
List<Number> resultBytes = (ArrayList<Number>) result.get("bytes");
for (int i = 0; i < 2; i++) {
bytes[i] = resultBytes.get(i).byteValue();
}
assertThat(bytes).isEqualTo(new byte[] { 0x1, 0x2 });
}
Aggregations