コード例 #1
0
  @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();
  }
コード例 #2
0
  @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);
  }
コード例 #3
0
  @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);
  }
コード例 #4
0
 @Config(reportSdk = ICE_CREAM_SANDWICH)
 @Test
 public void shutdownClosesCache() throws Exception {
   HttpResponseCache cache = mock(HttpResponseCache.class);
   UrlConnectionDownloader.cache = cache;
   loader.shutdown();
   verify(cache).close();
 }
コード例 #5
0
  @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();
  }
コード例 #6
0
 @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");
   }
 }
コード例 #7
0
 @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);
 }