Search in sources :

Example 36 with MockHttpInputMessage

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));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 37 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Marshaller(org.springframework.oxm.Marshaller) StreamSource(javax.xml.transform.stream.StreamSource) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.jupiter.api.Test)

Example 38 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) StreamSource(javax.xml.transform.stream.StreamSource) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.jupiter.api.Test)

Example 39 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) StreamSource(javax.xml.transform.stream.StreamSource) UnmarshallingFailureException(org.springframework.oxm.UnmarshallingFailureException) Unmarshaller(org.springframework.oxm.Unmarshaller) Test(org.junit.jupiter.api.Test)

Example 40 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) MediaType(org.springframework.http.MediaType) ClassPathResource(org.springframework.core.io.ClassPathResource) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)76 Test (org.junit.jupiter.api.Test)65 MediaType (org.springframework.http.MediaType)31 ArrayList (java.util.ArrayList)16 List (java.util.List)15 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)13 Test (org.junit.Test)11 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)8 ClassPathResource (org.springframework.core.io.ClassPathResource)8 Resource (org.springframework.core.io.Resource)7 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)7 Type (java.lang.reflect.Type)5 HashMap (java.util.HashMap)5 Message (com.google.protobuf.Message)4 InputStream (java.io.InputStream)4 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)4 StreamSource (javax.xml.transform.stream.StreamSource)4 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)4 XmlRootElement (jakarta.xml.bind.annotation.XmlRootElement)3 Charset (java.nio.charset.Charset)3