From cd30b10fb8f3c9822cae85d748eb21914f29df09 Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Fri, 6 Jan 2023 18:01:50 +1000 Subject: [PATCH] Fix cache test for 102/103 (#7633) (cherry picked from commit 930f1381253a157dad0074996c972bb8a95d90fb) --- okhttp/src/test/java/okhttp3/CacheTest.java | 37 +++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/okhttp/src/test/java/okhttp3/CacheTest.java b/okhttp/src/test/java/okhttp3/CacheTest.java index e95e090522a8..e59d7af9aa30 100644 --- a/okhttp/src/test/java/okhttp3/CacheTest.java +++ b/okhttp/src/test/java/okhttp3/CacheTest.java @@ -104,7 +104,6 @@ public final class CacheTest { // 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); @@ -151,7 +150,12 @@ public final class CacheTest { 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(); @@ -193,7 +197,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 { @@ -202,6 +206,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); }