From bdf417403339ae13abcb2e7e61ac3a4f3bf19fff Mon Sep 17 00:00:00 2001 From: Jonathan Moore Date: Thu, 13 Jan 2011 17:00:30 +0000 Subject: [PATCH] Capture acceptance tests for only-if-cached (implementation already passes these). "To do this, the client may include the only-if-cached directive in a request. If it receives this directive, a cache SHOULD either respond using a cached entry that is consistent with the other constraints of the request, or respond with a 504 (Gateway Timeout) status." http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4 git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1058661 13f79535-47bb-0310-9956-ffa450edef68 --- .../cache/TestProtocolRecommendations.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java index a805beffe..d75bbae9c 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestProtocolRecommendations.java @@ -1374,4 +1374,91 @@ public class TestProtocolRecommendations extends AbstractProtocolTest { assertTrue(foundNoCache); assertFalse(foundDisallowedDirective); } + + /* + * "To do this, the client may include the only-if-cached directive in + * a request. If it receives this directive, a cache SHOULD either + * respond using a cached entry that is consistent with the other + * constraints of the request, or respond with a 504 (Gateway Timeout) + * status." + * + * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4 + */ + @Test + public void cacheMissResultsIn504WithOnlyIfCached() + throws Exception { + HttpRequest req = HttpTestUtils.makeDefaultRequest(); + req.setHeader("Cache-Control", "only-if-cached"); + + replayMocks(); + HttpResponse result = impl.execute(host, req); + verifyMocks(); + + assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, + result.getStatusLine().getStatusCode()); + } + + @Test + public void cacheHitOkWithOnlyIfCached() + throws Exception { + HttpRequest req1 = HttpTestUtils.makeDefaultRequest(); + HttpResponse resp1 = HttpTestUtils.make200Response(); + resp1.setHeader("Cache-Control","max-age=3600"); + + backendExpectsAnyRequest().andReturn(resp1); + + HttpRequest req2 = HttpTestUtils.makeDefaultRequest(); + req2.setHeader("Cache-Control", "only-if-cached"); + + replayMocks(); + impl.execute(host, req1); + HttpResponse result = impl.execute(host, req2); + verifyMocks(); + + assertTrue(HttpTestUtils.semanticallyTransparent(resp1, result)); + } + + @Test + public void returns504ForStaleEntryWithOnlyIfCached() + throws Exception { + HttpRequest req1 = HttpTestUtils.makeDefaultRequest(); + HttpResponse resp1 = HttpTestUtils.make200Response(); + resp1.setHeader("Date", formatDate(tenSecondsAgo)); + resp1.setHeader("Cache-Control","max-age=5"); + + backendExpectsAnyRequest().andReturn(resp1); + + HttpRequest req2 = HttpTestUtils.makeDefaultRequest(); + req2.setHeader("Cache-Control", "only-if-cached"); + + replayMocks(); + impl.execute(host, req1); + HttpResponse result = impl.execute(host, req2); + verifyMocks(); + + assertEquals(HttpStatus.SC_GATEWAY_TIMEOUT, + result.getStatusLine().getStatusCode()); + } + + @Test + public void returnsStaleCacheEntryWithOnlyIfCachedAndMaxStale() + throws Exception { + + HttpRequest req1 = HttpTestUtils.makeDefaultRequest(); + HttpResponse resp1 = HttpTestUtils.make200Response(); + resp1.setHeader("Date", formatDate(tenSecondsAgo)); + resp1.setHeader("Cache-Control","max-age=5"); + + backendExpectsAnyRequest().andReturn(resp1); + + HttpRequest req2 = HttpTestUtils.makeDefaultRequest(); + req2.setHeader("Cache-Control", "max-stale=20, only-if-cached"); + + replayMocks(); + impl.execute(host, req1); + HttpResponse result = impl.execute(host, req2); + verifyMocks(); + + assertTrue(HttpTestUtils.semanticallyTransparent(resp1, result)); + } }