Read Expires cookie attribute in HttpComponents connector

Prior to this commit, the HttpComponents implementation for the
`WebClient` would only consider the max-age attribute of response
cookies when parsing the response. This is not aligned with other client
implementations that consider the max-age attribute first, and then the
expires if the former was not present. The expires date is then
translated into a max-age duration. This behavior is done naturally by
several implementations.

This commit updates the `HttpComponentsClientHttpResponse` to do the
same.

Fixes gh-33157
This commit is contained in:
Brian Clozel
2024-09-13 15:49:06 +02:00
parent b388ff60dd
commit 5efb385e64
2 changed files with 47 additions and 3 deletions

View File

@@ -23,6 +23,10 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -57,7 +61,9 @@ import static org.assertj.core.api.Assertions.fail;
import static org.junit.jupiter.api.Named.named;
/**
* Tests for {@link ClientHttpConnector} implementations.
* @author Arjen Poutsma
* @author Brian Clozel
*/
class ClientHttpConnectorTests {
@@ -172,6 +178,26 @@ class ClientHttpConnectorTests {
.verify();
}
@ParameterizedConnectorTest
void cookieExpireValueSetAsMaxAge(ClientHttpConnector connector) {
ZonedDateTime tomorrow = ZonedDateTime.now(ZoneId.of("UTC")).plusDays(1);
String formattedDate = tomorrow.format(DateTimeFormatter.RFC_1123_DATE_TIME);
prepareResponse(response -> {
response.setResponseCode(200);
response.addHeader("Set-Cookie", "id=test; Expires= " + formattedDate + ";");
});
Mono<ClientHttpResponse> futureResponse =
connector.connect(HttpMethod.GET, this.server.url("/").uri(), ReactiveHttpOutputMessage::setComplete);
StepVerifier.create(futureResponse)
.assertNext(response -> {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getCookies().getFirst("id").getMaxAge()).isCloseTo(Duration.ofDays(1), Duration.ofSeconds(10));
}
)
.verifyComplete();
}
private Buffer randomBody(int size) {
Buffer responseBody = new Buffer();
Random rnd = new Random();
@@ -211,7 +237,8 @@ class ClientHttpConnectorTests {
return Arrays.asList(
named("Reactor Netty", new ReactorClientHttpConnector()),
named("Jetty", new JettyClientHttpConnector()),
named("HttpComponents", new HttpComponentsClientHttpConnector())
named("HttpComponents", new HttpComponentsClientHttpConnector()),
named("Jdk", new JdkClientHttpConnector())
);
}