@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 testPreserveBinaryData() throws Exception { byte[] data = { (byte)0x00, (byte)0xDE, (byte)0xEA, (byte)0xDB, (byte)0xEE, (byte)0xF0 }; HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "application/octet-stream") .setResponse(data) .create(); byte[] out = IOUtils.toByteArray(response.getResponse()); assertEquals(data.length, response.getContentLength()); assertTrue(Arrays.equals(data, out)); out = IOUtils.toByteArray(response.getResponse()); assertTrue(Arrays.equals(data, out)); }
@Test public void testPreserveBinaryData() throws Exception { byte[] data = { (byte)0x00, (byte)0xDE, (byte)0xEA, (byte)0xDB, (byte)0xEE, (byte)0xF0 }; HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "application/octet-stream") .setResponse(data) .create(); byte[] out = IOUtils.toByteArray(response.getResponse()); assertEquals(data.length, response.getContentLength()); assertTrue(Arrays.equals(data, out)); out = IOUtils.toByteArray(response.getResponse()); assertTrue(Arrays.equals(data, out)); }
@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); }
@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); } }
@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 assertAllowsNegativeCaching(int status) { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(status) .setResponse(UTF8_DATA) .setStrictNoCache() .create(); long ttl = response.getCacheTtl(); assertTrue(ttl <= response.getDefaultTtl() && ttl > 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); }
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 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 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 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 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 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); }