@Config(reportSdk = GINGERBREAD) @Test public void responseSourceHeaderSetsResponseValue() throws Exception { server.enqueue(new MockResponse()); Downloader.Response response1 = loader.load(URL, false); assertThat(response1.cached).isFalse(); server.enqueue(new MockResponse().addHeader(RESPONSE_SOURCE, "CACHE 200")); Downloader.Response response2 = loader.load(URL, true); assertThat(response2.cached).isTrue(); }
@Config(reportSdk = ICE_CREAM_SANDWICH) @Test public void cacheOnlyInstalledOnce() throws Exception { UrlConnectionDownloader.cache = null; server.enqueue(new MockResponse()); loader.load(URL, false); Object cache = UrlConnectionDownloader.cache; assertThat(cache).isNotNull(); server.enqueue(new MockResponse()); loader.load(URL, false); assertThat(UrlConnectionDownloader.cache).isSameAs(cache); }
@Config(reportSdk = GINGERBREAD) @Test public void allowExpiredSetsCacheControl() throws Exception { server.enqueue(new MockResponse()); loader.load(URL, false); RecordedRequest request1 = server.takeRequest(); assertThat(request1.getHeader("Cache-Control")).isNull(); server.enqueue(new MockResponse()); loader.load(URL, true); RecordedRequest request2 = server.takeRequest(); assertThat(request2.getHeader("Cache-Control")) // .isEqualTo("only-if-cached,max-age=" + Integer.MAX_VALUE); }
@Config(reportSdk = ICE_CREAM_SANDWICH) @Test public void shutdownClosesCache() throws Exception { HttpResponseCache cache = mock(HttpResponseCache.class); UrlConnectionDownloader.cache = cache; loader.shutdown(); verify(cache).close(); }
@Config(reportSdk = GINGERBREAD) @Test public void cacheNotInstalledWhenUnavailable() throws Exception { UrlConnectionDownloader.cache = null; server.enqueue(new MockResponse()); loader.load(URL, false); Object cache = UrlConnectionDownloader.cache; assertThat(cache).isNull(); }
@Test public void throwsResponseException() throws Exception { server.enqueue(new MockResponse().setStatus("HTTP/1.1 401 Not Authorized")); try { loader.load(URL, false); fail("Expected ResponseException."); } catch (Downloader.ResponseException e) { assertThat(e).hasMessage("401 Not Authorized"); } }
@Test public void readsContentLengthHeader() throws Exception { server.enqueue(new MockResponse().addHeader("Content-Length", 1024)); Downloader.Response response = loader.load(URL, true); assertThat(response.contentLength).isEqualTo(1024); }