diff --git a/okhttp-testing-support/src/main/kotlin/okhttp3/internal/io/InMemoryFileSystem.kt b/okhttp-testing-support/src/main/kotlin/okhttp3/internal/io/InMemoryFileSystem.kt index f6832d2a66af..ccf28b649932 100644 --- a/okhttp-testing-support/src/main/kotlin/okhttp3/internal/io/InMemoryFileSystem.kt +++ b/okhttp-testing-support/src/main/kotlin/okhttp3/internal/io/InMemoryFileSystem.kt @@ -119,4 +119,7 @@ class InMemoryFileSystem : FileSystem, TestRule { } override fun toString() = "InMemoryFileSystem" + fun allPaths(): MutableSet { + return files.keys + } } diff --git a/okhttp/src/test/java/okhttp3/CacheTest.java b/okhttp/src/test/java/okhttp3/CacheTest.java index 632322f445fd..1c1695e76a67 100644 --- a/okhttp/src/test/java/okhttp3/CacheTest.java +++ b/okhttp/src/test/java/okhttp3/CacheTest.java @@ -296,18 +296,16 @@ private void testResponseCaching(TransferKind transferKind) throws IOException { assertThat(response2.handshake().localPrincipal()).isEqualTo(localPrincipal); } - @Test public void secureResponseCachingWithCorruption() throws IOException { - server.useHttps(handshakeCertificates.sslSocketFactory()); - server.enqueue(new MockResponse.Builder() + @Test public void secureResponseCachingWithCorruption() throws Exception { + server.useHttps(handshakeCertificates.sslSocketFactory(), false); + server.enqueue(new MockResponse() .addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)) .addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)) - .body("ABC") - .build()); - server.enqueue(new MockResponse.Builder() + .setBody("ABC")); + server.enqueue(new MockResponse() .addHeader("Last-Modified: " + formatDate(-5, TimeUnit.MINUTES)) .addHeader("Expires: " + formatDate(2, TimeUnit.HOURS)) - .body("DEF") - .build()); + .setBody("DEF")); client = client.newBuilder() .sslSocketFactory( @@ -319,10 +317,10 @@ private void testResponseCaching(TransferKind transferKind) throws IOException { Response response1 = client.newCall(request).execute(); assertThat(response1.body().string()).isEqualTo("ABC"); - Path cacheEntry = fileSystem.allPaths().stream() - .filter((e) -> e.name().endsWith(".0")) + File cacheEntry = fileSystem.allPaths().stream() + .filter((e) -> e.getName().endsWith(".0")) .findFirst() - .orElseThrow(); + .orElseThrow(Exception::new); corruptCertificate(cacheEntry); Response response2 = client.newCall(request).execute(); // Not Cached! @@ -333,10 +331,15 @@ private void testResponseCaching(TransferKind transferKind) throws IOException { assertThat(cache.hitCount()).isEqualTo(0); } - private void corruptCertificate(Path cacheEntry) throws IOException { - String content = Okio.buffer(fileSystem.source(cacheEntry)).readUtf8(); - content = content.replace("MII", "!!!"); - Okio.buffer(fileSystem.sink(cacheEntry)).writeUtf8(content).close(); + private void corruptCertificate(File cacheEntry) throws IOException { + BufferedSource source = Okio.buffer(fileSystem.source(cacheEntry)); + try { + String content = source.readUtf8(); + content = content.replace("MII", "!!!"); + Okio.buffer(fileSystem.sink(cacheEntry)).writeUtf8(content).close(); + } finally { + source.close(); + } } @Test public void responseCachingAndRedirects() throws Exception {