Search in sources :

Example 26 with MockHttpOutputMessage

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

the class ResourceHttpMessageConverterTests method writeContentNotGettingInputStream.

// SPR-12999
@Test
@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());
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) 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.Test)

Example 27 with MockHttpOutputMessage

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

the class ResourceHttpMessageConverterTests method writeContentInputStreamThrowingNullPointerException.

// SPR-13620
@Test
@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());
}
Also used : InputStream(java.io.InputStream) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) 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.Test)

Example 28 with MockHttpOutputMessage

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

the class ResourceHttpMessageConverterTests method writeByteArrayNullMediaType.

// SPR-10848
@Test
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()));
}
Also used : MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Test(org.junit.Test)

Example 29 with MockHttpOutputMessage

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

the class ResourceHttpMessageConverterTests method writeContentNotClosingInputStream.

// SPR-12999
@Test
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());
}
Also used : InputStream(java.io.InputStream) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) 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.Test)

Example 30 with MockHttpOutputMessage

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

the class ResourceRegionHttpMessageConverterTests method partialContentMultipleByteRanges.

@Test
public void partialContentMultipleByteRanges() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    Resource body = new ClassPathResource("byterangeresource.txt", getClass());
    List<HttpRange> rangeList = HttpRange.parseRanges("bytes=0-5,7-15,17-20,22-38");
    List<ResourceRegion> regions = new ArrayList<>();
    for (HttpRange range : rangeList) {
        regions.add(range.toResourceRegion(body));
    }
    converter.write(regions, MediaType.TEXT_PLAIN, outputMessage);
    HttpHeaders headers = outputMessage.getHeaders();
    assertThat(headers.getContentType().toString(), Matchers.startsWith("multipart/byteranges;boundary="));
    String boundary = "--" + headers.getContentType().toString().substring(30);
    String content = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
    assertThat(ranges[0], is(boundary));
    assertThat(ranges[1], is("Content-Type: text/plain"));
    assertThat(ranges[2], is("Content-Range: bytes 0-5/39"));
    assertThat(ranges[3], is("Spring"));
    assertThat(ranges[4], is(boundary));
    assertThat(ranges[5], is("Content-Type: text/plain"));
    assertThat(ranges[6], is("Content-Range: bytes 7-15/39"));
    assertThat(ranges[7], is("Framework"));
    assertThat(ranges[8], is(boundary));
    assertThat(ranges[9], is("Content-Type: text/plain"));
    assertThat(ranges[10], is("Content-Range: bytes 17-20/39"));
    assertThat(ranges[11], is("test"));
    assertThat(ranges[12], is(boundary));
    assertThat(ranges[13], is("Content-Type: text/plain"));
    assertThat(ranges[14], is("Content-Range: bytes 22-38/39"));
    assertThat(ranges[15], is("resource content."));
}
Also used : ResourceRegion(org.springframework.core.io.support.ResourceRegion) HttpHeaders(org.springframework.http.HttpHeaders) MockHttpOutputMessage(org.springframework.http.MockHttpOutputMessage) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) HttpRange(org.springframework.http.HttpRange) Test(org.junit.Test)

Aggregations

MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)49 Test (org.junit.Test)48 MediaType (org.springframework.http.MediaType)24 ClassPathResource (org.springframework.core.io.ClassPathResource)12 Resource (org.springframework.core.io.Resource)11 ByteArrayResource (org.springframework.core.io.ByteArrayResource)5 InputStreamResource (org.springframework.core.io.InputStreamResource)5 HttpHeaders (org.springframework.http.HttpHeaders)5 ArrayList (java.util.ArrayList)4 ResourceRegion (org.springframework.core.io.support.ResourceRegion)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 StringReader (java.io.StringReader)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 DifferenceEvaluator (org.xmlunit.diff.DifferenceEvaluator)3 Feed (com.rometools.rome.feed.atom.Feed)2 Channel (com.rometools.rome.feed.rss.Channel)2 Item (com.rometools.rome.feed.rss.Item)2 BufferedImage (java.awt.image.BufferedImage)2 InputStream (java.io.InputStream)2 Result (javax.xml.transform.Result)2