Search in sources :

Example 16 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.

the class ByteArrayHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    byte[] body = new byte[] { 0x1, 0x2 };
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    inputMessage.getHeaders().setContentType(new MediaType("application", "octet-stream"));
    byte[] result = converter.read(byte[].class, inputMessage);
    assertThat(result).as("Invalid result").isEqualTo(body);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MediaType(cn.taketoday.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 17 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.

the class ResourceHttpMessageConverterTests method shouldReadImageResource.

@Test
public void shouldReadImageResource() throws IOException {
    byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg"));
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
    inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
    inputMessage.getHeaders().setContentDisposition(ContentDisposition.attachment().filename("yourlogo.jpg").build());
    Resource actualResource = converter.read(Resource.class, inputMessage);
    assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream())).isEqualTo(body);
    assertThat(actualResource.getName()).isEqualTo("yourlogo.jpg");
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Resource(cn.taketoday.core.io.Resource) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) InputStreamResource(cn.taketoday.core.io.InputStreamResource) Test(org.junit.jupiter.api.Test)

Example 18 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.

the class ResourceHttpMessageConverterTests method shouldNotReadInputStreamResource.

// SPR-14882
@Test
public void shouldNotReadInputStreamResource() throws IOException {
    ResourceHttpMessageConverter noStreamConverter = new ResourceHttpMessageConverter(false);
    try (InputStream body = getClass().getResourceAsStream("logo.jpg")) {
        MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
        inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
        assertThatExceptionOfType(HttpMessageNotReadableException.class).isThrownBy(() -> noStreamConverter.read(InputStreamResource.class, inputMessage));
    }
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) InputStream(java.io.InputStream) InputStreamResource(cn.taketoday.core.io.InputStreamResource) Test(org.junit.jupiter.api.Test)

Example 19 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.

the class StringHttpMessageConverterTests method readJson.

// gh-24123
@Test
public void readJson() throws IOException {
    String body = "{\"result\":\"\u0414\u0410\"}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    String result = this.converter.read(String.class, inputMessage);
    assertThat(result).as("Invalid result").isEqualTo(body);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 20 with MockHttpInputMessage

use of cn.taketoday.http.MockHttpInputMessage in project today-infrastructure by TAKETODAY.

the class StringHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    String body = "Hello World";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(TEXT_PLAIN_UTF_8);
    String result = this.converter.read(String.class, inputMessage);
    assertThat(result).as("Invalid result").isEqualTo(body);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpInputMessage (cn.taketoday.http.MockHttpInputMessage)82 Test (org.junit.jupiter.api.Test)82 MediaType (cn.taketoday.http.MediaType)56 MockHttpOutputMessage (cn.taketoday.http.MockHttpOutputMessage)26 ArrayList (java.util.ArrayList)24 List (java.util.List)18 TypeReference (cn.taketoday.core.TypeReference)12 HashMap (java.util.HashMap)10 HttpMessageNotReadableException (cn.taketoday.http.converter.HttpMessageNotReadableException)9 Message (com.google.protobuf.Message)8 InputStream (java.io.InputStream)8 ClassPathResource (cn.taketoday.core.io.ClassPathResource)7 InputStreamResource (cn.taketoday.core.io.InputStreamResource)6 Resource (cn.taketoday.core.io.Resource)6 Type (java.lang.reflect.Type)6 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)6 Charset (java.nio.charset.Charset)5 ByteArrayResource (cn.taketoday.core.io.ByteArrayResource)4 Field (java.lang.reflect.Field)4 Nullable (cn.taketoday.lang.Nullable)2