Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTTPCLIENT-2354: Allow caching of responses with "must-revalidate, max-age=0" in shared caches #609

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public boolean isResponseCacheable(final ResponseCacheControl cacheControl, fina
if (sharedCache) {
if (request.containsHeader(HttpHeaders.AUTHORIZATION) &&
cacheControl.getSharedMaxAge() == -1 &&
!cacheControl.isPublic()) {
!(cacheControl.isPublic() || cacheControl.isMustRevalidate())) {
LOG.debug("Request contains private credentials");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,4 +944,60 @@ void testImmutableAndFreshResponseIsCacheable() {

Assertions.assertTrue(policy.isResponseCacheable(responseCacheControl, request, response));
}

@Test
void testPublicWithAuthorizationIsCacheable() {
request = new BasicHttpRequest("GET", "/resource");
request.setHeader(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd2Q=");
response.setHeader("Cache-Control", "public");
responseCacheControl = ResponseCacheControl.builder()
.setCachePublic(true)
.build();

final boolean isCacheable = policy.isResponseCacheable(responseCacheControl, request, response);
Assertions.assertTrue(isCacheable,
"Response with public directive and Authorization header should be cacheable in shared cache.");
}

@Test
void testSMaxageWithAuthorizationIsCacheable() {
request = new BasicHttpRequest("GET", "/resource");
request.setHeader(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd2Q=");
response.setHeader("Cache-Control", "s-maxage=60");
responseCacheControl = ResponseCacheControl.builder()
.setSharedMaxAge(60)
.build();

final boolean isCacheable = policy.isResponseCacheable(responseCacheControl, request, response);
Assertions.assertTrue(isCacheable,
"Response with s-maxage and Authorization header should be cacheable in shared cache.");
}

@Test
void testNoDirectivesWithAuthorizationNotCacheable() {
request = new BasicHttpRequest("GET", "/resource");
request.setHeader(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd2Q=");
response.setHeader("Cache-Control", "");
responseCacheControl = ResponseCacheControl.builder()
.build();

final boolean isCacheable = policy.isResponseCacheable(responseCacheControl, request, response);
Assertions.assertFalse(isCacheable,
"Response without must-revalidate, public, or s-maxage should not be cacheable with Authorization header.");
}

@Test
void testMustRevalidateWithAuthorizationIsCacheable() {
request = new BasicHttpRequest("GET", "/resource");
request.setHeader(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd2Q=");
response.setHeader("Cache-Control", "must-revalidate");
responseCacheControl = ResponseCacheControl.builder()
.setMustRevalidate(true)
.build();

final boolean isCacheable = policy.isResponseCacheable(responseCacheControl, request, response);
Assertions.assertTrue(isCacheable,
"Response with must-revalidate and Authorization header should be cacheable in shared cache.");
}

}
Loading