Skip to content

Commit

Permalink
[4.x] Fix bad merge (#8053)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke authored Oct 15, 2023
1 parent cd581af commit 9553f6d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,7 @@ class InMemoryFileSystem : FileSystem, TestRule {
}

override fun toString() = "InMemoryFileSystem"
fun allPaths(): MutableSet<File> {
return files.keys
}
}
33 changes: 18 additions & 15 deletions okhttp/src/test/java/okhttp3/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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!
Expand All @@ -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 {
Expand Down

0 comments on commit 9553f6d

Please sign in to comment.