Search in sources :

Example 41 with MockHttpInputMessage

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

the class FormHttpMessageConverterTests method readForm.

@Test
public void readForm() throws Exception {
    String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.ISO_8859_1));
    inputMessage.getHeaders().setContentType(new MediaType("application", "x-www-form-urlencoded", StandardCharsets.ISO_8859_1));
    MultiValueMap<String, String> result = this.converter.read(null, inputMessage);
    assertThat(result.size()).as("Invalid result").isEqualTo(3);
    assertThat(result.getFirst("name 1")).as("Invalid result").isEqualTo("value 1");
    List<String> values = result.get("name 2");
    assertThat(values.size()).as("Invalid result").isEqualTo(2);
    assertThat(values.get(0)).as("Invalid result").isEqualTo("value 2+1");
    assertThat(values.get(1)).as("Invalid result").isEqualTo("value 2+2");
    assertThat(result.getFirst("name 3")).as("Invalid result").isNull();
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 42 with MockHttpInputMessage

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

the class AtomFeedHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    InputStream inputStream = getClass().getResourceAsStream("atom.xml");
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
    inputMessage.getHeaders().setContentType(ATOM_XML_UTF8);
    Feed result = converter.read(Feed.class, inputMessage);
    assertThat(result.getTitle()).isEqualTo("title");
    assertThat(result.getSubtitle().getValue()).isEqualTo("subtitle");
    List<?> entries = result.getEntries();
    assertThat(entries.size()).isEqualTo(2);
    Entry entry1 = (Entry) entries.get(0);
    assertThat(entry1.getId()).isEqualTo("id1");
    assertThat(entry1.getTitle()).isEqualTo("title1");
    Entry entry2 = (Entry) entries.get(1);
    assertThat(entry2.getId()).isEqualTo("id2");
    assertThat(entry2.getTitle()).isEqualTo("title2");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) Entry(com.rometools.rome.feed.atom.Entry) InputStream(java.io.InputStream) Feed(com.rometools.rome.feed.atom.Feed) Test(org.junit.jupiter.api.Test)

Example 43 with MockHttpInputMessage

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

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(org.springframework.http.MockHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 44 with MockHttpInputMessage

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

the class StringHttpMessageConverterTests method readJsonApi.

// gh-25328
@Test
public void readJsonApi() throws IOException {
    String body = "{\"result\":\"\u0414\u0410\"}";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(new MediaType("application", "vnd.api.v1+json"));
    String result = this.converter.read(String.class, inputMessage);
    assertThat(result).as("Invalid result").isEqualTo(body);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) MediaType(org.springframework.http.MediaType) Test(org.junit.jupiter.api.Test)

Example 45 with MockHttpInputMessage

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

the class ResourceHttpMessageConverterTests method shouldReadInputStreamResource.

// SPR-13443
@Test
public void shouldReadInputStreamResource() throws IOException {
    try (InputStream body = getClass().getResourceAsStream("logo.jpg")) {
        MockHttpInputMessage inputMessage = new MockHttpInputMessage(body);
        inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG);
        inputMessage.getHeaders().setContentDisposition(ContentDisposition.attachment().filename("yourlogo.jpg").build());
        inputMessage.getHeaders().setContentLength(123);
        Resource actualResource = converter.read(InputStreamResource.class, inputMessage);
        assertThat(actualResource).isInstanceOf(InputStreamResource.class);
        assertThat(actualResource.getInputStream()).isEqualTo(body);
        assertThat(actualResource.getFilename()).isEqualTo("yourlogo.jpg");
        assertThat(actualResource.contentLength()).isEqualTo(123);
    }
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) 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