diff --git a/okhttp/src/jvmTest/java/okhttp3/CacheTest.java b/okhttp/src/jvmTest/java/okhttp3/CacheTest.java index c22a52b50d28..4a564cdfb250 100644 --- a/okhttp/src/jvmTest/java/okhttp3/CacheTest.java +++ b/okhttp/src/jvmTest/java/okhttp3/CacheTest.java @@ -116,7 +116,6 @@ public void setUp( // We can't test 100 because it's not really a response. // assertCached(false, 100); assertCached(false, 101); - assertCached(false, 102); assertCached(true, 200); assertCached(false, 201); assertCached(false, 202); @@ -163,7 +162,12 @@ public void setUp( assertCached(false, 506); } - private void assertCached(boolean shouldPut, int responseCode) throws Exception { + @Test public void responseCachingWith1xxInformationalResponse() throws Exception { + assertSubsequentResponseCached( 102, 200); + assertSubsequentResponseCached( 103, 200); + } + + private void assertCached(boolean shouldWriteToCache, int responseCode) throws Exception { int expectedResponseCode = responseCode; server = new MockWebServer(); @@ -206,7 +210,7 @@ private void assertCached(boolean shouldPut, int responseCode) throws Exception response.body().string(); Response cached = cacheGet(cache, request); - if (shouldPut) { + if (shouldWriteToCache) { assertThat(cached).isNotNull(); cached.body().close(); } else { @@ -215,6 +219,33 @@ private void assertCached(boolean shouldPut, int responseCode) throws Exception server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers } + private void assertSubsequentResponseCached(int initialResponseCode, int finalResponseCode) throws Exception { + server = new MockWebServer(); + MockResponse.Builder builder = new MockResponse.Builder() + .addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)) + .addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)) + .code(finalResponseCode) + .body("ABCDE") + .addInformationalResponse(new MockResponse(initialResponseCode)); + + server.enqueue(builder.build()); + server.start(); + + Request request = new Request.Builder() + .url(server.url("/")) + .build(); + Response response = client.newCall(request).execute(); + assertThat(response.code()).isEqualTo(finalResponseCode); + + // Exhaust the content stream. + response.body().string(); + + Response cached = cacheGet(cache, request); + assertThat(cached).isNotNull(); + cached.body().close(); + server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers + } + @Test public void responseCachingAndInputStreamSkipWithFixedLength() throws IOException { testResponseCaching(TransferKind.FIXED_LENGTH); }