Search in sources :

Example 61 with MockHttpInputMessage

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"));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 62 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 63 with MockHttpInputMessage

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"));
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HashMap(java.util.HashMap) Charset(java.nio.charset.Charset) MediaType(org.springframework.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 64 with MockHttpInputMessage

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

Example 65 with MockHttpInputMessage

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);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Message(com.google.protobuf.Message) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) 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