use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
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);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method readAndWriteGenerics.
@Test
@SuppressWarnings("unchecked")
public void readAndWriteGenerics() throws Exception {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter() {
@Override
protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
return new ObjectMapper().getTypeFactory().constructCollectionType(ArrayList.class, MyBean.class);
} else {
return super.getJavaType(type, contextClass);
}
}
};
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"));
List<MyBean> results = (List<MyBean>) converter.read(List.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, MediaType.APPLICATION_JSON, outputMessage);
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
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 org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class Jaxb2RootElementHttpMessageConverterTests method readXmlType.
@Test
public void readXmlType() throws Exception {
byte[] body = "<foo s=\"Hello World\"/>".getBytes(StandardCharsets.UTF_8);
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
Type result = (Type) converter.read(Type.class, inputMessage);
assertThat(result.s).as("Invalid result").isEqualTo("Hello World");
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class Jaxb2RootElementHttpMessageConverterTests method readXmlRootElementExternalEntityEnabled.
@Test
public void readXmlRootElementExternalEntityEnabled() throws Exception {
Resource external = new ClassPathResource("external.txt", getClass());
String content = "<!DOCTYPE root [" + " <!ELEMENT external ANY >\n" + " <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + " <rootElement><external>&ext;</external></rootElement>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes(StandardCharsets.UTF_8));
this.converter.setProcessExternalEntities(true);
RootElement rootElement = (RootElement) converter.read(RootElement.class, inputMessage);
assertThat(rootElement.external).isEqualTo("Foo Bar");
}
Aggregations