Downloader { public Resources load(@Cache.Policy int policy) throws ExternalResourceException { buildUrl(); Logger.i(ExternalResources.TAG, "Load configuration from url: " + url.build()); final CacheControl cacheControl; switch (policy) { case Cache.POLICY_NONE: cacheControl = new CacheControl.Builder().noCache().noStore().build(); break; case Cache.POLICY_OFFLINE: cacheControl = CacheControl.FORCE_CACHE; break; case Cache.POLICY_ALL: default: cacheControl = new CacheControl.Builder().build(); break; } Logger.v(ExternalResources.TAG, "CachePolicy: " + policy); Request request = new Request.Builder().url(url.build()).cacheControl(cacheControl).build(); try { Response response = client.newCall(request).execute(); int responseCode = response.code(); Logger.d(ExternalResources.TAG, "Response code: " + responseCode); if (responseCode >= 300) { response.body().close(); throw new ResponseException(responseCode + " " + response.message(), policy, responseCode); } return converter.fromReader(response.body().charStream()); } catch (IOException e) { throw new ExternalResourceException(e); } } Downloader(@NonNull Context context, @NonNull Converter converter, @NonNull Url url,
      @NonNull Options options);  Downloader(@NonNull Context context, @NonNull OkHttpClient client,
      @NonNull Converter converter, @NonNull Url url, @NonNull Options options); Resources load(@Cache.Policy int policy);  }
@Test public void testLoad() throws Exception { server.start(); DefaultUrl url = new DefaultUrl(server.url("/success").toString()); Downloader downloader = new Downloader(context, converter, url, options); assertNotNull("From network", downloader.load(Cache.POLICY_ALL)); server.shutdown(); assertNotNull("From cache", downloader.load(Cache.POLICY_OFFLINE)); }
@Test(expected = ExternalResourceException.class) public void testLoadWithException() throws Exception { server.start(); DefaultUrl url = new DefaultUrl(server.url("/error").toString()); Downloader downloader = new Downloader(context, converter, url, options); downloader.load(Cache.POLICY_NONE); server.shutdown(); }