HTTPCLIENT-2277: Cache update bug fix

This commit is contained in:
Oleg Kalnichevski 2023-10-30 17:29:20 +01:00
parent 7724432894
commit 1492f57a84
4 changed files with 33 additions and 12 deletions

View File

@ -232,12 +232,16 @@ public class HttpCacheEntryFactory {
*
* @param requestInstant Date/time when the request was made (Used for age calculations)
* @param responseInstant Date/time that the response came back (Used for age calculations)
* @param host Target host
* @param request Original client request (a deep copy of this object is made)
* @param response Origin response (a deep copy of this object is made)
* @param entry Existing cache entry.
*/
public HttpCacheEntry createUpdated(
final Instant requestInstant,
final Instant responseInstant,
final HttpHost host,
final HttpRequest request,
final HttpResponse response,
final HttpCacheEntry entry) {
Args.notNull(requestInstant, "Request instant");
@ -249,13 +253,16 @@ public class HttpCacheEntryFactory {
if (HttpCacheEntry.isNewer(entry, response)) {
return entry;
}
final String s = CacheKeyGenerator.getRequestUri(host, request);
final URI uri = CacheKeyGenerator.normalize(s);
final HeaderGroup requestHeaders = filterHopByHopHeaders(request);
final HeaderGroup mergedHeaders = mergeHeaders(entry, response);
return new HttpCacheEntry(
requestInstant,
responseInstant,
entry.getRequestMethod(),
entry.getRequestURI(),
headers(entry.requestHeaderIterator()),
request.getMethod(),
uri.toASCIIString(),
requestHeaders,
entry.getStatus(),
mergedHeaders,
entry.getResource(),

View File

@ -435,6 +435,8 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
final HttpCacheEntry updatedEntry = cacheEntryFactory.createUpdated(
requestSent,
responseReceived,
host,
request,
originResponse,
stale.entry);
final String variantKey = cacheKeyGenerator.generateVariantKey(request, updatedEntry);
@ -456,6 +458,8 @@ class BasicHttpAsyncCache implements HttpAsyncCache {
final HttpCacheEntry updatedEntry = cacheEntryFactory.createUpdated(
requestSent,
responseReceived,
host,
request,
originResponse,
negotiated.entry);

View File

@ -266,6 +266,8 @@ class BasicHttpCache implements HttpCache {
final HttpCacheEntry updatedEntry = cacheEntryFactory.createUpdated(
requestSent,
responseReceived,
host,
request,
originResponse,
stale.entry);
final String variantKey = cacheKeyGenerator.generateVariantKey(request, updatedEntry);
@ -286,8 +288,10 @@ class BasicHttpCache implements HttpCache {
final HttpCacheEntry updatedEntry = cacheEntryFactory.createUpdated(
requestSent,
responseReceived,
host,
request,
originResponse,
negotiated.entry);
negotiated.entry);
storeInternal(negotiated.getEntryKey(), updatedEntry);
final String rootKey = cacheKeyGenerator.generateKey(host, request);

View File

@ -211,7 +211,7 @@ public class TestHttpCacheEntryFactory {
@Test
public void testUpdateCacheEntryReturnsDifferentEntryInstance() {
entry = HttpTestUtils.makeCacheEntry();
final HttpCacheEntry newEntry = impl.createUpdated(requestDate, responseDate, response, entry);
final HttpCacheEntry newEntry = impl.createUpdated(requestDate, responseDate, host, request, response, entry);
Assertions.assertNotSame(newEntry, entry);
}
@ -339,17 +339,23 @@ public class TestHttpCacheEntryFactory {
new BasicHeader("Cache-Control", "public")
);
final HttpCacheEntry updatedEntry = impl.createUpdated(tenSecondsAgo, oneSecondAgo, response, entry);
request.setHeaders(
new BasicHeader("X-custom", "my other stuff"),
new BasicHeader(HttpHeaders.ACCEPT, "stuff, other-stuff"),
new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "en, de")
);
final HttpCacheEntry updatedEntry = impl.createUpdated(tenSecondsAgo, oneSecondAgo, host, request, response, entry);
MatcherAssert.assertThat(updatedEntry, HttpCacheEntryMatcher.equivalent(
HttpTestUtils.makeCacheEntry(
tenSecondsAgo,
oneSecondAgo,
Method.GET,
"/stuff",
"http://foo.example.com:80/stuff",
new Header[]{
new BasicHeader("X-custom", "my stuff"),
new BasicHeader(HttpHeaders.ACCEPT, "stuff"),
new BasicHeader("X-custom", "my other stuff"),
new BasicHeader(HttpHeaders.ACCEPT, "stuff, other-stuff"),
new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "en, de")
},
HttpStatus.SC_NOT_MODIFIED,
@ -374,7 +380,7 @@ public class TestHttpCacheEntryFactory {
response.setHeader("Date", DateUtils.formatStandardDate(tenSecondsAgo));
response.setHeader("ETag", "\"old-etag\"");
final HttpCacheEntry newEntry = impl.createUpdated(Instant.now(), Instant.now(), response, entry);
final HttpCacheEntry newEntry = impl.createUpdated(Instant.now(), Instant.now(), host, request, response, entry);
Assertions.assertSame(newEntry, entry);
}
@ -382,7 +388,7 @@ public class TestHttpCacheEntryFactory {
@Test
public void testUpdateHasLatestRequestAndResponseDates() {
entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
final HttpCacheEntry updated = impl.createUpdated(twoSecondsAgo, oneSecondAgo, response, entry);
final HttpCacheEntry updated = impl.createUpdated(twoSecondsAgo, oneSecondAgo, host, request, response, entry);
Assertions.assertEquals(twoSecondsAgo, updated.getRequestInstant());
Assertions.assertEquals(oneSecondAgo, updated.getResponseInstant());
@ -393,7 +399,7 @@ public class TestHttpCacheEntryFactory {
entry = HttpTestUtils.makeCacheEntry();
response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
Assertions.assertThrows(IllegalArgumentException.class, () ->
impl.createUpdated(Instant.now(), Instant.now(), response, entry));
impl.createUpdated(Instant.now(), Instant.now(), host, request, response, entry));
}
}