use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class JsonbHttpMessageConverterTests method readInvalidJson.
@Test
public void readInvalidJson() {
String body = "FooBar";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> this.converter.read(MyBean.class, inputMessage));
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MarshallingHttpMessageConverterTests method readWithTypeMismatchException.
@Test
public void readWithTypeMismatchException() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
Marshaller marshaller = mock(Marshaller.class);
Unmarshaller unmarshaller = mock(Unmarshaller.class);
given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(3);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(String.class, inputMessage)).withCauseInstanceOf(TypeMismatchException.class);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MarshallingHttpMessageConverterTests method read.
@Test
public void read() throws Exception {
String body = "<root>Hello World</root>";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
Unmarshaller unmarshaller = mock(Unmarshaller.class);
given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setUnmarshaller(unmarshaller);
String result = (String) converter.read(Object.class, inputMessage);
assertThat(result).as("Invalid result").isEqualTo(body);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class MarshallingHttpMessageConverterTests method readWithMarshallingFailureException.
@Test
public void readWithMarshallingFailureException() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");
Unmarshaller unmarshaller = mock(Unmarshaller.class);
given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setUnmarshaller(unmarshaller);
assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> converter.read(Object.class, inputMessage)).withCause(ex);
}
use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.
the class BufferedImageHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class);
byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream());
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg"));
BufferedImage result = converter.read(BufferedImage.class, inputMessage);
assertThat(result.getHeight()).as("Invalid height").isEqualTo(500);
assertThat(result.getWidth()).as("Invalid width").isEqualTo(750);
}
Aggregations