Search in sources :

Example 11 with MockHttpInputMessage

use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.

the class GsonHttpMessageConverterTests method readInvalidJson.

@Test
public void readInvalidJson() throws IOException {
    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 12 with MockHttpInputMessage

use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.

the class GsonHttpMessageConverterTests method readUntyped.

@Test
@SuppressWarnings("unchecked")
public void readUntyped() throws IOException {
    String body = "{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    HashMap<String, Object> result = (HashMap<String, Object>) this.converter.read(HashMap.class, inputMessage);
    assertThat(result.get("string")).isEqualTo("Foo");
    Number n = (Number) result.get("number");
    assertThat(n.longValue()).isEqualTo(42);
    n = (Number) result.get("fraction");
    assertThat(n.doubleValue()).isCloseTo(42D, within(0D));
    List<String> array = new ArrayList<>();
    array.add("Foo");
    array.add("Bar");
    assertThat(result.get("array")).isEqualTo(array);
    assertThat(result.get("bool")).isEqualTo(Boolean.TRUE);
    byte[] bytes = new byte[2];
    List<Number> resultBytes = (ArrayList<Number>) result.get("bytes");
    for (int i = 0; i < 2; i++) {
        bytes[i] = resultBytes.get(i).byteValue();
    }
    assertThat(bytes).isEqualTo(new byte[] { 0x1, 0x2 });
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MediaType(org.springframework.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 13 with MockHttpInputMessage

use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.

the class GsonHttpMessageConverterTests method writeParameterizedBaseType.

@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
    };
    ParameterizedTypeReference<List<MyBase>> baseList = new ParameterizedTypeReference<>() {
    };
    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"));
    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, baseList.getType(), new 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) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 14 with MockHttpInputMessage

use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.

the class GsonHttpMessageConverterTests method readAndWriteParameterizedType.

@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<>() {
    };
    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"));
    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(), new 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) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 15 with MockHttpInputMessage

use of org.springframework.http.MockHttpInputMessage in project spring-framework by spring-projects.

the class GsonHttpMessageConverterTests method readTyped.

@Test
public void readTyped() throws IOException {
    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"));
    MyBean result = (MyBean) this.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 });
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MediaType(org.springframework.http.MediaType) 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