Clean HTTP response headers after decompression in JDK client

Prior to this commit, gh-35225 introduced HTTP response body
decompression support for "gzip" and "deflate" encodings for the
`JdkClientHttpRequestFactory`.

While body decompression works, the client keeps the "Content-Encoding"
and "Content-Length" response headers intact, which misleads further
response handling: the body size has changed and it is not compressed
anymore.

This commit ensures that the relevant response headers are removed from
the HTTP response after decompression.

Fixes gh-35668
This commit is contained in:
Brian Clozel
2025-10-20 11:38:25 +02:00
parent b85993c7cb
commit 5accc21223
4 changed files with 31 additions and 16 deletions
@@ -136,6 +136,7 @@ public abstract class AbstractMockWebServerTests {
.body(buffer)
.code(200);
builder.setHeader(HttpHeaders.CONTENT_ENCODING, encoding);
builder.setHeader(HttpHeaders.CONTENT_LENGTH, buffer.size());
return builder.build();
}
return new MockResponse.Builder().code(404).build();
@@ -135,7 +135,9 @@ class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
try (ClientHttpResponse response = request.execute()) {
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().getFirst("Content-Encoding"))
.as("Invalid content encoding").isEqualTo("gzip");
.as("Content Encoding should be removed").isNull();
assertThat(response.getHeaders().getFirst("Content-Length"))
.as("Content-Length should be removed").isNull();
assertThat(response.getBody()).as("Invalid request body").hasContent("Payload to compress");
}
}
@@ -150,7 +152,9 @@ class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
try (ClientHttpResponse response = request.execute()) {
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().getFirst("Content-Encoding"))
.as("Invalid content encoding").isEqualTo("deflate");
.as("Content Encoding should be removed").isNull();
assertThat(response.getHeaders().getFirst("Content-Length"))
.as("Content-Length should be removed").isNull();
assertThat(response.getBody()).as("Invalid request body").hasContent("Payload to compress");
}
}