1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Bug fixed that caused gzipped responses to be returned from cache after cache validation (#298).

This commit is contained in:
aahlenst
2013-09-16 12:21:17 +02:00
parent 081258a266
commit 357f77221c
2 changed files with 14 additions and 1 deletions

View File

@@ -649,10 +649,17 @@ public class HttpEngine {
if (cachedResponseHeaders.validate(responseHeaders)) {
release(false);
ResponseHeaders combinedHeaders = cachedResponseHeaders.combine(responseHeaders);
setResponse(combinedHeaders, cachedResponseBody);
this.responseHeaders = combinedHeaders;
// Update the cache after applying the combined headers but before initializing the content
// stream, otherwise the Content-Encoding header (if present) will be stripped from the
// combined headers and not end up in the cache file if transparent gzip compression is
// turned on.
OkResponseCache responseCache = client.getOkResponseCache();
responseCache.trackConditionalCacheHit();
responseCache.update(cacheResponse, policy.getHttpConnectionToCache());
initContentStream(cachedResponseBody);
return;
} else {
Util.closeQuietly(cachedResponseBody);

View File

@@ -946,8 +946,14 @@ public final class HttpResponseCacheTest {
server.enqueue(
response.setBody(gzip("ABCABCABC".getBytes("UTF-8"))).addHeader("Content-Encoding: gzip"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
server.play();
// At least three request/response pairs are required because after the first request is cached
// a different execution path might be taken. Thus modifications to the cache applied during
// the second request might not be visible until another request is performed.
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
assertEquals("ABCABCABC", readAscii(openConnection(server.getUrl("/"))));
}