From c429b34ca976f7e3b7d2987176d20a2c7d39c32d Mon Sep 17 00:00:00 2001 From: Jonathan Moore Date: Thu, 7 Jun 2012 19:43:22 +0000 Subject: [PATCH] HTTPCLIENT-1202: Allow for caching responses without bodies. git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1347764 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/http/client/cache/HttpCacheEntry.java | 3 --- .../apache/http/impl/client/cache/CacheEntity.java | 4 +++- .../http/client/cache/TestHttpCacheEntry.java | 10 ---------- .../http/impl/client/cache/DummyHttpClient.java | 12 ++++++++++++ .../impl/client/cache/TestCachingHttpClient.java | 13 +++++++++++++ 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java index 495660809..740e7fab4 100644 --- a/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java +++ b/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java @@ -97,9 +97,6 @@ public HttpCacheEntry( if (responseHeaders == null) { throw new IllegalArgumentException("Response headers may not be null"); } - if (resource == null) { - throw new IllegalArgumentException("Resource may not be null"); - } this.requestDate = requestDate; this.responseDate = responseDate; this.statusLine = statusLine; diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java index 538689add..b63e7d847 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java @@ -35,6 +35,7 @@ import org.apache.http.HttpEntity; import org.apache.http.annotation.Immutable; import org.apache.http.client.cache.HttpCacheEntry; +import org.apache.http.client.cache.Resource; import org.apache.http.protocol.HTTP; @Immutable @@ -66,7 +67,8 @@ public boolean isRepeatable() { } public long getContentLength() { - return this.cacheEntry.getResource().length(); + Resource resource = this.cacheEntry.getResource(); + return (resource != null) ? resource.length() : 0L; } public InputStream getContent() throws IOException { diff --git a/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java b/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java index 1d6dce344..edf359dc9 100644 --- a/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java +++ b/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java @@ -177,16 +177,6 @@ public void mustProvideResponseHeaders() { } } - @Test - public void mustProvideResource() { - try { - new HttpCacheEntry(new Date(), new Date(), statusLine, - new Header[]{}, null); - fail("Should have thrown exception"); - } catch (IllegalArgumentException expected) { - } - } - @Test public void canRetrieveOriginalStatusLine() { entry = new HttpCacheEntry(new Date(), new Date(), statusLine, diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java index 3a1496e71..b829c6bcc 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java @@ -50,6 +50,7 @@ public class DummyHttpClient implements HttpClient { private ClientConnectionManager connManager = new SingleClientConnManager(); private HttpRequest request; private HttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP",1,1), HttpStatus.SC_OK, "OK"); + private int executions = 0; public void setParams(HttpParams params) { this.params = params; @@ -78,24 +79,28 @@ public HttpRequest getCapturedRequest() { public HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException { this.request = request; + executions++; return response; } public HttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException { this.request = request; + executions++; return response; } public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException { this.request = request; + executions++; return response; } public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context) throws IOException, ClientProtocolException { this.request = request; + executions++; return response; } @@ -103,6 +108,7 @@ public T execute(HttpUriRequest request, ResponseHandler responseHandler) throws IOException, ClientProtocolException { this.request = request; + executions++; return responseHandler.handleResponse(response); } @@ -110,6 +116,7 @@ public T execute(HttpUriRequest request, ResponseHandler responseHandler, HttpContext context) throws IOException, ClientProtocolException { this.request = request; + executions++; return responseHandler.handleResponse(response); } @@ -117,6 +124,7 @@ public T execute(HttpHost target, HttpRequest request, ResponseHandler responseHandler) throws IOException, ClientProtocolException { this.request = request; + executions++; return responseHandler.handleResponse(response); } @@ -124,7 +132,11 @@ public T execute(HttpHost target, HttpRequest request, ResponseHandler responseHandler, HttpContext context) throws IOException, ClientProtocolException { this.request = request; + executions++; return responseHandler.handleResponse(response); } + public int getExecutions() { + return executions; + } } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java index 61eb28d43..91b42f8f5 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java @@ -2003,6 +2003,19 @@ public void testAllowsBackendToSetContextVariablesOnRevalidation() throws Except assertAllContextVariablesAreEqualTo(ctx, value); } + @Test + public void testCanCacheAResponseWithoutABody() throws Exception { + HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, "No Content"); + response.setHeader("Date", DateUtils.formatDate(new Date())); + response.setHeader("Cache-Control","max-age=300"); + DummyHttpClient backend = new DummyHttpClient(); + backend.setResponse(response); + impl = new CachingHttpClient(backend); + impl.execute(host, request); + impl.execute(host, request); + assertEquals(1, backend.getExecutions()); + } + private void getCacheEntryReturns(HttpCacheEntry result) throws IOException { expect(mockCache.getCacheEntry(host, request)).andReturn(result); }