@@ -296,18 +296,16 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
296
296
assertThat (response2 .handshake ().localPrincipal ()).isEqualTo (localPrincipal );
297
297
}
298
298
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 ()
302
302
.addHeader ("Last-Modified: " + formatDate (-1 , TimeUnit .HOURS ))
303
303
.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 ()
307
306
.addHeader ("Last-Modified: " + formatDate (-5 , TimeUnit .MINUTES ))
308
307
.addHeader ("Expires: " + formatDate (2 , TimeUnit .HOURS ))
309
- .body ("DEF" )
310
- .build ());
308
+ .setBody ("DEF" ));
311
309
312
310
client = client .newBuilder ()
313
311
.sslSocketFactory (
@@ -319,10 +317,10 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
319
317
Response response1 = client .newCall (request ).execute ();
320
318
assertThat (response1 .body ().string ()).isEqualTo ("ABC" );
321
319
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" ))
324
322
.findFirst ()
325
- .orElseThrow ();
323
+ .orElseThrow (Exception :: new );
326
324
corruptCertificate (cacheEntry );
327
325
328
326
Response response2 = client .newCall (request ).execute (); // Not Cached!
@@ -333,10 +331,15 @@ private void testResponseCaching(TransferKind transferKind) throws IOException {
333
331
assertThat (cache .hitCount ()).isEqualTo (0 );
334
332
}
335
333
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
+ }
340
343
}
341
344
342
345
@ Test public void responseCachingAndRedirects () throws Exception {
0 commit comments