Search in sources :

Example 21 with ResourceRegion

use of org.springframework.core.io.support.ResourceRegion in project spring-framework by spring-projects.

the class ResourceRegionHttpMessageConverterTests method partialContentMultipleByteRangesInRandomOrderAndOverlapping.

@Test
public void partialContentMultipleByteRangesInRandomOrderAndOverlapping() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    Resource body = new ClassPathResource("byterangeresource.txt", getClass());
    List<HttpRange> rangeList = HttpRange.parseRanges("bytes=7-15,0-5,17-20,20-29");
    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()).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]).isEqualTo(boundary);
    assertThat(ranges[1]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[2]).isEqualTo("Content-Range: bytes 7-15/39");
    assertThat(ranges[3]).isEqualTo("Framework");
    assertThat(ranges[4]).isEqualTo(boundary);
    assertThat(ranges[5]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[6]).isEqualTo("Content-Range: bytes 0-5/39");
    assertThat(ranges[7]).isEqualTo("Spring");
    assertThat(ranges[8]).isEqualTo(boundary);
    assertThat(ranges[9]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[10]).isEqualTo("Content-Range: bytes 17-20/39");
    assertThat(ranges[11]).isEqualTo("test");
    assertThat(ranges[12]).isEqualTo(boundary);
    assertThat(ranges[13]).isEqualTo("Content-Type: text/plain");
    assertThat(ranges[14]).isEqualTo("Content-Range: bytes 20-29/39");
    assertThat(ranges[15]).isEqualTo("t resource");
}
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.jupiter.api.Test)

Aggregations

ResourceRegion (org.springframework.core.io.support.ResourceRegion)21 Test (org.junit.jupiter.api.Test)12 Resource (org.springframework.core.io.Resource)11 ClassPathResource (org.springframework.core.io.ClassPathResource)10 DataBuffer (org.springframework.core.io.buffer.DataBuffer)8 HttpHeaders (org.springframework.http.HttpHeaders)8 HttpRange (org.springframework.http.HttpRange)5 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)5 MediaType (org.springframework.http.MediaType)4 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ByteArrayResource (org.springframework.core.io.ByteArrayResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OptionalLong (java.util.OptionalLong)1