Skip to content

Commit

Permalink
Fix bug for ModifyResponseBodyGatewayFilter not setting up response c…
Browse files Browse the repository at this point in the history
…ontent-type, if newContentType is present in config. Fixes gh-2647 (#2649)
  • Loading branch information
muchnik authored Mar 8, 2024
1 parent 8c6cd46 commit 258dc09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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

0 comments on commit 258dc09

Please sign in to comment.