Search in sources :

Example 16 with Handshake

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());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 17 with Handshake

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();
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Certificate(java.security.cert.Certificate) HeldCertificate(okhttp3.internal.tls.HeldCertificate) Test(org.junit.Test)

Aggregations

Handshake (okhttp3.Handshake)9 Certificate (java.security.cert.Certificate)8 Test (org.junit.Test)8 Request (okhttp3.Request)7 CacheResponse (java.net.CacheResponse)6 SecureCacheResponse (java.net.SecureCacheResponse)6 Response (okhttp3.Response)6 MockResponse (okhttp3.mockwebserver.MockResponse)5 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)4 Headers (okhttp3.Headers)4 ResponseBody (okhttp3.ResponseBody)4 IOException (java.io.IOException)3 List (java.util.List)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 JavaNetHeaders (okhttp3.internal.JavaNetHeaders)3 HttpHeaders (okhttp3.internal.http.HttpHeaders)3 Principal (java.security.Principal)2 X509Certificate (java.security.cert.X509Certificate)2 CipherSuite (okhttp3.CipherSuite)2 ConnectionSpec (okhttp3.ConnectionSpec)2