/** * @return True if this result is stale. */ public boolean isStale() { return getCacheTtl() <= 0; }
/** * @return True if this result is stale. */ public boolean isStale() { if(getCacheControlMaxAge() == 0) { return true; } return getCacheTtl() <= 0; }
public static void copyResponseToServlet(HttpResponse response, HttpServletResponse servletResponse) throws IOException { servletResponse.setStatus(response.getHttpStatusCode()); servletResponse.setContentLength(response.getContentLength()); for (Map.Entry<String, String> header : response.getHeaders().entries()) { servletResponse.addHeader(header.getKey(), header.getValue()); } HttpUtil.setCachingHeaders(servletResponse, (int)response.getCacheTtl()); IOUtils.copy(response.getResponse(), servletResponse.getOutputStream()); }
public static void copyToServletResponseAndOverrideCacheHeaders( HttpResponse response, HttpServletResponse servletResponse) throws IOException { copyHeadersAndStatusToServletResponse(response, servletResponse); HttpUtil.setCachingHeaders(servletResponse, (int)(response.getCacheTtl() / 1000L)); copyContentToServletResponse(response, servletResponse); }
/** * Verifies that the cache TTL is within acceptable ranges. * This always rounds down due to timing, so actual verification will be against maxAge - 1. */ private static void assertTtlOk(int maxAge, HttpResponse response) { assertEquals(maxAge - 1, roundToSeconds(response.getCacheTtl() - 1)); }
/** * Verifies that the cache TTL is within acceptable ranges. * This always rounds down due to timing, so actual verification will be against maxAge - 1. */ private static void assertTtlOk(int maxAge, HttpResponse response) { assertEquals(maxAge - 1, roundToSeconds(response.getCacheTtl() - 1)); }
/** * Verifies that the cache TTL is within acceptable ranges. * This always rounds down due to timing, so actual verification will be against maxAge - 1. */ private static void assertTtlOk(int maxAge, HttpResponse response) { assertEquals(maxAge - 1, roundToSeconds(response.getCacheTtl() - 1)); }
@Test public void testRetryAfter() { HttpResponse response; for (int rc : Arrays.asList(HttpResponse.SC_INTERNAL_SERVER_ERROR, HttpResponse.SC_GATEWAY_TIMEOUT, HttpResponse.SC_BAD_REQUEST)) { response = new HttpResponseBuilder() .setHttpStatusCode(rc) .setHeader("Retry-After","60") .create(); long ttl = response.getCacheTtl(); assertTrue(ttl <= 60 * 1000L && ttl > 0); } }
@Test public void testRetryAfter() { HttpResponse response; for (int rc : Arrays.asList(HttpResponse.SC_INTERNAL_SERVER_ERROR, HttpResponse.SC_GATEWAY_TIMEOUT, HttpResponse.SC_BAD_REQUEST)) { response = new HttpResponseBuilder() .setHttpStatusCode(rc) .setHeader("Retry-After","60") .create(); long ttl = response.getCacheTtl(); assertTrue(ttl <= 60 * 1000L && ttl > 0); } }
private static void assertDoesNotAllowNegativeCaching(int status) { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(status) .setResponse(UTF8_DATA) .setStrictNoCache() .create(); assertEquals(-1, response.getCacheTtl()); }
private static void assertDoesNotAllowNegativeCaching(int status) { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(status) .setResponse(UTF8_DATA) .setStrictNoCache() .create(); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictPragmaJunk() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "junk") .create(); assertFalse(response.isStrictNoCache()); int expected = roundToSeconds(timeSource.currentTimeMillis() + response.getDefaultTtl()); int expires = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expires); assertTrue(response.getCacheTtl() <= response.getDefaultTtl() && response.getCacheTtl() > 0); }
private static void assertAllowsNegativeCaching(int status) { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(status) .setResponse(UTF8_DATA) .setStrictNoCache() .create(); long ttl = response.getCacheTtl(); assertTrue(ttl <= response.getDefaultTtl() && ttl > 0); }
@Test public void testOAuthFlow_unauthUser() throws Exception { MakeRequestClient client = makeNonSocialClient(null, null, GADGET_URL); HttpResponse response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL); assertEquals("", response.getResponseAsString()); assertEquals(403, response.getHttpStatusCode()); assertEquals(-1, response.getCacheTtl()); assertEquals(OAuthError.UNAUTHENTICATED.name(), response.getMetadata().get("oauthError")); }
@Test public void testOAuthFlow_unauthUser() throws Exception { MakeRequestClient client = makeNonSocialClient(null, null, GADGET_URL); HttpResponse response = client.sendGet(FakeOAuthServiceProvider.RESOURCE_URL); assertEquals("", response.getResponseAsString()); assertEquals(403, response.getHttpStatusCode()); assertEquals(-1, response.getCacheTtl()); assertEquals(OAuthError.UNAUTHENTICATED.name(), response.getMetadata().get("oauthError")); }
@Test public void testStrictCacheControlNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictCacheControlNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testStrictPragmaNoCache() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Pragma", "no-cache") .create(); assertTrue(response.isStrictNoCache()); assertEquals(-1, response.getCacheExpiration()); assertEquals(-1, response.getCacheTtl()); }
@Test public void testNegativeCaching() { assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.error().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.notFound().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.timeout().getCacheExpiration() > timeSource.currentTimeMillis()); long ttl = HttpResponse.error().getCacheTtl(); assertTrue(ttl <= HttpResponse.DEFAULT_TTL && ttl > 0); }
@Test public void testNegativeCaching() { assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.error().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.notFound().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.timeout().getCacheExpiration() > timeSource.currentTimeMillis()); long ttl = HttpResponse.error().getCacheTtl(); assertTrue(ttl <= HttpResponse.DEFAULT_TTL && ttl > 0); }