Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for ModifyResponseBodyGatewayFilter not setting up response content-type #2649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
Expand Down Expand Up @@ -233,6 +234,11 @@ public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
|| headers.containsKey(HttpHeaders.CONTENT_LENGTH)) {
messageBody = messageBody.doOnNext(data -> headers.setContentLength(data.readableByteCount()));
}

if (StringUtils.hasText(config.newContentType)) {
headers.set(HttpHeaders.CONTENT_TYPE, config.newContentType);
}

// TODO: fail if isStreamingMediaType?
return getDelegate().writeWith(messageBody);
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.util.UriComponentsBuilder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "spring.codec.max-in-memory-size=40")
Expand All @@ -63,6 +65,16 @@ public void testModificationOfResponseBody() {
.exchange().expectBody().json("{\"value\": \"httpbin compatible home\", \"length\": 23}");
}

@Test
public void testModificationOfContentType() {
URI uri = UriComponentsBuilder.fromUriString(this.baseUri + "/").build(true).toUri();

testClient.get().uri(uri).header("Host", "www.modifyresponsebodyjavacontenttype.org").accept(MediaType.ALL)
.exchange().expectHeader().valueEquals(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.expectBody().consumeWith(result -> assertThat(new String(result.getResponseBody(), UTF_8))
.isEqualTo("Modified response"));
}

@Test
public void modifyResponeBodyToLarge() {
testClient.post().uri("/post").header("Host", "www.modifyresponsebodyjavatoolarge.org")
Expand Down Expand Up @@ -97,6 +109,14 @@ public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
return Mono.just(toLarge);
}))
.uri(uri))
.route("modify_response_java_test_content_type",
r -> r.path("/").and().host("www.modifyresponsebodyjavacontenttype.org")
.filters(
f -> f.prefixPath("/httpbin").modifyResponseBody(String.class, String.class,
MediaType.TEXT_PLAIN_VALUE, (webExchange, originalResponse) -> {
return Mono.just("Modified response");
}))
.uri(uri))
.build();
}

Expand Down