use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
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 org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method readAndWriteParameterizedType.
@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
};
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(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(), 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 readNonUnicode.
@Test
@SuppressWarnings("unchecked")
public void readNonUnicode() throws Exception {
String body = "{\"føø\":\"bår\"}";
Charset charset = StandardCharsets.ISO_8859_1;
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("føø", "bår"));
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MappingJackson2HttpMessageConverterTests method readValidJsonWithUnknownProperty.
@Test
public void readValidJsonWithUnknownProperty() throws IOException {
String body = "{\"string\":\"string\",\"unknownProperty\":\"value\"}";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
converter.read(MyBean.class, inputMessage);
// Assert no HttpMessageNotReadableException is thrown
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class ProtobufJsonFormatHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
byte[] body = this.testMsg.toByteArray();
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(ProtobufHttpMessageConverter.PROTOBUF);
Message result = this.converter.read(Msg.class, inputMessage);
assertThat(result).isEqualTo(this.testMsg);
}
Aggregations