@Test public void noneResponseSourceHeaders() throws Exception { server.enqueue(new MockResponse().setBody("Isla Nublar")); HttpURLConnection connection1 = factory.open(server.getUrl("/")); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.getUrl("/")); connection2.setRequestProperty("Cache-Control", "only-if-cached"); assertResponseHeader(connection2, "NONE"); }
@Test public void cacheResponseSourceHeaders() throws Exception { server.enqueue( new MockResponse() .addHeader("Expires: " + formatDate(2, TimeUnit.HOURS)) .setBody("Isla Nublar")); HttpURLConnection connection1 = factory.open(server.getUrl("/")); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.getUrl("/")); assertResponseHeader(connection2, "CACHE 200"); 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.getUrl("/")); assertResponseHeader(connection1, "NETWORK 200"); assertResponseBody(connection1, "Isla Nublar"); HttpURLConnection connection2 = factory.open(server.getUrl("/")); assertResponseHeader(connection2, "CONDITIONAL_CACHE 200"); assertResponseBody(connection2, "Isla Sorna"); }
@Test public void networkFailureResponseSourceHeader() throws Exception { server.enqueue(new MockResponse().setResponseCode(404)); HttpURLConnection connection = factory.open(server.getUrl("/")); assertResponseHeader(connection, "NETWORK 404"); connection.getErrorStream().close(); }
@Test public void networkResponseSourceHeader() throws Exception { server.enqueue(new MockResponse().setBody("Isla Sorna")); HttpURLConnection connection = factory.open(server.getUrl("/")); assertResponseHeader(connection, "NETWORK 200"); assertResponseBody(connection, "Isla Sorna"); }
@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.getUrl("/a")); connection.setInstanceFollowRedirects(false); assertResponseBody(connection, "A"); assertResponseCode(connection, 302); }
/** * Response code 407 should only come from proxy servers. Android's client throws if it is sent by * an origin server. */ @Test public void originServerSends407() throws Exception { server.enqueue(new MockResponse().setResponseCode(407)); HttpURLConnection conn = factory.open(server.getUrl("/")); try { conn.getResponseCode(); fail(); } catch (IOException ignored) { } }
@Override protected HttpURLConnection createConnection(URL url) throws IOException { return mFactory.open(url); }