hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1044648 - in /httpcomponents/httpclient/trunk/httpclient-cache/src: main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java
Date Sat, 11 Dec 2010 13:12:06 GMT
Author: jonm
Date: Sat Dec 11 13:12:05 2010
New Revision: 1044648

URL: http://svn.apache.org/viewvc?rev=1044648&view=rev
Log:
HTTPCLIENT-1032: additional test coverage for CacheEntryUpdater. Also,
fixed small bug where Warning headers should not be removed from an
entry if the origin response is not used for updating cache entry
headers because it is post-dated.

Modified:
    httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java
    httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java?rev=1044648&r1=1044647&r2=1044648&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheEntryUpdater.java
Sat Dec 11 13:12:05 2010
@@ -98,17 +98,15 @@ class CacheEntryUpdater {
     }
 
     protected Header[] mergeHeaders(HttpCacheEntry entry, HttpResponse response) {
-        List<Header> cacheEntryHeaderList = new ArrayList<Header>(Arrays.asList(entry
-                .getAllHeaders()));
 
         if (entryAndResponseHaveDateHeader(entry, response)
                 && entryDateHeaderNewerThenResponse(entry, response)) {
-            // Don't merge Headers, keep the entries headers as they are newer.
-            removeCacheEntry1xxWarnings(cacheEntryHeaderList, entry);
-
-            return cacheEntryHeaderList.toArray(new Header[cacheEntryHeaderList.size()]);
+        	// Don't merge headers, keep the entry's headers as they are newer.
+        	return entry.getAllHeaders();
         }
 
+        List<Header> cacheEntryHeaderList = new ArrayList<Header>(Arrays.asList(entry
+        		.getAllHeaders()));
         removeCacheHeadersThatMatchResponse(cacheEntryHeaderList, response);
         removeCacheEntry1xxWarnings(cacheEntryHeaderList, entry);
         cacheEntryHeaderList.addAll(Arrays.asList(response.getAllHeaders()));

Modified: httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java?rev=1044648&r1=1044647&r2=1044648&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCacheEntryUpdater.java
Sat Dec 11 13:12:05 2010
@@ -30,21 +30,18 @@ import org.apache.http.Header;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
 import org.apache.http.HttpVersion;
-import org.apache.http.ProtocolVersion;
 import org.apache.http.client.cache.HttpCacheEntry;
 import org.apache.http.impl.cookie.DateUtils;
 import org.apache.http.message.BasicHeader;
 import org.apache.http.message.BasicHttpResponse;
-import org.apache.http.message.BasicStatusLine;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
 import java.io.IOException;
 import java.util.Date;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotSame;
+import static org.junit.Assert.*;
+import static org.apache.http.impl.cookie.DateUtils.formatDate;
 
 public class TestCacheEntryUpdater {
 
@@ -52,123 +49,194 @@ public class TestCacheEntryUpdater {
     private Date responseDate;
 
     private CacheEntryUpdater impl;
+    private HttpCacheEntry entry;
+    private Date now;
+    private Date oneSecondAgo;
+    private Date twoSecondsAgo;
+    private Date eightSecondsAgo;
+    private Date tenSecondsAgo;
+    private HttpResponse response;
 
     @Before
     public void setUp() throws Exception {
         requestDate = new Date(System.currentTimeMillis() - 1000);
         responseDate = new Date();
 
+        now = new Date();
+        oneSecondAgo = new Date(now.getTime() - 1000L);
+        twoSecondsAgo = new Date(now.getTime() - 2000L);
+        eightSecondsAgo = new Date(now.getTime() - 8000L);
+        tenSecondsAgo = new Date(now.getTime() - 10000L);
+
+        response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
+                HttpStatus.SC_NOT_MODIFIED, "Not Modified");
+
         impl = new CacheEntryUpdater();
     }
 
     @Test
-    public void testUpdateCacheEntryReturnsDifferentEntryInstance() throws IOException {
-
-        HttpCacheEntry entry =HttpTestUtils.makeCacheEntry();
-        BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_MODIFIED,
"");
-
-        HttpCacheEntry newEntry = impl.updateCacheEntry(null, entry, requestDate, responseDate,
response);
-
+    public void testUpdateCacheEntryReturnsDifferentEntryInstance()
+            throws IOException {
+        entry = HttpTestUtils.makeCacheEntry();
+        HttpCacheEntry newEntry = impl.updateCacheEntry(null, entry,
+                requestDate, responseDate, response);
         assertNotSame(newEntry, entry);
     }
 
     @Test
     public void testHeadersAreMergedCorrectly() throws IOException {
-
         Header[] headers = {
-                new BasicHeader("Date", DateUtils.formatDate(responseDate)),
+                new BasicHeader("Date", formatDate(responseDate)),
                 new BasicHeader("ETag", "\"etag\"")};
-
-        HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
-
-        HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion(
-                "http", 1, 1), HttpStatus.SC_NOT_MODIFIED, ""));
+        entry = HttpTestUtils.makeCacheEntry(headers);
         response.setHeaders(new Header[]{});
 
-        HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, cacheEntry, new Date(),
new Date(), response);
-
-        Assert.assertEquals(2, updatedEntry.getAllHeaders().length);
-
-        headersContain(updatedEntry.getAllHeaders(), "Date", DateUtils.formatDate(responseDate));
-        headersContain(updatedEntry.getAllHeaders(), "ETag", "\"etag\"");
+        HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
+                new Date(), new Date(), response);
 
