Assert.state(this.resourceHttpMessageConverter != null, "Not initialized"); setHeaders(response, resource, mediaType); this.resourceHttpMessageConverter.write(resource, mediaType, outputMessage);
@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 // SPR-10848 public void writeByteArrayNullMediaType() throws IOException { MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); byte[] byteArray = {1, 2, 3}; Resource body = new ByteArrayResource(byteArray); converter.write(body, null, outputMessage); assertTrue(Arrays.equals(byteArray, outputMessage.getBodyAsBytes())); }
@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()); }
Assert.state(this.resourceHttpMessageConverter != null, "Not initialized"); setHeaders(response, resource, mediaType); this.resourceHttpMessageConverter.write(resource, mediaType, outputMessage);
@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 // 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()); }
if (request.getHeader(HttpHeaders.RANGE) == null) { setHeaders(response, resource, mediaType); this.resourceHttpMessageConverter.write(resource, mediaType, outputMessage); if (httpRanges.size() == 1) { ResourceRegion resourceRegion = httpRanges.get(0).toResourceRegion(resource); this.resourceRegionHttpMessageConverter.write(resourceRegion, mediaType, outputMessage);