OkUrlFactory implements URLStreamHandlerFactory, Cloneable { public HttpURLConnection open(URL url) { return open(url, client.proxy()); } OkUrlFactory(OkHttpClient client); OkHttpClient client(); OkUrlFactory setClient(OkHttpClient client); @Override OkUrlFactory clone(); HttpURLConnection open(URL url); @Override URLStreamHandler createURLStreamHandler(final String protocol);  }
@Test public void originServerSends407() throws Exception { server.enqueue(new MockResponse().setResponseCode(407)); HttpURLConnection conn = factory.open(server.url("/").url()); try { conn.getResponseCode(); fail(); } catch (IOException ignored) { } }
@Test public void networkResponseSourceHeader() throws Exception { server.enqueue(new MockResponse().setBody("Isla Sorna")); HttpURLConnection connection = factory.open(server.url("/").url()); assertResponseHeader(connection, "NETWORK 200"); assertResponseBody(connection, "Isla Sorna"); }
@Test public void networkFailureResponseSourceHeader() throws Exception { server.enqueue(new MockResponse().setResponseCode(404)); HttpURLConnection connection = factory.open(server.url("/").url()); assertResponseHeader(connection, "NETWORK 404"); connection.getErrorStream().close(); }
@Test public void conditionalCacheHitResponseSourceHeaders() throws Exception { server.enqueue(new MockResponse() .addHeader("Last-Modified: " + formatDate(0, TimeUnit.SECONDS)) .addHeader("Cache-Control: max-age=0") .setBody("Isla Nublar")); server.enqueue(new MockResponse().setResponseCode(304)); HttpURLConnection connection1 = factory.open(server.url("/").url()); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.url("/").url()); assertResponseHeader(connection2, "CONDITIONAL_CACHE 304"); assertResponseBody(connection2, "Isla Nublar"); }
@Test public void conditionalCacheMissResponseSourceHeaders() throws Exception { server.enqueue(new MockResponse() .addHeader("Last-Modified: " + formatDate(0, TimeUnit.SECONDS)) .addHeader("Cache-Control: max-age=0") .setBody("Isla Nublar")); server.enqueue(new MockResponse().setBody("Isla Sorna")); HttpURLConnection connection1 = factory.open(server.url("/").url()); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.url("/").url()); assertResponseHeader(connection2, "CONDITIONAL_CACHE 200"); assertResponseBody(connection2, "Isla Sorna"); }
@Test public void cacheResponseSourceHeaders() throws Exception { server.enqueue(new MockResponse() .addHeader("Expires: " + formatDate(2, TimeUnit.HOURS)) .setBody("Isla Nublar")); HttpURLConnection connection1 = factory.open(server.url("/").url()); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.url("/").url()); assertResponseHeader(connection2, "CACHE 200"); assertResponseBody(connection2, "Isla Nublar"); }
@Test public void noneResponseSourceHeaders() throws Exception { server.enqueue(new MockResponse().setBody("Isla Nublar")); HttpURLConnection connection1 = factory.open(server.url("/").url()); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.url("/").url()); connection2.setRequestProperty("Cache-Control", "only-if-cached"); assertResponseHeader(connection2, "NONE"); }
@Test public void setInstanceFollowRedirectsFalse() throws Exception { server.enqueue(new MockResponse() .setResponseCode(302) .addHeader("Location: /b") .setBody("A")); server.enqueue(new MockResponse() .setBody("B")); HttpURLConnection connection = factory.open(server.url("/a").url()); connection.setInstanceFollowRedirects(false); assertResponseBody(connection, "A"); assertResponseCode(connection, 302); }