use of okhttp3.Handshake in project okhttp by square.
the class CacheTest method secureResponseCachingAndRedirects.
@Test
public void secureResponseCachingAndRedirects() throws IOException {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(NULL_HOSTNAME_VERIFIER).build();
Response response1 = get(server.url("/"));
assertEquals("ABC", response1.body().string());
assertNotNull(response1.handshake().cipherSuite());
// Cached!
Response response2 = get(server.url("/"));
assertEquals("ABC", response2.body().string());
assertNotNull(response2.handshake().cipherSuite());
// 2 direct + 2 redirect = 4
assertEquals(4, cache.requestCount());
assertEquals(2, cache.hitCount());
assertEquals(response1.handshake().cipherSuite(), response2.handshake().cipherSuite());
}
use of okhttp3.Handshake in project okhttp by square.
the class CallTest method matchingPinnedCertificate.
@Test
public void matchingPinnedCertificate() throws Exception {
enableTls();
server.enqueue(new MockResponse());
server.enqueue(new MockResponse());
// Make a first request without certificate pinning. Use it to collect certificates to pin.
Request request1 = new Request.Builder().url(server.url("/")).build();
Response response1 = client.newCall(request1).execute();
CertificatePinner.Builder certificatePinnerBuilder = new CertificatePinner.Builder();
for (Certificate certificate : response1.handshake().peerCertificates()) {
certificatePinnerBuilder.add(server.getHostName(), CertificatePinner.pin(certificate));
}
response1.body().close();
// Make another request with certificate pinning. It should complete normally.
client = client.newBuilder().certificatePinner(certificatePinnerBuilder.build()).build();
Request request2 = new Request.Builder().url(server.url("/")).build();
Response response2 = client.newCall(request2).execute();
assertNotSame(response2.handshake(), response1.handshake());
response2.body().close();
}
Aggregations