Skip to content

Commit

Permalink
Do not add Accept-Ranges header on client-side
Browse files Browse the repository at this point in the history
This commit fixes a regression that added the Accept-Ranges header on
both client and server. Accept-Ranges is response header, so we now make
sure it only appears on the server side.

See gh-28291
  • Loading branch information
poutsma committed Apr 12, 2022
1 parent 3c37fa2 commit 5fc8a98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ public void addHeaders(ReactiveHttpOutputMessage message, Resource resource, @Nu
headers.setContentLength(length);
}
}
headers.set(HttpHeaders.ACCEPT_RANGES, "bytes");
if (message instanceof ServerHttpResponse) {
// server side
headers.set(HttpHeaders.ACCEPT_RANGES, "bytes");
}
}

private static MediaType getResourceMediaType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import org.springframework.core.ResolvableType;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
import org.springframework.http.HttpStatus;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.testfixture.http.client.reactive.MockClientHttpRequest;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse;

Expand Down Expand Up @@ -66,7 +69,7 @@ public void getWritableMediaTypes() throws Exception {
}

@Test
public void writeResource() throws Exception {
public void writeResourceServer() throws Exception {

testWrite(get("/").build());

Expand All @@ -78,6 +81,21 @@ public void writeResource() throws Exception {
StepVerifier.create(this.response.getBodyAsString()).expectNext(content).expectComplete().verify();
}

@Test
public void writeResourceClient() throws Exception {

MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, "/");
Mono<Void> mono = this.writer.write(this.input, ResolvableType.forClass(Resource.class), TEXT_PLAIN, request, HINTS);
StepVerifier.create(mono).expectComplete().verify();

assertThat(request.getHeaders().getContentType()).isEqualTo(TEXT_PLAIN);
assertThat(request.getHeaders().getContentLength()).isEqualTo(39L);
assertThat(request.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES)).isNull();

String content = "Spring Framework test resource content.";
StepVerifier.create(request.getBodyAsString()).expectNext(content).expectComplete().verify();
}

@Test
public void writeSingleRegion() throws Exception {

Expand Down

0 comments on commit 5fc8a98

Please sign in to comment.