Skip to content

Commit

Permalink
Fix cache test for 102/103 (#7633)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Jan 6, 2023
1 parent 33c0fef commit 930f138
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions okhttp/src/jvmTest/java/okhttp3/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down

0 comments on commit 930f138

Please sign in to comment.