/** * 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 testFixedDateNew() throws Exception { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * time + 1000 + HttpResponse.DEFAULT_DRIFT_LIMIT_MS)) .create(); // Verify that the old time is ignored: assertEquals(time + roundToSeconds(response.getDefaultTtl()), roundToSeconds(response.getCacheExpiration())); assertTtlOk(roundToSeconds(response.getDefaultTtl()), response); }
@Test public void testFixedDate() throws Exception { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * time)) .create(); assertEquals(time + roundToSeconds(response.getDefaultTtl()), roundToSeconds(response.getCacheExpiration())); assertEquals(DateUtil.formatRfc1123Date(timeSource.currentTimeMillis()), response.getHeader("Date")); assertTtlOk(roundToSeconds(response.getDefaultTtl()), response); }
@Test public void testFixedDate() throws Exception { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * time)) .create(); assertEquals(time + roundToSeconds(response.getDefaultTtl()), roundToSeconds(response.getCacheExpiration())); assertEquals(DateUtil.formatRfc1123Date(timeSource.currentTimeMillis()), response.getHeader("Date")); assertTtlOk(roundToSeconds(response.getDefaultTtl()), response); }
@Test public void testMaxAgeNoDate() throws Exception { int maxAge = 10; // Guess time. int expected = roundToSeconds(timeSource.currentTimeMillis()) + maxAge; HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "public, max-age=" + maxAge) .create(); int expiration = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expiration); assertTtlOk(maxAge, response); }
@Test public void testMaxAgeNoDate() throws Exception { int maxAge = 10; // Guess time. int expected = roundToSeconds(timeSource.currentTimeMillis()) + maxAge; HttpResponse response = new HttpResponseBuilder() .addHeader("Cache-Control", "public, max-age=" + maxAge) .create(); int expiration = roundToSeconds(response.getCacheExpiration()); assertEquals(expected, expiration); assertTtlOk(maxAge, response); }
@Test public void testExpires() throws Exception { int maxAge = 10; int time = roundToSeconds(timeSource.currentTimeMillis()) + maxAge; HttpResponse response = new HttpResponseBuilder() .addHeader("Expires", DateUtil.formatRfc1123Date(1000L * time)) .create(); assertEquals(time, roundToSeconds(response.getCacheExpiration())); // Second rounding makes this n-1. assertTtlOk(maxAge, response); }
@Test public void testExpires() throws Exception { int maxAge = 10; int time = roundToSeconds(timeSource.currentTimeMillis()) + maxAge; HttpResponse response = new HttpResponseBuilder() .addHeader("Expires", DateUtil.formatRfc1123Date(1000L * time)) .create(); assertEquals(time, roundToSeconds(response.getCacheExpiration())); // Second rounding makes this n-1. assertTtlOk(maxAge, response); }
@Test public void testExpires() throws Exception { int maxAge = 10; int time = roundToSeconds(timeSource.currentTimeMillis()) + maxAge; HttpResponse response = new HttpResponseBuilder() .addHeader("Expires", DateUtil.formatRfc1123Date(1000L * time)) .create(); assertEquals(time, roundToSeconds(response.getCacheExpiration())); // Second rounding makes this n-1. assertTtlOk(maxAge, response); }
@Test public void testMaxAgeWithDate() throws Exception { int maxAge = 10; int now = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * now)) .addHeader("Cache-Control", "public, max-age=" + maxAge) .create(); assertEquals(now + maxAge, roundToSeconds(response.getCacheExpiration())); assertTtlOk(maxAge, response); }
@Test public void testMaxAgeWithDate() throws Exception { int maxAge = 10; int now = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Date", DateUtil.formatRfc1123Date(1000L * now)) .addHeader("Cache-Control", "public, max-age=" + maxAge) .create(); assertEquals(now + maxAge, roundToSeconds(response.getCacheExpiration())); assertTtlOk(maxAge, response); }
@Test public void testExpiresZeroValue() throws Exception { HttpResponse response = new HttpResponseBuilder().addHeader("Expires", "0").create(); assertEquals(0, roundToSeconds(response.getCacheExpiration())); }
@Test public void testExpiresUnknownValue() throws Exception { HttpResponse response = new HttpResponseBuilder().addHeader("Expires", "howdy").create(); assertEquals(0, roundToSeconds(response.getCacheExpiration())); }
@Test public void testExpiresZeroValue() throws Exception { HttpResponse response = new HttpResponseBuilder().addHeader("Expires", "0").create(); assertEquals(0, roundToSeconds(response.getCacheExpiration())); }
@Test public void testExpiresUnknownValue() throws Exception { HttpResponse response = new HttpResponseBuilder().addHeader("Expires", "howdy").create(); assertEquals(0, roundToSeconds(response.getCacheExpiration())); }
@Test public void testExpiresUnknownValue() throws Exception { HttpResponse response = new HttpResponseBuilder().addHeader("Expires", "howdy").create(); assertEquals(0, roundToSeconds(response.getCacheExpiration())); }
@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); }
@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); }
@Test public void testSetNoCache() { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Expires", DateUtil.formatRfc1123Date(1000L * time)) .setStrictNoCache() .create(); assertNull(response.getHeader("Expires")); assertEquals("no-cache", response.getHeader("Pragma")); assertEquals("no-cache", response.getHeader("Cache-Control")); }
@Test public void testSetNoCache() { int time = roundToSeconds(timeSource.currentTimeMillis()); HttpResponse response = new HttpResponseBuilder() .addHeader("Expires", DateUtil.formatRfc1123Date(1000L * time)) .setStrictNoCache() .create(); assertNull(response.getHeader("Expires")); assertEquals("no-cache", response.getHeader("Pragma")); assertEquals("no-cache", response.getHeader("Cache-Control")); }