Skip to content

Commit 9553f6d

Browse files
authored
[4.x] Fix bad merge (#8053)
1 parent cd581af commit 9553f6d

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

okhttp-testing-support/src/main/kotlin/okhttp3/internal/io/InMemoryFileSystem.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,7 @@ class InMemoryFileSystem : FileSystem, TestRule {
119119
}
120120

121121
override fun toString() = "InMemoryFileSystem"
122+
fun allPaths(): MutableSet<File> {
123+
return files.keys
124+
}
122125
}

okhttp/src/test/java/okhttp3/CacheTest.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,16 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
296296
assertThat(response2.handshake().localPrincipal()).isEqualTo(localPrincipal);
297297
}
298298

299-
@Test public void secureResponseCachingWithCorruption() throws IOException {
300-
server.useHttps(handshakeCertificates.sslSocketFactory());
301-
server.enqueue(new MockResponse.Builder()
299+
@Test public void secureResponseCachingWithCorruption() throws Exception {
300+
server.useHttps(handshakeCertificates.sslSocketFactory(), false);
301+
server.enqueue(new MockResponse()
302302
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
303303
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
304-
.body("ABC")
305-
.build());
306-
server.enqueue(new MockResponse.Builder()
304+
.setBody("ABC"));
305+
server.enqueue(new MockResponse()
307306
.addHeader("Last-Modified: " + formatDate(-5, TimeUnit.MINUTES))
308307
.addHeader("Expires: " + formatDate(2, TimeUnit.HOURS))
309-
.body("DEF")
310-
.build());
308+
.setBody("DEF"));
311309

312310
client = client.newBuilder()
313311
.sslSocketFactory(
@@ -319,10 +317,10 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
319317
Response response1 = client.newCall(request).execute();
320318
assertThat(response1.body().string()).isEqualTo("ABC");
321319

322-
Path cacheEntry = fileSystem.allPaths().stream()
323-
.filter((e) -> e.name().endsWith(".0"))
320+
File cacheEntry = fileSystem.allPaths().stream()
321+
.filter((e) -> e.getName().endsWith(".0"))
324322
.findFirst()
325-
.orElseThrow();
323+
.orElseThrow(Exception::new);
326324
corruptCertificate(cacheEntry);
327325

328326
Response response2 = client.newCall(request).execute(); // Not Cached!
@@ -333,10 +331,15 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
333331
assertThat(cache.hitCount()).isEqualTo(0);
334332
}
335333

336-
private void corruptCertificate(Path cacheEntry) throws IOException {
337-
String content = Okio.buffer(fileSystem.source(cacheEntry)).readUtf8();
338-
content = content.replace("MII", "!!!");
339-
Okio.buffer(fileSystem.sink(cacheEntry)).writeUtf8(content).close();
334+
private void corruptCertificate(File cacheEntry) throws IOException {
335+
BufferedSource source = Okio.buffer(fileSystem.source(cacheEntry));
336+
try {
337+
String content = source.readUtf8();
338+
content = content.replace("MII", "!!!");
339+
Okio.buffer(fileSystem.sink(cacheEntry)).writeUtf8(content).close();
340+
} finally {
341+
source.close();
342+
}
340343
}
341344

342345
@Test public void responseCachingAndRedirects() throws Exception {

0 commit comments

Comments
 (0)