hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1347764 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/client/cache/ main/java/org/apache/http/impl/client/cache/ test/java/org/apache/http/client/cache/ test/java/org/apache/http/impl/client/cache/
Date Thu, 07 Jun 2012 19:43:23 GMT
Author: jonm
Date: Thu Jun  7 19:43:22 2012
New Revision: 1347764

URL: http://svn.apache.org/viewvc?rev=1347764&view=rev
Log:
HTTPCLIENT-1202: Allow for caching responses without bodies.

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java?rev=1347764&r1=1347763&r2=1347764&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/client/cache/HttpCacheEntry.java
Thu Jun  7 19:43:22 2012
@@ -97,9 +97,6 @@ public class HttpCacheEntry implements S
         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;

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java?rev=1347764&r1=1347763&r2=1347764&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntity.java
Thu Jun  7 19:43:22 2012
@@ -35,6 +35,7 @@ import org.apache.http.Header;
 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 @@ class CacheEntity implements HttpEntity,
     }
 
     public long getContentLength() {
-        return this.cacheEntry.getResource().length();
+        Resource resource = this.cacheEntry.getResource();
+		return (resource != null) ? resource.length() : 0L;
     }
 
     public InputStream getContent() throws IOException {

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java?rev=1347764&r1=1347763&r2=1347764&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/client/cache/TestHttpCacheEntry.java
Thu Jun  7 19:43:22 2012
@@ -178,16 +178,6 @@ public class TestHttpCacheEntry {
     }
 
     @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,
                 new Header[]{}, mockResource);

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java?rev=1347764&r1=1347763&r2=1347764&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/DummyHttpClient.java
Thu Jun  7 19:43:22 2012
@@ -50,6 +50,7 @@ public class DummyHttpClient implements 
     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 class DummyHttpClient implements 
     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 class DummyHttpClient implements 
             ResponseHandler<? extends T> responseHandler) throws IOException,
             ClientProtocolException {
         this.request = request;
+        executions++;
         return responseHandler.handleResponse(response);
     }
 
@@ -110,6 +116,7 @@ public class DummyHttpClient implements 
             ResponseHandler<? extends T> responseHandler, HttpContext context)
             throws IOException, ClientProtocolException {
         this.request = request;
+        executions++;
         return responseHandler.handleResponse(response);
     }
 
@@ -117,6 +124,7 @@ public class DummyHttpClient implements 
             ResponseHandler<? extends T> responseHandler) throws IOException,
             ClientProtocolException {
         this.request = request;
+        executions++;
         return responseHandler.handleResponse(response);
     }
 
@@ -124,7 +132,11 @@ public class DummyHttpClient implements 
             ResponseHandler<? extends T> responseHandler, HttpContext context)
             throws IOException, ClientProtocolException {
         this.request = request;
+        executions++;
         return responseHandler.handleResponse(response);
     }
 
+    public int getExecutions() {
+    	return executions;
+    }
 }

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java?rev=1347764&r1=1347763&r2=1347764&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
Thu Jun  7 19:43:22 2012
@@ -2003,6 +2003,19 @@ public class TestCachingHttpClient {
 		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);
     }



Mime
View raw message