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
@@ -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();