Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 73AB310B88 for ; Wed, 26 Feb 2014 16:52:05 +0000 (UTC) Received: (qmail 37628 invoked by uid 500); 26 Feb 2014 16:50:18 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 37520 invoked by uid 500); 26 Feb 2014 16:50:12 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 37133 invoked by uid 99); 26 Feb 2014 16:49:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Feb 2014 16:49:58 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Feb 2014 16:49:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 967EB2388C6D for ; Wed, 26 Feb 2014 16:49:09 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1572159 [12/45] - in /httpcomponents/site: ./ css/ httpcomponents-asyncclient-4.0.x/ httpcomponents-client-4.2.x/ httpcomponents-client-4.3.x/ httpcomponents-client-4.3.x/fluent-hc/ httpcomponents-client-4.3.x/fluent-hc/apidocs/ httpcompon... Date: Wed, 26 Feb 2014 16:48:15 -0000 To: commits@hc.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140226164909.967EB2388C6D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Modified: httpcomponents/site/httpcomponents-client-4.3.x/httpclient-cache/xref-test/org/apache/http/impl/client/cache/TestBasicHttpCache.html URL: http://svn.apache.org/viewvc/httpcomponents/site/httpcomponents-client-4.3.x/httpclient-cache/xref-test/org/apache/http/impl/client/cache/TestBasicHttpCache.html?rev=1572159&r1=1572158&r2=1572159&view=diff ============================================================================== --- httpcomponents/site/httpcomponents-client-4.3.x/httpclient-cache/xref-test/org/apache/http/impl/client/cache/TestBasicHttpCache.html (original) +++ httpcomponents/site/httpcomponents-client-4.3.x/httpclient-cache/xref-test/org/apache/http/impl/client/cache/TestBasicHttpCache.html Wed Feb 26 16:47:55 2014 @@ -72,526 +72,525 @@ 62 import org.apache.http.entity.ByteArrayEntity; 63 import org.apache.http.message.BasicHeader; 64 import org.apache.http.message.BasicHttpResponse; -65 import org.apache.http.util.EntityUtils; -66 import org.junit.Assert; -67 import org.junit.Before; -68 import org.junit.Test; -69 -70 public class TestBasicHttpCache { -71 -72 private BasicHttpCache impl; -73 private SimpleHttpCacheStorage backing; -74 -75 @Before -76 public void setUp() throws Exception { -77 backing = new SimpleHttpCacheStorage(); -78 impl = new BasicHttpCache(new HeapResourceFactory(), backing, CacheConfig.DEFAULT); -79 } -80 -81 @Test -82 public void testDoNotFlushCacheEntriesOnGet() throws Exception { -83 final HttpHost host = new HttpHost("foo.example.com"); -84 final HttpRequest req = new HttpGet("/bar"); -85 final String key = (new CacheKeyGenerator()).getURI(host, req); -86 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -87 -88 backing.map.put(key, entry); -89 -90 impl.flushCacheEntriesFor(host, req); -91 -92 assertEquals(entry, backing.map.get(key)); -93 } -94 -95 @Test -96 public void testDoNotFlushCacheEntriesOnHead() throws Exception { -97 final HttpHost host = new HttpHost("foo.example.com"); -98 final HttpRequest req = new HttpHead("/bar"); -99 final String key = (new CacheKeyGenerator()).getURI(host, req); -100 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -101 -102 backing.map.put(key, entry); -103 -104 impl.flushCacheEntriesFor(host, req); -105 -106 assertEquals(entry, backing.map.get(key)); -107 } -108 -109 @Test -110 public void testDoNotFlushCacheEntriesOnOptions() throws Exception { -111 final HttpHost host = new HttpHost("foo.example.com"); -112 final HttpRequest req = new HttpOptions("/bar"); -113 final String key = (new CacheKeyGenerator()).getURI(host, req); -114 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -115 -116 backing.map.put(key, entry); -117 -118 impl.flushCacheEntriesFor(host, req); -119 -120 assertEquals(entry, backing.map.get(key)); -121 } -122 -123 @Test -124 public void testDoNotFlushCacheEntriesOnTrace() throws Exception { -125 final HttpHost host = new HttpHost("foo.example.com"); -126 final HttpRequest req = new HttpTrace("/bar"); -127 final String key = (new CacheKeyGenerator()).getURI(host, req); -128 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -129 -130 backing.map.put(key, entry); -131 -132 impl.flushCacheEntriesFor(host, req); -133 -134 assertEquals(entry, backing.map.get(key)); -135 } -136 -137 @Test -138 public void testFlushContentLocationEntryIfUnSafeRequest() -139 throws Exception { -140 final HttpHost host = new HttpHost("foo.example.com"); -141 final HttpRequest req = new HttpPost("/foo"); -142 final HttpResponse resp = HttpTestUtils.make200Response(); -143 resp.setHeader("Content-Location", "/bar"); -144 resp.setHeader(HeaderConstants.ETAG, "\"etag\""); -145 final String key = (new CacheKeyGenerator()).getURI(host, new HttpGet("/bar")); -146 -147 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] { -148 new BasicHeader("Date", DateUtils.formatDate(new Date())), -149 new BasicHeader("ETag", "\"old-etag\"") -150 }); -151 -152 backing.map.put(key, entry); -153 -154 impl.flushInvalidatedCacheEntriesFor(host, req, resp); -155 -156 assertNull(backing.map.get(key)); -157 } -158 -159 @Test -160 public void testDoNotFlushContentLocationEntryIfSafeRequest() -161 throws Exception { -162 final HttpHost host = new HttpHost("foo.example.com"); -163 final HttpRequest req = new HttpGet("/foo"); -164 final HttpResponse resp = HttpTestUtils.make200Response(); -165 resp.setHeader("Content-Location", "/bar"); -166 final String key = (new CacheKeyGenerator()).getURI(host, new HttpGet("/bar")); -167 -168 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] { -169 new BasicHeader("Date", DateUtils.formatDate(new Date())), -170 new BasicHeader("ETag", "\"old-etag\"") -171 }); -172 -173 backing.map.put(key, entry); -174 -175 impl.flushInvalidatedCacheEntriesFor(host, req, resp); -176 -177 assertEquals(entry, backing.map.get(key)); -178 } -179 -180 @Test -181 public void testCanFlushCacheEntriesAtUri() throws Exception { -182 final HttpHost host = new HttpHost("foo.example.com"); -183 final HttpRequest req = new HttpDelete("/bar"); -184 final String key = (new CacheKeyGenerator()).getURI(host, req); -185 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -186 -187 backing.map.put(key, entry); -188 -189 impl.flushCacheEntriesFor(host, req); -190 -191 assertNull(backing.map.get(key)); -192 } -193 -194 @Test -195 public void testRecognizesComplete200Response() -196 throws Exception { -197 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -198 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -199 resp.setEntity(new ByteArrayEntity(bytes)); -200 resp.setHeader("Content-Length","128"); -201 final Resource resource = new HeapResource(bytes); -202 -203 assertFalse(impl.isIncompleteResponse(resp, resource)); -204 } -205 -206 @Test -207 public void testRecognizesComplete206Response() -208 throws Exception { -209 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_PARTIAL_CONTENT, "Partial Content"); -210 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -211 final Resource resource = new HeapResource(bytes); -212 resp.setEntity(new ByteArrayEntity(bytes)); -213 resp.setHeader("Content-Length","128"); -214 resp.setHeader("Content-Range","bytes 0-127/255"); -215 -216 assertFalse(impl.isIncompleteResponse(resp, resource)); -217 } -218 -219 @Test -220 public void testRecognizesIncomplete200Response() -221 throws Exception { -222 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -223 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -224 final Resource resource = new HeapResource(bytes); -225 resp.setEntity(new ByteArrayEntity(bytes)); -226 resp.setHeader("Content-Length","256"); -227 -228 assertTrue(impl.isIncompleteResponse(resp, resource)); -229 } -230 -231 @Test -232 public void testIgnoresIncompleteNon200Or206Responses() -233 throws Exception { -234 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_FORBIDDEN, "Forbidden"); -235 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -236 final Resource resource = new HeapResource(bytes); -237 resp.setEntity(new ByteArrayEntity(bytes)); -238 resp.setHeader("Content-Length","256"); -239 -240 assertFalse(impl.isIncompleteResponse(resp, resource)); -241 } -242 -243 @Test -244 public void testResponsesWithoutExplicitContentLengthAreComplete() -245 throws Exception { -246 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -247 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -248 final Resource resource = new HeapResource(bytes); -249 resp.setEntity(new ByteArrayEntity(bytes)); -250 -251 assertFalse(impl.isIncompleteResponse(resp, resource)); -252 } -253 -254 @Test -255 public void testResponsesWithUnparseableContentLengthHeaderAreComplete() -256 throws Exception { -257 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -258 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -259 final Resource resource = new HeapResource(bytes); -260 resp.setHeader("Content-Length","foo"); -261 resp.setEntity(new ByteArrayEntity(bytes)); -262 -263 assertFalse(impl.isIncompleteResponse(resp, resource)); -264 } -265 -266 @Test -267 public void testIncompleteResponseErrorProvidesPlainTextErrorMessage() -268 throws Exception { -269 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -270 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -271 final Resource resource = new HeapResource(bytes); -272 resp.setEntity(new ByteArrayEntity(bytes)); -273 resp.setHeader("Content-Length","256"); -274 -275 final HttpResponse result = impl.generateIncompleteResponseError(resp, resource); -276 final Header ctype = result.getFirstHeader("Content-Type"); -277 assertEquals("text/plain;charset=UTF-8", ctype.getValue()); -278 } -279 -280 @Test -281 public void testIncompleteResponseErrorProvidesNonEmptyErrorMessage() -282 throws Exception { -283 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -284 final byte[] bytes = HttpTestUtils.getRandomBytes(128); -285 final Resource resource = new HeapResource(bytes); -286 resp.setEntity(new ByteArrayEntity(bytes)); -287 resp.setHeader("Content-Length","256"); -288 -289 final HttpResponse result = impl.generateIncompleteResponseError(resp, resource); -290 final int clen = Integer.parseInt(result.getFirstHeader("Content-Length").getValue()); -291 assertTrue(clen > 0); -292 final HttpEntity body = result.getEntity(); -293 if (body.getContentLength() < 0) { -294 final InputStream is = body.getContent(); -295 int bytes_read = 0; -296 while((is.read()) != -1) { -297 bytes_read++; -298 } -299 is.close(); -300 assertEquals(clen, bytes_read); -301 } else { -302 assertTrue(body.getContentLength() == clen); -303 } -304 } -305 -306 @Test -307 public void testCacheUpdateAddsVariantURIToParentEntry() throws Exception { -308 final String parentCacheKey = "parentCacheKey"; -309 final String variantCacheKey = "variantCacheKey"; -310 final String existingVariantKey = "existingVariantKey"; -311 final String newVariantCacheKey = "newVariantCacheKey"; -312 final String newVariantKey = "newVariantKey"; -313 final Map<String,String> existingVariants = new HashMap<String,String>(); -314 existingVariants.put(existingVariantKey, variantCacheKey); -315 final HttpCacheEntry parent = HttpTestUtils.makeCacheEntry(existingVariants); -316 final HttpCacheEntry variant = HttpTestUtils.makeCacheEntry(); -317 -318 final HttpCacheEntry result = impl.doGetUpdatedParentEntry(parentCacheKey, parent, variant, newVariantKey, newVariantCacheKey); -319 final Map<String,String> resultMap = result.getVariantMap(); -320 assertEquals(2, resultMap.size()); -321 assertEquals(variantCacheKey, resultMap.get(existingVariantKey)); -322 assertEquals(newVariantCacheKey, resultMap.get(newVariantKey)); -323 } -324 -325 @Test -326 public void testStoreInCachePutsNonVariantEntryInPlace() throws Exception { -327 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -328 assertFalse(entry.hasVariants()); -329 final HttpHost host = new HttpHost("foo.example.com"); -330 final HttpRequest req = new HttpGet("http://foo.example.com/bar"); -331 final String key = (new CacheKeyGenerator()).getURI(host, req); -332 -333 impl.storeInCache(host, req, entry); -334 assertSame(entry, backing.map.get(key)); -335 } -336 -337 @Test -338 public void testTooLargeResponsesAreNotCached() throws Exception { -339 final HttpHost host = new HttpHost("foo.example.com"); -340 final HttpRequest request = new HttpGet("http://foo.example.com/bar"); -341 -342 final Date now = new Date(); -343 final Date requestSent = new Date(now.getTime() - 3 * 1000L); -344 final Date responseGenerated = new Date(now.getTime() - 2 * 1000L); -345 final Date responseReceived = new Date(now.getTime() - 1 * 1000L); -346 -347 final HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -348 originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES + 1)); -349 originResponse.setHeader("Cache-Control","public, max-age=3600"); -350 originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated)); -351 originResponse.setHeader("ETag", "\"etag\""); -352 -353 final HttpResponse result = impl.cacheAndReturnResponse(host, request, originResponse, requestSent, responseReceived); -354 assertEquals(0, backing.map.size()); -355 assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result)); -356 } +65 import org.junit.Assert; +66 import org.junit.Before; +67 import org.junit.Test; +68 +69 public class TestBasicHttpCache { +70 +71 private BasicHttpCache impl; +72 private SimpleHttpCacheStorage backing; +73 +74 @Before +75 public void setUp() throws Exception { +76 backing = new SimpleHttpCacheStorage(); +77 impl = new BasicHttpCache(new HeapResourceFactory(), backing, CacheConfig.DEFAULT); +78 } +79 +80 @Test +81 public void testDoNotFlushCacheEntriesOnGet() throws Exception { +82 final HttpHost host = new HttpHost("foo.example.com"); +83 final HttpRequest req = new HttpGet("/bar"); +84 final String key = (new CacheKeyGenerator()).getURI(host, req); +85 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); +86 +87 backing.map.put(key, entry); +88 +89 impl.flushCacheEntriesFor(host, req); +90 +91 assertEquals(entry, backing.map.get(key)); +92 } +93 +94 @Test +95 public void testDoNotFlushCacheEntriesOnHead() throws Exception { +96 final HttpHost host = new HttpHost("foo.example.com"); +97 final HttpRequest req = new HttpHead("/bar"); +98 final String key = (new CacheKeyGenerator()).getURI(host, req); +99 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); +100 +101 backing.map.put(key, entry); +102 +103 impl.flushCacheEntriesFor(host, req); +104 +105 assertEquals(entry, backing.map.get(key)); +106 } +107 +108 @Test +109 public void testDoNotFlushCacheEntriesOnOptions() throws Exception { +110 final HttpHost host = new HttpHost("foo.example.com"); +111 final HttpRequest req = new HttpOptions("/bar"); +112 final String key = (new CacheKeyGenerator()).getURI(host, req); +113 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); +114 +115 backing.map.put(key, entry); +116 +117 impl.flushCacheEntriesFor(host, req); +118 +119 assertEquals(entry, backing.map.get(key)); +120 } +121 +122 @Test +123 public void testDoNotFlushCacheEntriesOnTrace() throws Exception { +124 final HttpHost host = new HttpHost("foo.example.com"); +125 final HttpRequest req = new HttpTrace("/bar"); +126 final String key = (new CacheKeyGenerator()).getURI(host, req); +127 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); +128 +129 backing.map.put(key, entry); +130 +131 impl.flushCacheEntriesFor(host, req); +132 +133 assertEquals(entry, backing.map.get(key)); +134 } +135 +136 @Test +137 public void testFlushContentLocationEntryIfUnSafeRequest() +138 throws Exception { +139 final HttpHost host = new HttpHost("foo.example.com"); +140 final HttpRequest req = new HttpPost("/foo"); +141 final HttpResponse resp = HttpTestUtils.make200Response(); +142 resp.setHeader("Content-Location", "/bar"); +143 resp.setHeader(HeaderConstants.ETAG, "\"etag\""); +144 final String key = (new CacheKeyGenerator()).getURI(host, new HttpGet("/bar")); +145 +146 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] { +147 new BasicHeader("Date", DateUtils.formatDate(new Date())), +148 new BasicHeader("ETag", "\"old-etag\"") +149 }); +150 +151 backing.map.put(key, entry); +152 +153 impl.flushInvalidatedCacheEntriesFor(host, req, resp); +154 +155 assertNull(backing.map.get(key)); +156 } +157 +158 @Test +159 public void testDoNotFlushContentLocationEntryIfSafeRequest() +160 throws Exception { +161 final HttpHost host = new HttpHost("foo.example.com"); +162 final HttpRequest req = new HttpGet("/foo"); +163 final HttpResponse resp = HttpTestUtils.make200Response(); +164 resp.setHeader("Content-Location", "/bar"); +165 final String key = (new CacheKeyGenerator()).getURI(host, new HttpGet("/bar")); +166 +167 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(new Header[] { +168 new BasicHeader("Date", DateUtils.formatDate(new Date())), +169 new BasicHeader("ETag", "\"old-etag\"") +170 }); +171 +172 backing.map.put(key, entry); +173 +174 impl.flushInvalidatedCacheEntriesFor(host, req, resp); +175 +176 assertEquals(entry, backing.map.get(key)); +177 } +178 +179 @Test +180 public void testCanFlushCacheEntriesAtUri() throws Exception { +181 final HttpHost host = new HttpHost("foo.example.com"); +182 final HttpRequest req = new HttpDelete("/bar"); +183 final String key = (new CacheKeyGenerator()).getURI(host, req); +184 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); +185 +186 backing.map.put(key, entry); +187 +188 impl.flushCacheEntriesFor(host, req); +189 +190 assertNull(backing.map.get(key)); +191 } +192 +193 @Test +194 public void testRecognizesComplete200Response() +195 throws Exception { +196 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +197 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +198 resp.setEntity(new ByteArrayEntity(bytes)); +199 resp.setHeader("Content-Length","128"); +200 final Resource resource = new HeapResource(bytes); +201 +202 assertFalse(impl.isIncompleteResponse(resp, resource)); +203 } +204 +205 @Test +206 public void testRecognizesComplete206Response() +207 throws Exception { +208 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_PARTIAL_CONTENT, "Partial Content"); +209 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +210 final Resource resource = new HeapResource(bytes); +211 resp.setEntity(new ByteArrayEntity(bytes)); +212 resp.setHeader("Content-Length","128"); +213 resp.setHeader("Content-Range","bytes 0-127/255"); +214 +215 assertFalse(impl.isIncompleteResponse(resp, resource)); +216 } +217 +218 @Test +219 public void testRecognizesIncomplete200Response() +220 throws Exception { +221 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +222 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +223 final Resource resource = new HeapResource(bytes); +224 resp.setEntity(new ByteArrayEntity(bytes)); +225 resp.setHeader("Content-Length","256"); +226 +227 assertTrue(impl.isIncompleteResponse(resp, resource)); +228 } +229 +230 @Test +231 public void testIgnoresIncompleteNon200Or206Responses() +232 throws Exception { +233 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_FORBIDDEN, "Forbidden"); +234 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +235 final Resource resource = new HeapResource(bytes); +236 resp.setEntity(new ByteArrayEntity(bytes)); +237 resp.setHeader("Content-Length","256"); +238 +239 assertFalse(impl.isIncompleteResponse(resp, resource)); +240 } +241 +242 @Test +243 public void testResponsesWithoutExplicitContentLengthAreComplete() +244 throws Exception { +245 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +246 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +247 final Resource resource = new HeapResource(bytes); +248 resp.setEntity(new ByteArrayEntity(bytes)); +249 +250 assertFalse(impl.isIncompleteResponse(resp, resource)); +251 } +252 +253 @Test +254 public void testResponsesWithUnparseableContentLengthHeaderAreComplete() +255 throws Exception { +256 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +257 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +258 final Resource resource = new HeapResource(bytes); +259 resp.setHeader("Content-Length","foo"); +260 resp.setEntity(new ByteArrayEntity(bytes)); +261 +262 assertFalse(impl.isIncompleteResponse(resp, resource)); +263 } +264 +265 @Test +266 public void testIncompleteResponseErrorProvidesPlainTextErrorMessage() +267 throws Exception { +268 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +269 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +270 final Resource resource = new HeapResource(bytes); +271 resp.setEntity(new ByteArrayEntity(bytes)); +272 resp.setHeader("Content-Length","256"); +273 +274 final HttpResponse result = impl.generateIncompleteResponseError(resp, resource); +275 final Header ctype = result.getFirstHeader("Content-Type"); +276 assertEquals("text/plain;charset=UTF-8", ctype.getValue()); +277 } +278 +279 @Test +280 public void testIncompleteResponseErrorProvidesNonEmptyErrorMessage() +281 throws Exception { +282 final HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +283 final byte[] bytes = HttpTestUtils.getRandomBytes(128); +284 final Resource resource = new HeapResource(bytes); +285 resp.setEntity(new ByteArrayEntity(bytes)); +286 resp.setHeader("Content-Length","256"); +287 +288 final HttpResponse result = impl.generateIncompleteResponseError(resp, resource); +289 final int clen = Integer.parseInt(result.getFirstHeader("Content-Length").getValue()); +290 assertTrue(clen > 0); +291 final HttpEntity body = result.getEntity(); +292 if (body.getContentLength() < 0) { +293 final InputStream is = body.getContent(); +294 int bytes_read = 0; +295 while((is.read()) != -1) { +296 bytes_read++; +297 } +298 is.close(); +299 assertEquals(clen, bytes_read); +300 } else { +301 assertTrue(body.getContentLength() == clen); +302 } +303 } +304 +305 @Test +306 public void testCacheUpdateAddsVariantURIToParentEntry() throws Exception { +307 final String parentCacheKey = "parentCacheKey"; +308 final String variantCacheKey = "variantCacheKey"; +309 final String existingVariantKey = "existingVariantKey"; +310 final String newVariantCacheKey = "newVariantCacheKey"; +311 final String newVariantKey = "newVariantKey"; +312 final Map<String,String> existingVariants = new HashMap<String,String>(); +313 existingVariants.put(existingVariantKey, variantCacheKey); +314 final HttpCacheEntry parent = HttpTestUtils.makeCacheEntry(existingVariants); +315 final HttpCacheEntry variant = HttpTestUtils.makeCacheEntry(); +316 +317 final HttpCacheEntry result = impl.doGetUpdatedParentEntry(parentCacheKey, parent, variant, newVariantKey, newVariantCacheKey); +318 final Map<String,String> resultMap = result.getVariantMap(); +319 assertEquals(2, resultMap.size()); +320 assertEquals(variantCacheKey, resultMap.get(existingVariantKey)); +321 assertEquals(newVariantCacheKey, resultMap.get(newVariantKey)); +322 } +323 +324 @Test +325 public void testStoreInCachePutsNonVariantEntryInPlace() throws Exception { +326 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); +327 assertFalse(entry.hasVariants()); +328 final HttpHost host = new HttpHost("foo.example.com"); +329 final HttpRequest req = new HttpGet("http://foo.example.com/bar"); +330 final String key = (new CacheKeyGenerator()).getURI(host, req); +331 +332 impl.storeInCache(host, req, entry); +333 assertSame(entry, backing.map.get(key)); +334 } +335 +336 @Test +337 public void testTooLargeResponsesAreNotCached() throws Exception { +338 final HttpHost host = new HttpHost("foo.example.com"); +339 final HttpRequest request = new HttpGet("http://foo.example.com/bar"); +340 +341 final Date now = new Date(); +342 final Date requestSent = new Date(now.getTime() - 3 * 1000L); +343 final Date responseGenerated = new Date(now.getTime() - 2 * 1000L); +344 final Date responseReceived = new Date(now.getTime() - 1 * 1000L); +345 +346 final HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); +347 originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES + 1)); +348 originResponse.setHeader("Cache-Control","public, max-age=3600"); +349 originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated)); +350 originResponse.setHeader("ETag", "\"etag\""); +351 +352 final HttpResponse result = impl.cacheAndReturnResponse(host, request, originResponse, requestSent, responseReceived); +353 assertEquals(0, backing.map.size()); +354 assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result)); +355 } +356 357 -358 -359 @Test -360 public void testSmallEnoughResponsesAreCached() throws Exception { -361 final HttpHost host = new HttpHost("foo.example.com"); -362 final HttpRequest request = new HttpGet("http://foo.example.com/bar"); -363 -364 final Date now = new Date(); -365 final Date requestSent = new Date(now.getTime() - 3 * 1000L); -366 final Date responseGenerated = new Date(now.getTime() - 2 * 1000L); -367 final Date responseReceived = new Date(now.getTime() - 1 * 1000L); -368 -369 final HttpResponse originResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -370 originResponse.setEntity(HttpTestUtils.makeBody(CacheConfig.DEFAULT_MAX_OBJECT_SIZE_BYTES - 1)); -371 originResponse.setHeader("Cache-Control","public, max-age=3600"); -372 originResponse.setHeader("Date", DateUtils.formatDate(responseGenerated)); -373 originResponse.setHeader("ETag", "\"etag\""); -374 -375 final HttpResponse result = impl.cacheAndReturnResponse(host, request, originResponse, requestSent, responseReceived); -376 assertEquals(1, backing.map.size()); -377 assertTrue(backing.map.containsKey((new CacheKeyGenerator()).getURI(host, request))); -378 assertTrue(HttpTestUtils.semanticallyTransparent(originResponse, result)); -379 } -380 -381 @Test -382 public void testGetCacheEntryReturnsNullOnCacheMiss() throws Exception { -383 final HttpHost host = new HttpHost("foo.example.com"); -384 final HttpRequest request = new HttpGet("http://foo.example.com/bar"); -385 final HttpCacheEntry result = impl.getCacheEntry(host, request); -386 Assert.assertNull(result); -387 } -388 -389 @Test -390 public void testGetCacheEntryFetchesFromCacheOnCacheHitIfNoVariants() throws Exception { -391 final HttpCacheEntry entry = HttpTestUtils.makeCacheEntry(); -392 assertFalse(entry.hasVariants()); -393 final HttpHost host = new HttpHost("foo.example.com"); -394 final HttpRequest request = new HttpGet("http://foo.example.com/bar"); -395 -396 final String key = (new CacheKeyGenerator()).getURI(host, request); -397 -398 backing.map.put(key,entry); -399 -400 final HttpCacheEntry result = impl.getCacheEntry(host, request); -401 Assert.assertSame(entry, result); -402 } -403 -404 @Test -405 public void testGetCacheEntryReturnsNullIfNoVariantInCache() throws Exception { -406 final HttpHost host = new HttpHost("foo.example.com"); -407 final HttpRequest request = new HttpGet("http://foo.example.com/bar"); -408 -409 final HttpRequest origRequest = new HttpGet("http://foo.example.com/bar"); -410 origRequest.setHeader("Accept-Encoding","gzip"); -411 -412 final HttpResponse origResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); -413 origResponse.setEntity(HttpTestUtils.makeBody(128)); -414 origResponse.setHeader("Date", DateUtils.formatDate(new Date())); -415 origResponse.setHeader("Cache-Control", "max-age=3600, public"); -416 origResponse.setHeader("ETag", "\"etag\""); -417 origResponse.setHeader("Vary", "Accept-Encoding"); -418 origResponse.setHeader("Content-Encoding","gzip"); -419 [... 395 lines stripped ...]