Remove Content-* response headers for error handling
Prior to this commit, when WebFlux handlers added `"Content-*"` response headers and an error happened while handling the request, all those headers would not be cleared from the response before error handling. This commit clears those headers from the response in two places: * when invoking the handler and adapting the response * when writing the response body Not removing those headers might break HTTP clients since they're given wrong information about how to interpret the HTTP response body: the error response body might be very different from the original one. Fixes gh-24238
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,6 +30,7 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -37,11 +38,12 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class ServerHttpResponseTests {
|
||||
|
||||
@Test
|
||||
public void writeWith() throws Exception {
|
||||
void writeWith() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.writeWith(Flux.just(wrap("a"), wrap("b"), wrap("c"))).block();
|
||||
|
||||
@@ -56,7 +58,7 @@ public class ServerHttpResponseTests {
|
||||
}
|
||||
|
||||
@Test // SPR-14952
|
||||
public void writeAndFlushWithFluxOfDefaultDataBuffer() throws Exception {
|
||||
void writeAndFlushWithFluxOfDefaultDataBuffer() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
Flux<Flux<DefaultDataBuffer>> flux = Flux.just(Flux.just(wrap("foo")));
|
||||
response.writeAndFlushWith(flux).block();
|
||||
@@ -70,21 +72,35 @@ public class ServerHttpResponseTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeWithError() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.getHeaders().setContentLength(12);
|
||||
void writeWithFluxError() throws Exception {
|
||||
IllegalStateException error = new IllegalStateException("boo");
|
||||
response.writeWith(Flux.error(error)).onErrorResume(ex -> Mono.empty()).block();
|
||||
writeWithError(Flux.error(error));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeWithMonoError() throws Exception {
|
||||
IllegalStateException error = new IllegalStateException("boo");
|
||||
writeWithError(Mono.error(error));
|
||||
}
|
||||
|
||||
void writeWithError(Publisher<DataBuffer> body) throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set(HttpHeaders.CONTENT_ENCODING, "gzip");
|
||||
headers.setContentLength(12);
|
||||
response.writeWith(body).onErrorResume(ex -> Mono.empty()).block();
|
||||
|
||||
assertThat(response.statusCodeWritten).isFalse();
|
||||
assertThat(response.headersWritten).isFalse();
|
||||
assertThat(response.cookiesWritten).isFalse();
|
||||
assertThat(response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)).isFalse();
|
||||
assertThat(headers).doesNotContainKeys(HttpHeaders.CONTENT_TYPE, HttpHeaders.CONTENT_LENGTH,
|
||||
HttpHeaders.CONTENT_ENCODING);
|
||||
assertThat(response.body.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setComplete() throws Exception {
|
||||
void setComplete() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.setComplete().block();
|
||||
|
||||
@@ -95,7 +111,7 @@ public class ServerHttpResponseTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beforeCommitWithComplete() throws Exception {
|
||||
void beforeCommitWithComplete() throws Exception {
|
||||
ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.beforeCommit(() -> Mono.fromRunnable(() -> response.getCookies().add(cookie.getName(), cookie)));
|
||||
@@ -113,7 +129,7 @@ public class ServerHttpResponseTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beforeCommitActionWithSetComplete() throws Exception {
|
||||
void beforeCommitActionWithSetComplete() throws Exception {
|
||||
ResponseCookie cookie = ResponseCookie.from("ID", "123").build();
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.beforeCommit(() -> {
|
||||
@@ -130,7 +146,6 @@ public class ServerHttpResponseTests {
|
||||
}
|
||||
|
||||
|
||||
|
||||
private DefaultDataBuffer wrap(String a) {
|
||||
return new DefaultDataBufferFactory().wrap(ByteBuffer.wrap(a.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user