@Override public String getContentType() { MediaType type = this.outputMessage.getHeaders().getContentType(); return (type != null ? type.toString() : null); }
@Override public String getCharacterEncoding() { MediaType type = this.outputMessage.getHeaders().getContentType(); return (type != null && type.getCharset() != null ? type.getCharset().name() : null); }
@Test public void shouldWriteImageResource() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource body = new ClassPathResource("logo.jpg", getClass()); converter.write(body, null, outputMessage); assertEquals("Invalid content-type", MediaType.IMAGE_JPEG, outputMessage.getHeaders().getContentType()); assertEquals("Invalid content-length", body.getFile().length(), outputMessage.getHeaders().getContentLength()); }
@Test public void writeStreamSource() throws Exception { String xml = "<root>Hello World</root>"; StreamSource streamSource = new StreamSource(new StringReader(xml)); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(streamSource, null, outputMessage); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<root>Hello World</root>")); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); }
@Test // SPR-12999 public void writeContentNotClosingInputStream() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource resource = mock(Resource.class); InputStream inputStream = mock(InputStream.class); given(resource.getInputStream()).willReturn(inputStream); given(inputStream.read(any())).willReturn(-1); doThrow(new NullPointerException()).when(inputStream).close(); converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage); assertEquals(0, outputMessage.getHeaders().getContentLength()); }
@Test // SPR-8867 public void writeOverrideRequestedContentType() throws IOException { String body = "H\u00e9llo W\u00f6rld"; MediaType requestedContentType = new MediaType("text", "html"); HttpHeaders headers = this.outputMessage.getHeaders(); headers.setContentType(TEXT_PLAIN_UTF_8); this.converter.write(body, requestedContentType, this.outputMessage); assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength()); assertFalse(headers.getAcceptCharset().isEmpty()); }
@Test public void getContentLength() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); MediaType contentType = ProtobufHttpMessageConverter.PROTOBUF; this.converter.write(this.testMsg, contentType, outputMessage); assertEquals(-1, outputMessage.getHeaders().getContentLength()); }
@Test public void write() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); byte[] body = new byte[]{0x1, 0x2}; converter.write(body, null, outputMessage); assertArrayEquals("Invalid result", body, outputMessage.getBodyAsBytes()); assertEquals("Invalid content-type", new MediaType("application", "octet-stream"), outputMessage.getHeaders().getContentType()); assertEquals("Invalid content-length", 2, outputMessage.getHeaders().getContentLength()); }
@Test // SPR-12999 @SuppressWarnings("unchecked") public void writeContentNotGettingInputStream() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource resource = mock(Resource.class); given(resource.getInputStream()).willThrow(FileNotFoundException.class); converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage); assertEquals(0, outputMessage.getHeaders().getContentLength()); }
@Test public void writeUTF8() throws IOException { String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, TEXT_PLAIN_UTF_8, this.outputMessage); HttpHeaders headers = this.outputMessage.getHeaders(); assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8)); assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength()); assertFalse(headers.getAcceptCharset().isEmpty()); }
@Test public void prettyPrintWithSse() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); outputMessage.getHeaders().setContentType(MediaType.TEXT_EVENT_STREAM); PrettyPrintBean bean = new PrettyPrintBean(); bean.setName("Jason"); this.converter.setPrettyPrint(true); this.converter.writeInternal(bean, null, outputMessage); String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8); assertEquals("{\ndata: \"name\" : \"Jason\"\ndata:}", result); }
@Test public void writeOtherCharset() throws IOException, SAXException { Feed feed = new Feed("atom_1.0"); feed.setTitle("title"); String encoding = "ISO-8859-1"; feed.setEncoding(encoding); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(feed, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "atom+xml", Charset.forName(encoding)), outputMessage.getHeaders().getContentType()); }
@Test public void writeDefaultCharset() throws IOException { String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, null, this.outputMessage); HttpHeaders headers = this.outputMessage.getHeaders(); assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1)); assertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType()); assertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength()); assertFalse(headers.getAcceptCharset().isEmpty()); }
@Test // SPR-13620 @SuppressWarnings("unchecked") public void writeContentInputStreamThrowingNullPointerException() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Resource resource = mock(Resource.class); InputStream in = mock(InputStream.class); given(resource.getInputStream()).willReturn(in); given(in.read(any())).willThrow(NullPointerException.class); converter.write(resource, MediaType.APPLICATION_OCTET_STREAM, outputMessage); assertEquals(0, outputMessage.getHeaders().getContentLength()); }
@Test public void writeUTF16() throws IOException { MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, contentType, outputMessage); assertEquals("Invalid result", body, outputMessage.getBodyAsString(StandardCharsets.UTF_16BE)); assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType()); }
@Test public void writeUTF16() throws IOException { MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); String body = "H\u00e9llo W\u00f6rld"; this.converter.write(body, contentType, outputMessage); assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(StandardCharsets.UTF_16BE)); assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType()); }
@Test public void writeUTF16() throws IOException { MediaType contentType = new MediaType("application", "json", StandardCharsets.UTF_16BE); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); String body = "H\u00e9llo W\u00f6rld"; converter.write(body, contentType, outputMessage); assertEquals("Invalid result", "\"" + body + "\"", outputMessage.getBodyAsString(StandardCharsets.UTF_16BE)); assertEquals("Invalid content-type", contentType, outputMessage.getHeaders().getContentType()); }
@Test public void writeXmlRootElement() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElement, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev)); }
@Test public void writeXmlRootElementSubclass() throws Exception { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); converter.write(rootElementCglib, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE)); assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev)); }
@Test public void write() throws Exception { String body = "<root>Hello World</root>"; MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); Marshaller marshaller = mock(Marshaller.class); willDoNothing().given(marshaller).marshal(eq(body), isA(Result.class)); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); converter.write(body, null, outputMessage); assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders().getContentType()); }