From 9553f6dce95ca7b867d3fac34a474a1f1014288e Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Sun, 15 Oct 2023 12:49:48 +0100 Subject: [PATCH] [4.x] Fix bad merge (#8053) --- .../okhttp3/internal/io/InMemoryFileSystem.kt | 3 ++ okhttp/src/test/java/okhttp3/CacheTest.java | 33 ++++++++++--------- 2 files changed, 21 insertions(+), 15 deletions(-) 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 {