diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java index 67f574852..33bdbf10a 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java @@ -263,10 +263,9 @@ public class CachingExec implements ClientExecChain { requestCompliance.makeRequestCompliant(request); request.addHeader("Via",via); - flushEntriesInvalidatedByRequest(context.getTargetHost(), request); - if (!cacheableRequestPolicy.isServableFromCache(request)) { log.debug("Request is not servable from cache"); + flushEntriesInvalidatedByRequest(context.getTargetHost(), request); return callBackend(route, request, context, execAware); } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java index 553163aa8..5524f1e02 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java @@ -402,6 +402,26 @@ public class TestCachingExec extends TestCachingExecChain { verifyMocks(); } + @Test + public void testDoesNotFlushCachesOnCacheHit() throws Exception { + requestPolicyAllowsCaching(true); + requestIsFatallyNonCompliant(null); + + getCacheEntryReturns(mockCacheEntry); + doesNotFlushCache(); + cacheEntrySuitable(true); + cacheEntryValidatable(true); + + expect(mockResponseGenerator.generateResponse(isA(HttpRequestWrapper.class), isA(HttpCacheEntry.class))) + .andReturn(mockBackendResponse); + + replayMocks(); + final HttpResponse result = impl.execute(route, request, context); + verifyMocks(); + + Assert.assertSame(mockBackendResponse, result); + } + private IExpectationSetters implExpectsAnyRequestAndReturn( final CloseableHttpResponse response) throws Exception { final CloseableHttpResponse resp = impl.callBackend( diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java index f2ddc6738..b71caf926 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExecChain.java @@ -1777,4 +1777,9 @@ public abstract class TestCachingExecChain { .andReturn(mockCachedResponse); } + protected void doesNotFlushCache() throws IOException { + mockCache.flushInvalidatedCacheEntriesFor(isA(HttpHost.class), isA(HttpRequest.class)); + EasyMock.expectLastCall().andThrow(new AssertionError("flushInvalidatedCacheEntriesFor should not have been called")).anyTimes(); + } + }