@Test public void testStrictNoCacheAndNegativeCaching() { assertDoesNotAllowNegativeCaching(HttpResponse.SC_UNAUTHORIZED); assertDoesNotAllowNegativeCaching(HttpResponse.SC_FORBIDDEN); assertDoesNotAllowNegativeCaching(HttpResponse.SC_OK); assertAllowsNegativeCaching(HttpResponse.SC_NOT_FOUND); assertAllowsNegativeCaching(HttpResponse.SC_INTERNAL_SERVER_ERROR); assertAllowsNegativeCaching(HttpResponse.SC_GATEWAY_TIMEOUT); }
@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 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); }
/** * 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 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 testEncodingDetectionUtf8WithBomNoContentHeader() throws Exception { HttpResponse response = new HttpResponseBuilder() .setResponse(UTF8_DATA) .create(); assertEquals("UTF-8", response.getEncoding().toUpperCase()); assertEquals(UTF8_STRING, response.getResponseAsString()); }
@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 testIsError() { // These aren't all valid status codes, but they're reserved in these blocks. Changes // would be required to the HTTP standard anyway before this test would be invalid. for (int i = 100; i < 400; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertFalse("Status below 400 considered to be an error", response.isError()); } } for (int i = 400; i < 600; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertTrue("Status above 400 considered to be an error", response.isError()); } } }
@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 testNullHeaderNamesStripped() { HttpResponse response = new HttpResponseBuilder() .addHeader(null, "dummy") .create(); for (String key : response.getHeaders().keySet()) { assertNotNull("Null header not removed.", key); } }
@Before public void setUp() { setHttpTimeSource(); }
@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()); }
/** * 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 testEncodingDetectionUtf8WithBomNoContentHeader() throws Exception { HttpResponse response = new HttpResponseBuilder() .setResponse(UTF8_DATA) .create(); assertEquals("UTF-8", response.getEncoding().toUpperCase()); assertEquals(UTF8_STRING, response.getResponseAsString()); }
@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 testIsError() { // These aren't all valid status codes, but they're reserved in these blocks. Changes // would be required to the HTTP standard anyway before this test would be invalid. for (int i = 100; i < 400; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertFalse("Status below 400 considered to be an error", response.isError()); } } for (int i = 400; i < 600; i += 100) { for (int j = 0; j < 10; ++j) { HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(i).create(); assertTrue("Status above 400 considered to be an error", response.isError()); } } }
@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 testNullHeaderNamesStripped() { HttpResponse response = new HttpResponseBuilder() .addHeader(null, "dummy") .create(); for (String key : response.getHeaders().keySet()) { assertNotNull("Null header not removed.", key); } }
@Before public void setUp() { HttpResponseTest.setHttpTimeSource(); }
@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); }