Search in sources :

Example 71 with MockHttpInputMessage

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

the class SourceHttpMessageConverterTests method readDOMSource.

@Test
public void readDOMSource() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    DOMSource result = (DOMSource) converter.read(DOMSource.class, inputMessage);
    Document document = (Document) result.getNode();
    assertThat(document.getDocumentElement().getLocalName()).as("Invalid result").isEqualTo("root");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) DOMSource(javax.xml.transform.dom.DOMSource) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

Example 72 with MockHttpInputMessage

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

the class SourceHttpMessageConverterTests method readSAXSourceExternal.

@Test
public void readSAXSourceExternal() throws Exception {
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(bodyExternal.getBytes(StandardCharsets.UTF_8));
    inputMessage.getHeaders().setContentType(MediaType.APPLICATION_XML);
    converter.setSupportDtd(true);
    SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
    InputSource inputSource = result.getInputSource();
    XMLReader reader = result.getXMLReader();
    reader.setContentHandler(new DefaultHandler() {

        @Override
        public void characters(char[] ch, int start, int length) {
            String s = new String(ch, start, length);
            assertThat(s).as("Invalid result").isNotEqualTo("Foo Bar");
        }
    });
    reader.parse(inputSource);
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.jupiter.api.Test)

Example 73 with MockHttpInputMessage

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

the class RssChannelHttpMessageConverterTests method read.

@Test
public void read() throws IOException {
    InputStream inputStream = getClass().getResourceAsStream("rss.xml");
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(inputStream);
    inputMessage.getHeaders().setContentType(RSS_XML_UTF8);
    Channel result = converter.read(Channel.class, inputMessage);
    assertThat(result.getTitle()).isEqualTo("title");
    assertThat(result.getLink()).isEqualTo("https://example.com");
    assertThat(result.getDescription()).isEqualTo("description");
    List<?> items = result.getItems();
    assertThat(items.size()).isEqualTo(2);
    Item item1 = (Item) items.get(0);
    assertThat(item1.getTitle()).isEqualTo("title1");
    Item item2 = (Item) items.get(1);
    assertThat(item2.getTitle()).isEqualTo("title2");
}
Also used : Item(com.rometools.rome.feed.rss.Item) MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) InputStream(java.io.InputStream) Channel(com.rometools.rome.feed.rss.Channel) Test(org.junit.jupiter.api.Test)

Example 74 with MockHttpInputMessage

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

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.getFilename()).isEqualTo("yourlogo.jpg");
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) 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)

Example 75 with MockHttpInputMessage

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

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(org.springframework.http.MockHttpInputMessage) 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