private void expectGetAndSetCacheTtl(Uri url, Integer cacheTtl) throws Exception { HttpRequest req = new HttpRequest(url); HttpResponse resp = new HttpResponseBuilder().setCacheTtl(cacheTtl).create(); expect(pipeline.execute(req)).andReturn(resp); }
private void expectGetAndSetCacheTtl(Uri url, Integer cacheTtl) throws Exception { HttpRequest req = new HttpRequest(url); HttpResponse resp = new HttpResponseBuilder().setCacheTtl(cacheTtl).create(); expect(pipeline.execute(req)).andReturn(resp); }
public boolean addResponse(HttpRequest request, HttpResponse response) { if (isCacheable(request, response)) { // Both are cacheable. Check for forced cache TTL overrides. HttpResponseBuilder responseBuilder = new HttpResponseBuilder(response); int forcedTtl = request.getCacheTtl(); if (forcedTtl != -1) { responseBuilder.setCacheTtl(forcedTtl); } response = responseBuilder.create(); String keyString = createKey(request); addResponseImpl(keyString, response); return true; } return false; }
@Test public void setCacheTtl() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .addHeader("Expires", "some time stamp normally goes here") .addHeader("Cache-Control", "no-cache") .setCacheTtl(100); Multimap<String, String> headers = builder.getHeaders(); assertFalse("Expires header not removed.", headers.containsKey("Expires")); assertFalse("Pragma header not removed", headers.containsKey("Pragma")); assertEquals("public,max-age=100", headers.get("Cache-Control").iterator().next()); }
@Test public void setCacheTtl() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .addHeader("Expires", "some time stamp normally goes here") .addHeader("Cache-Control", "no-cache") .setCacheTtl(100); Multimap<String, String> headers = builder.getHeaders(); assertFalse("Expires header not removed.", headers.containsKey("Expires")); assertFalse("Pragma header not removed", headers.containsKey("Pragma")); assertEquals("public,max-age=100", headers.get("Cache-Control").iterator().next()); }
@Test public void setCacheTtl() { HttpResponseBuilder builder = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .addHeader("Expires", "some time stamp normally goes here") .addHeader("Cache-Control", "no-cache") .setCacheTtl(100); Multimap<String, String> headers = builder.getHeaders(); assertFalse("Expires header not removed.", headers.containsKey("Expires")); assertFalse("Pragma header not removed", headers.containsKey("Pragma")); assertEquals("public,max-age=100", headers.get("Cache-Control").iterator().next()); }
@Test public void authTypeNoneStaleConditionalGetNoEtag() throws Exception { // Cached response is stale and has no Etag header on it. Test that an // If-None-Match header is not issued. HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder() .setCacheTtl(-1) .create(); cache.data.put(DEFAULT_URI, cached); fetcher.response = HttpResponse.error(); // Really don't care what this is. pipeline.execute(request); assertEquals(null, fetcher.request.getHeader(HttpHeaders.IF_NONE_MATCH)); }
@Test public void authTypeNoneStaleConditionalGetNoLastModified() throws Exception { // Cached response is stale and has no Last-Modified header on it. Test that an // If-Modified-Since header is not issued. HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder() .setCacheTtl(-1) .create(); cache.data.put(DEFAULT_URI, cached); fetcher.response = HttpResponse.error(); // Really don't care what this is. pipeline.execute(request); assertEquals(null, fetcher.request.getHeader(HttpHeaders.IF_MODIFIED_SINCE)); }
@Test public void authTypeNoneStaleConditionalGetNoLastModified() throws Exception { // Cached response is stale and has no Last-Modified header on it. Test that an // If-Modified-Since header is not issued. HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder() .setCacheTtl(-1) .create(); cache.data.put(DEFAULT_URI, cached); fetcher.response = HttpResponse.error(); // Really don't care what this is. pipeline.execute(request); assertEquals(null, fetcher.request.getHeader(HttpHeaders.IF_MODIFIED_SINCE)); }
@Test public void authTypeNoneStaleConditionalGetNoEtag() throws Exception { // Cached response is stale and has no Etag header on it. Test that an // If-None-Match header is not issued. HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder() .setCacheTtl(-1) .create(); cache.data.put(DEFAULT_URI, cached); fetcher.response = HttpResponse.error(); // Really don't care what this is. pipeline.execute(request); assertEquals(null, fetcher.request.getHeader(HttpHeaders.IF_NONE_MATCH)); }
@Test public void authTypeNoneWasCachedErrorStale() throws Exception { HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder().setCacheTtl(-1).setHttpStatusCode(401).create(); cache.data.put(DEFAULT_URI, cached); HttpResponse fetched = HttpResponse.error(); fetcher.response = fetched; HttpResponse response = pipeline.execute(request); assertEquals(fetched, response); // 500 served because cached is an error (401) assertEquals(request, fetcher.request); assertEquals(fetched, cache.data.get(DEFAULT_URI)); assertEquals(1, cache.readCount); assertEquals(1, cache.writeCount); assertEquals(1, fetcher.fetchCount); }
@Test public void authTypeNoneWasCachedErrorStale() throws Exception { HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder().setCacheTtl(-1).setHttpStatusCode(401).create(); cache.data.put(DEFAULT_URI, cached); HttpResponse fetched = HttpResponse.error(); fetcher.response = fetched; HttpResponse response = pipeline.execute(request); assertEquals(fetched, response); // 500 served because cached is an error (401) assertEquals(request, fetcher.request); assertEquals(fetched, cache.data.get(DEFAULT_URI)); assertEquals(1, cache.readCount); assertEquals(1, cache.writeCount); assertEquals(1, fetcher.fetchCount); }
@Test public void authTypeNoneStaleCachedServed() throws Exception { HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder().setCacheTtl(-1).create(); cache.data.put(DEFAULT_URI, cached); fetcher.response = HttpResponse.error(); HttpResponse response = pipeline.execute(request); assertEquals(cached, response); // cached item is served instead of 500 assertEquals(request, fetcher.request); assertEquals(1, cache.readCount); assertEquals(0, cache.writeCount); assertEquals(1, fetcher.fetchCount); }
@Test public void testStaleImgWithNegativeTtlReservedAndFetchTriggered() throws Exception { cache.addResponse(new HttpRequest(Uri.parse(IMG_URL)), new HttpResponseBuilder().setResponseString("test") .setCacheTtl(-1).create()); checkVisitBypassedAndFetchTriggered("img", IMG_URL, false, true); }
@Test public void testStaleImgWithNegativeTtlReservedAndFetchTriggered() throws Exception { cache.addResponse(new HttpRequest(Uri.parse(IMG_URL)), new HttpResponseBuilder().setResponseString("test") .setCacheTtl(-1).create()); checkVisitBypassedAndFetchTriggered("img", IMG_URL, false, true); }
@Test public void testFixedDateOk() throws Exception { int time = roundToSeconds(HttpResponseTest.timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader( "Date", DateUtil.formatRfc1123Date(1000L * time + 1000 - DefaultRequestPipeline.DEFAULT_DRIFT_LIMIT_MS)).setCacheTtl(100) .create(); HttpResponse newResponse = DefaultRequestPipeline.maybeFixDriftTime(response); assertSame(response, newResponse); }
@Test public void testFixedDateOk() throws Exception { int time = roundToSeconds(HttpResponseTest.timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader( "Date", DateUtil.formatRfc1123Date(1000L * time + 1000 - DefaultRequestPipeline.DEFAULT_DRIFT_LIMIT_MS)).setCacheTtl(100) .create(); HttpResponse newResponse = DefaultRequestPipeline.maybeFixDriftTime(response); assertSame(response, newResponse); }
@Test public void authTypeNoneStaleCachedServed() throws Exception { HttpRequest request = new HttpRequest(DEFAULT_URI).setAuthType(AuthType.NONE); HttpResponse cached = new HttpResponseBuilder().setCacheTtl(-1).create(); cache.data.put(DEFAULT_URI, cached); fetcher.response = HttpResponse.error(); HttpResponse response = pipeline.execute(request); assertEquals(cached, response); // cached item is served instead of 500 assertEquals(request, fetcher.request); assertEquals(1, cache.readCount); assertEquals(0, cache.writeCount); assertEquals(1, fetcher.fetchCount); }
@Test public void testCopyResponseToServlet() throws Exception { HttpResponse response = new HttpResponseBuilder() .setResponseString("response string").setHttpStatusCode(200).addHeader("h1", "v1") .addHeader("h2", "v2").setCacheTtl(1000).create(); HttpServletResponse servletResponse = EasyMock.createMock(HttpServletResponse.class); HttpServletResponseRecorder recorder = new HttpServletResponseRecorder(servletResponse); ServletUtil.copyResponseToServlet(response, recorder); assertEquals(200, recorder.getHttpStatusCode()); assertEquals("response string", recorder.getResponseAsString()); assertEquals("v1", recorder.getHeader("h1")); assertEquals("v2", recorder.getHeader("h2")); }
@Test public void testFixedDateNew() throws Exception { int time = roundToSeconds(HttpResponseTest.timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader( "Date", DateUtil.formatRfc1123Date(1000L * time + 1000 + DefaultRequestPipeline.DEFAULT_DRIFT_LIMIT_MS)).setCacheTtl(100) .create(); response = DefaultRequestPipeline.maybeFixDriftTime(response); // Verify that the old time is ignored: assertEquals(time + 100, roundToSeconds(response.getCacheExpiration())); assertEquals(DateUtil.formatRfc1123Date(HttpResponseTest.timeSource.currentTimeMillis()), response.getHeader("Date")); }