hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1421842 - in /httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/BasicHttpCache.java test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
Date Fri, 14 Dec 2012 13:24:11 GMT
Author: olegk
Date: Fri Dec 14 13:24:10 2012
New Revision: 1421842

URL: http://svn.apache.org/viewvc?rev=1421842&view=rev
Log:
Follow-up to HTTPCLIENT-1276
Contributed by Francois-Xavier Bonnet <francois-xavier.bonnet at centraliens.net>

Modified:
    httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
    httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java?rev=1421842&r1=1421841&r2=1421842&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/BasicHttpCache.java
Fri Dec 14 13:24:10 2012
@@ -196,7 +196,12 @@ class BasicHttpCache implements HttpCach
             src = entry;
         }
 
-        Resource resource = resourceFactory.copy(requestId, src.getResource());
+        Resource oldResource = entry.getResource();
+        Resource resource = null;
+        if (oldResource != null) {
+            resource = resourceFactory.copy(requestId, entry.getResource());
+            oldResource.dispose();
+        }
         Map<String,String> variantMap = new HashMap<String,String>(src.getVariantMap());
         variantMap.put(variantKey, variantCacheKey);
         return new HttpCacheEntry(

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java?rev=1421842&r1=1421841&r2=1421842&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
(original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClient.java
Fri Dec 14 13:24:10 2012
@@ -2090,7 +2090,52 @@ public class TestCachingHttpClient {
         assertEquals(HttpStatus.SC_NOT_MODIFIED, result2.getStatusLine().getStatusCode());
         assertEquals("etag", result2.getFirstHeader("Etag").getValue());
     }
-	
+
+    @Test
+    public void testNotModifiedResponseWithVaryUpdatesCacheEntryWhenNoEntity() throws Exception
{
+
+        Date now = new Date();
+
+        impl = new CachingHttpClient(mockBackend);
+
+        HttpRequest req1 = new HttpGet("http://foo.example.com/");
+        req1.addHeader("If-None-Match", "etag");
+
+        HttpRequest req2 = new HttpGet("http://foo.example.com/");
+        req2.addHeader("If-None-Match", "etag");
+
+        HttpResponse resp1 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_MODIFIED,
"Not modified");
+        resp1.setHeader("Date", DateUtils.formatDate(now));
+        resp1.setHeader("Cache-Control","max-age=0");
+        resp1.setHeader("Etag", "etag");
+        resp1.setHeader("Vary", "Accept-Encoding");
+
+        HttpResponse resp2 = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_MODIFIED,
"Not modified");
+        resp2.setHeader("Date", DateUtils.formatDate(now));
+        resp2.setHeader("Cache-Control","max-age=0");
+        resp1.setHeader("Etag", "etag");
+        resp1.setHeader("Vary", "Accept-Encoding");
+
+        expect(
+                mockBackend.execute(isA(HttpHost.class),
+                        isA(HttpRequest.class), (HttpContext)
+                        isNull())).andReturn(resp1);
+        expect(
+                mockBackend.execute(isA(HttpHost.class),
+                        isA(HttpRequest.class), (HttpContext)
+                        isNull())).andReturn(resp2);
+
+        replayMocks();
+        HttpResponse result1 = impl.execute(host, req1);
+        HttpResponse result2 = impl.execute(host, req2);
+        verifyMocks();
+
+        assertEquals(HttpStatus.SC_NOT_MODIFIED, result1.getStatusLine().getStatusCode());
+        assertEquals("etag", result1.getFirstHeader("Etag").getValue());
+        assertEquals(HttpStatus.SC_NOT_MODIFIED, result2.getStatusLine().getStatusCode());
+        assertEquals("etag", result2.getFirstHeader("Etag").getValue());
+    }
+
     private void getCacheEntryReturns(HttpCacheEntry result) throws IOException {
         expect(mockCache.getCacheEntry(host, request)).andReturn(result);
     }



Mime
View raw message