Support HttpComponents 5.6

This commit updates the HttpComponents HttpClient to refer to the parsed
`HttpEntity` for the content-related HTTP response headers such as
encoding and body length.

Closes gh-36100
This commit is contained in:
Brian Clozel
2026-01-06 16:56:45 +01:00
parent 3027d78f40
commit 97cd9dd03b
3 changed files with 31 additions and 2 deletions
+2 -2
View File
@@ -94,8 +94,8 @@ dependencies {
api("org.apache.derby:derby:10.16.1.1")
api("org.apache.derby:derbyclient:10.16.1.1")
api("org.apache.derby:derbytools:10.16.1.1")
api("org.apache.httpcomponents.client5:httpclient5:5.5")
api("org.apache.httpcomponents.core5:httpcore5-reactive:5.3.5")
api("org.apache.httpcomponents.client5:httpclient5:5.6")
api("org.apache.httpcomponents.core5:httpcore5-reactive:5.4")
api("org.apache.poi:poi-ooxml:5.2.5")
api("org.apache.tomcat.embed:tomcat-embed-core:11.0.15")
api("org.apache.tomcat.embed:tomcat-embed-websocket:11.0.15")
@@ -66,6 +66,16 @@ final class HttpComponentsClientHttpResponse implements ClientHttpResponse {
public HttpHeaders getHeaders() {
if (this.headers == null) {
MultiValueMap<String, String> adapter = new HttpComponentsHeadersAdapter(this.httpResponse);
// update headers if response is decompressed
HttpEntity httpEntity = this.httpResponse.getEntity();
if (httpEntity != null) {
if (httpEntity.getContentLength() == -1) {
adapter.remove(HttpHeaders.CONTENT_LENGTH);
}
if (httpEntity.getContentEncoding() == null) {
adapter.remove(HttpHeaders.CONTENT_ENCODING);
}
}
this.headers = HttpHeaders.readOnlyHttpHeaders(adapter);
}
return this.headers;
@@ -18,6 +18,7 @@ package org.springframework.http.client;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.stream.Stream;
@@ -32,9 +33,11 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.assertj.core.api.Assertions.assertThat;
@@ -60,6 +63,22 @@ class HttpComponentsClientHttpRequestFactoryTests extends AbstractHttpRequestFac
assertHttpMethod("patch", HttpMethod.PATCH);
}
@Test
void shouldDecompressWithExpectedHeaders() throws Exception {
ClientHttpRequest request = factory.createRequest(URI.create(baseUrl + "/compress/gzip"), HttpMethod.POST);
String message = "Hello World";
final byte[] body = message.getBytes(StandardCharsets.UTF_8);
StreamUtils.copy(body, request.getBody());
try (ClientHttpResponse response = request.execute()) {
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
String result = FileCopyUtils.copyToString(new InputStreamReader(response.getBody()));
assertThat(result).as("Invalid body").isEqualTo(message);
assertThat(response.getHeaders().get(HttpHeaders.CONTENT_ENCODING)).isNull();
assertThat(response.getHeaders().getContentLength()).isEqualTo(-1);
}
}
@Test
void assertCustomConfig() throws Exception {
HttpClient httpClient = HttpClientBuilder.create().build();