+         
+        Header[] updatedHeaders = updatedEntry.getAllHeaders();
+        assertEquals(2, updatedHeaders.length);
+        headersContain(updatedHeaders, "Date", formatDate(responseDate));
+        headersContain(updatedHeaders, "ETag", "\"etag\"");
     }
 
     @Test
     public void testNewerHeadersReplaceExistingHeaders() throws IOException {
-
         Header[] headers = {
-                new BasicHeader("Date", DateUtils.formatDate(requestDate)),
-                new BasicHeader("Cache-Control", "private"), new BasicHeader("ETag", "\"etag\""),
-                new BasicHeader("Last-Modified", DateUtils.formatDate(requestDate)),
+                new BasicHeader("Date", formatDate(requestDate)),
+                new BasicHeader("Cache-Control", "private"),
+                new BasicHeader("ETag", "\"etag\""),
+                new BasicHeader("Last-Modified", formatDate(requestDate)),
                 new BasicHeader("Cache-Control", "max-age=0"),};
-        HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
+        entry = HttpTestUtils.makeCacheEntry(headers);
 
-        HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion(
-                "http", 1, 1), HttpStatus.SC_NOT_MODIFIED, ""));
-        response.setHeaders(new Header[]{
-                new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)),
-                new BasicHeader("Cache-Control", "public"),});
+        response.setHeaders(new Header[] {
+                new BasicHeader("Last-Modified", formatDate(responseDate)),
+                new BasicHeader("Cache-Control", "public")});
 
-        HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, cacheEntry, new Date(),
new Date(), response);
+        HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
+                new Date(), new Date(), response);
 
+        Header[] updatedHeaders = updatedEntry.getAllHeaders();
 
-        Assert.assertEquals(4, updatedEntry.getAllHeaders().length);
-
-        headersContain(updatedEntry.getAllHeaders(), "Date", DateUtils.formatDate(requestDate));
-        headersContain(updatedEntry.getAllHeaders(), "ETag", "\"etag\"");
-        headersContain(updatedEntry.getAllHeaders(), "Last-Modified", DateUtils
+        assertEquals(4, updatedHeaders.length);
+        headersContain(updatedHeaders, "Date", formatDate(requestDate));
+        headersContain(updatedHeaders, "ETag", "\"etag\"");
+        headersContain(updatedHeaders, "Last-Modified", DateUtils
                 .formatDate(responseDate));
-        headersContain(updatedEntry.getAllHeaders(), "Cache-Control", "public");
+        headersContain(updatedHeaders, "Cache-Control", "public");
     }
 
     @Test
     public void testNewHeadersAreAddedByMerge() throws IOException {
 
         Header[] headers = {
-                new BasicHeader("Date", DateUtils.formatDate(requestDate)),
+                new BasicHeader("Date", formatDate(requestDate)),
                 new BasicHeader("ETag", "\"etag\"")};
 
-        HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
-        HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion(
-                "http", 1, 1), HttpStatus.SC_NOT_MODIFIED, ""));
+        entry = HttpTestUtils.makeCacheEntry(headers);
         response.setHeaders(new Header[]{
-                new BasicHeader("Last-Modified", DateUtils.formatDate(responseDate)),
+                new BasicHeader("Last-Modified", formatDate(responseDate)),
                 new BasicHeader("Cache-Control", "public"),});
 
-        HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, cacheEntry, new Date(),
new Date(), response);
-
+        HttpCacheEntry updatedEntry = impl.updateCacheEntry(null, entry,
+                new Date(), new Date(), response);
 
-        Assert.assertEquals(4, updatedEntry.getAllHeaders().length);
-
-        headersContain(updatedEntry.getAllHeaders(), "Date", DateUtils.formatDate(requestDate));
-        headersContain(updatedEntry.getAllHeaders(), "ETag", "\"etag\"");
-        headersContain(updatedEntry.getAllHeaders(), "Last-Modified", DateUtils
-                .formatDate(responseDate));
-        headersContain(updatedEntry.getAllHeaders(), "Cache-Control", "public");
+        Header[] updatedHeaders = updatedEntry.getAllHeaders();
+        assertEquals(4, updatedHeaders.length);
 
