Polish byte-range resource handling

This commit is contained in:
Brian Clozel
2016-09-20 23:46:21 +02:00
parent 129d7bef6e
commit 578af59f0c
4 changed files with 34 additions and 29 deletions

View File

@@ -20,9 +20,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Collections;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -36,6 +34,7 @@ import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRange;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
@@ -60,9 +59,6 @@ public class ResourceHttpMessageWriterTests {
private Resource resource;
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Before
public void setUp() throws Exception {
@@ -84,6 +80,7 @@ public class ResourceHttpMessageWriterTests {
assertThat(this.response.getHeaders().getContentType(), is(MediaType.TEXT_PLAIN));
assertThat(this.response.getHeaders().getContentLength(), is(39L));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
TestSubscriber.subscribe(result).assertComplete().assertValues("Spring Framework test resource content.");
@@ -98,6 +95,7 @@ public class ResourceHttpMessageWriterTests {
assertThat(this.response.getHeaders().getContentType(), is(MediaType.TEXT_PLAIN));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/39"));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
assertThat(this.response.getHeaders().getContentLength(), is(6L));
Mono<String> result = reduceToString(this.response.getBody(), this.response.bufferFactory());
@@ -105,12 +103,14 @@ public class ResourceHttpMessageWriterTests {
}
@Test
public void shouldThrowErrorInvalidRange() throws Exception {
public void shouldSetRangeNotSatisfiableStatus() throws Exception {
this.request.getHeaders().set(HttpHeaders.RANGE, "invalid");
this.expectedException.expect(IllegalArgumentException.class);
TestSubscriber.subscribe(this.writer.write(Mono.just(resource), null, ResolvableType.forClass(Resource.class),
MediaType.TEXT_PLAIN, this.request, this.response, Collections.emptyMap()));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
assertThat(this.response.getStatusCode(), is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE));
}
private Mono<String> reduceToString(Publisher<DataBuffer> buffers, DataBufferFactory bufferFactory) {