@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 testEncodingDetectionLatin1NoCharsetSpecified() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "text/plain;") .setResponse(LATIN1_DATA) .create(); assertEquals("ISO-8859-1", response.getEncoding().toUpperCase()); assertEquals(LATIN1_STRING, response.getResponseAsString()); }
@Test public void testEncodingDetectionUtf8WithBom() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "text/plain; charset=UTF-8") .setResponse(UTF8_DATA) .create(); assertEquals(UTF8_STRING, response.getResponseAsString()); assertEquals("UTF-8", response.getEncoding()); }
@Test public void testEncodingDetectionUtf8WithBomAndIncorrectCharset() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "text/plain; charset=UTTFF-88") .setResponse(UTF8_DATA) .create(); assertEquals(UTF8_STRING, response.getResponseAsString()); assertEquals("UTF-8", response.getEncoding()); }
@Test public void testGetEncodingForImageContentType() throws Exception { HttpResponse response = new HttpResponseBuilder() .setResponse(LATIN1_DATA) .addHeader("Content-Type", "image/png; charset=iso-8859-1") .create(); assertEquals(HttpResponse.DEFAULT_ENCODING.name(), response.getEncoding().toUpperCase()); }
@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 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 testCacheExpirationForStrictNoCacheResponse() throws Exception { assertEquals(-1, new HttpResponseBuilder() .setStrictNoCache() .setRefetchStrictNoCacheAfterMs(10000) .create() .getCacheExpiration()); }
@Test public void testEncodingDetectionUtf8WithBomCaseInsensitiveKey() throws Exception { HttpResponse response = new HttpResponseBuilder() .addHeader("Content-Type", "text/plain; Charset=utf-8") // Legitimate data, should be ignored in favor of explicit charset. .setResponse(LATIN1_DATA) .create(); assertEquals("UTF-8", response.getEncoding()); }
@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 testCacheExpirationForStrictNoCacheResponsePrivateLowMaxAge() throws Exception { assertEquals(-1, new HttpResponseBuilder() .addHeader("Cache-Control", "private, max-age=5000") .setRefetchStrictNoCacheAfterMs(10000) .create() .getCacheExpiration()); }
@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 testShouldRefetchForStrictNoCacheResponsePastShouldNotRefetch() throws Exception { assertEquals(false, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 5000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }
@Test public void testShouldRefetchForStrictNoCacheResponsePastShouldNotRefetch() throws Exception { assertEquals(false, new HttpResponseBuilder().setStrictNoCache() .setHeader("Date", DateUtil.formatRfc1123Date(HttpUtil.getTimeSource().currentTimeMillis() - 5000)) .setRefetchStrictNoCacheAfterMs(10000) .create() .shouldRefetch()); }
@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 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); }