+        headersContain(updatedHeaders, "Date", formatDate(requestDate));
+        headersContain(updatedHeaders, "ETag", "\"etag\"");
+        headersContain(updatedHeaders, "Last-Modified", formatDate(responseDate));
+        headersContain(updatedHeaders, "Cache-Control", "public");
     }
 
     @Test
-    public void testUpdatedEntryHasLatestRequestAndResponseDates() throws IOException {
-
-        Date now = new Date();
-
-        Date tenSecondsAgo = new Date(now.getTime() - 10000L);
-        Date eightSecondsAgo = new Date(now.getTime() - 8000L);
+    public void oldHeadersRetainedIfResponseOlderThanEntry()
+            throws Exception {
+        Header[] headers = {
+                new BasicHeader("Date", formatDate(oneSecondAgo)),
+                new BasicHeader("ETag", "\"new-etag\"")
+        };
+        entry = HttpTestUtils.makeCacheEntry(twoSecondsAgo, now, headers); 
+        response.setHeader("Date", formatDate(tenSecondsAgo));
+        response.setHeader("ETag", "\"old-etag\"");
+        HttpCacheEntry result = impl.updateCacheEntry("A", entry, new Date(),
+                new Date(), response);
+        assertEquals(2, result.getAllHeaders().length);
+        headersContain(result.getAllHeaders(), "Date", formatDate(oneSecondAgo));
+        headersContain(result.getAllHeaders(), "ETag", "\"new-etag\"");
+    }
 
-        Date twoSecondsAgo = new Date(now.getTime() - 2000L);
-        Date oneSecondAgo = new Date(now.getTime() - 1000L);
+    @Test
+    public void testUpdatedEntryHasLatestRequestAndResponseDates()
+            throws IOException {
+        entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
+        HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+                twoSecondsAgo, oneSecondAgo, response);
 
-        HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo);
+        assertEquals(twoSecondsAgo, updated.getRequestDate());
+        assertEquals(oneSecondAgo, updated.getResponseDate());
+    }
+    
+    @Test
+    public void entry1xxWarningsAreRemovedOnUpdate() throws Exception {
+        Header[] headers = {
+                new BasicHeader("Warning", "110 fred \"Response is stale\""),
+                new BasicHeader("ETag", "\"old\""),
+                new BasicHeader("Date", formatDate(eightSecondsAgo))
+        };
+        entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
+        response.setHeader("ETag", "\"new\"");
+        response.setHeader("Date", formatDate(twoSecondsAgo));
+        HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+                twoSecondsAgo, oneSecondAgo, response);
 
-        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_NOT_MODIFIED,
"");
+        assertEquals(0, updated.getHeaders("Warning").length);
+    }
+    
+    @Test
+    public void entryWithMalformedDateIsStillUpdated() throws Exception {
+        Header[] headers = {
+                new BasicHeader("ETag", "\"old\""),
+                new BasicHeader("Date", "bad-date")
+        };
+        entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
+        response.setHeader("ETag", "\"new\"");
+        response.setHeader("Date", formatDate(twoSecondsAgo));
+        HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+                twoSecondsAgo, oneSecondAgo, response);
 
-        HttpCacheEntry updated = impl.updateCacheEntry(null, entry, twoSecondsAgo, oneSecondAgo,
response);
+        assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
+    }
 
-        assertEquals(twoSecondsAgo, updated.getRequestDate());
-        assertEquals(oneSecondAgo, updated.getResponseDate());
+    @Test
+    public void entryIsStillUpdatedByResponseWithMalformedDate() throws Exception {
+        Header[] headers = {
+                new BasicHeader("ETag", "\"old\""),
+                new BasicHeader("Date", formatDate(tenSecondsAgo))
+        };
+        entry = HttpTestUtils.makeCacheEntry(tenSecondsAgo, eightSecondsAgo, headers);
+        response.setHeader("ETag", "\"new\"");
+        response.setHeader("Date", "bad-date");
+        HttpCacheEntry updated = impl.updateCacheEntry(null, entry,
+                twoSecondsAgo, oneSecondAgo, response);
 
+        assertEquals("\"new\"", updated.getFirstHeader("ETag").getValue());
+    }
+    
+    @Test
+    public void cannotUpdateFromANon304OriginResponse() throws Exception {
+        entry = HttpTestUtils.makeCacheEntry();
+        response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
+                HttpStatus.SC_OK, "OK");
+        try {
+            impl.updateCacheEntry("A", entry, new Date(), new Date(),
+                    response);
+            fail("should have thrown exception");
+        } catch (IllegalArgumentException expected) {
+        }
     }
 
     private void headersContain(Header[] headers, String name, String value) {
@@ -179,7 +247,7 @@ public class TestCacheEntryUpdater {
                 }
             }
         }
-        Assert.fail("Header [" + name + ": " + value + "] not found in headers.");
+        fail("Header [" + name + ": " + value + "] not found in headers.");
     }
 
 }



Mime
View raw message