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");
}
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);
}
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");
}
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");
}
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);
}
Aggregations