Search in sources :

Example 6 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method connectViaHttpsReusingConnectionsDifferentFactories.

@Test
public void connectViaHttpsReusingConnectionsDifferentFactories() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    // install a custom SSL socket factory so the server can be authorized
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
    assertContent("this response comes via HTTPS", connection1);
    SSLContext sslContext2 = SSLContext.getInstance("TLS");
    sslContext2.init(null, null, null);
    SSLSocketFactory sslSocketFactory2 = sslContext2.getSocketFactory();
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init((KeyStore) null);
    X509TrustManager trustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslSocketFactory2, trustManager).build());
    HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
    try {
        readAscii(connection2.getInputStream(), Integer.MAX_VALUE);
        fail("without an SSL socket factory, the connection should fail");
    } catch (SSLException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SSLException(javax.net.ssl.SSLException) Test(org.junit.Test)

Example 7 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method testClientConfiguredGzipContentEncodingAndConnectionReuse.

/**
   * Test a bug where gzip input streams weren't exhausting the input stream, which corrupted the
   * request that followed or prevented connection reuse.
   * http://code.google.com/p/android/issues/detail?id=7059
   * http://code.google.com/p/android/issues/detail?id=38817
   */
private void testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind transferKind, boolean tls) throws Exception {
    if (tls) {
        SSLSocketFactory socketFactory = sslClient.socketFactory;
        RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
        server.useHttps(socketFactory, false);
        urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    }
    MockResponse responseOne = new MockResponse();
    responseOne.addHeader("Content-Encoding: gzip");
    transferKind.setBody(responseOne, gzip("one (gzipped)"), 5);
    server.enqueue(responseOne);
    MockResponse responseTwo = new MockResponse();
    transferKind.setBody(responseTwo, "two (identity)", 5);
    server.enqueue(responseTwo);
    HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
    connection1.addRequestProperty("Accept-Encoding", "gzip");
    InputStream gunzippedIn = new GZIPInputStream(connection1.getInputStream());
    assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
    assertEquals(0, server.takeRequest().getSequenceNumber());
    HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
    assertEquals("two (identity)", readAscii(connection2.getInputStream(), Integer.MAX_VALUE));
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 8 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method testConnectViaSocketFactory.

public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
    SocketFactory uselessSocketFactory = new SocketFactory() {

        public Socket createSocket() {
            throw new IllegalArgumentException("useless");
        }

        public Socket createSocket(InetAddress host, int port) {
            return null;
        }

        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {
            return null;
        }

        public Socket createSocket(String host, int port) {
            return null;
        }

        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) {
            return null;
        }
    };
    if (useHttps) {
        server.useHttps(sslClient.socketFactory, false);
        urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    }
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(uselessSocketFactory).build());
    connection = urlFactory.open(server.url("/").url());
    try {
        connection.getResponseCode();
        fail();
    } catch (IllegalArgumentException expected) {
    }
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(SocketFactory.getDefault()).build());
    connection = urlFactory.open(server.url("/").url());
    assertEquals(200, connection.getResponseCode());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) InetAddress(java.net.InetAddress)

Example 9 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method connectViaHttpsWithSSLFallback.

// TODO(jwilson): tests below this marker need to be migrated to OkHttp's request/response API.
@Test
public void connectViaHttpsWithSSLFallback() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setSocketPolicy(FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("this response comes via SSL"));
    urlFactory.setClient(urlFactory.client().newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build());
    connection = urlFactory.open(server.url("/foo").url());
    assertContent("this response comes via SSL", connection);
    RecordedRequest failHandshakeRequest = server.takeRequest();
    assertNull(failHandshakeRequest.getRequestLine());
    RecordedRequest fallbackRequest = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", fallbackRequest.getRequestLine());
    assertEquals(TlsVersion.TLS_1_0, fallbackRequest.getTlsVersion());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 10 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method redirectedOnHttps.

@Test
public void redirectedOnHttps() throws IOException, InterruptedException {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("This is the new location!"));
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    connection = urlFactory.open(server.url("/").url());
    assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertEquals("GET / HTTP/1.1", first.getRequestLine());
    RecordedRequest retry = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
    assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)35 Test (org.junit.Test)31 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)20 URL (java.net.URL)8 RecordingHostnameVerifier (okhttp3.RecordingHostnameVerifier)6 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)5 Call (okhttp3.Call)4 CertificatePinner (okhttp3.CertificatePinner)4 OkHttpClient (okhttp3.OkHttpClient)4 Request (okhttp3.Request)4 SingleInetAddressDns (okhttp3.internal.SingleInetAddressDns)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)3 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)3 HttpURLConnection (java.net.HttpURLConnection)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 SSLContext (javax.net.ssl.SSLContext)2 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)2 SSLProtocolException (javax.net.ssl.SSLProtocolException)2 Response (okhttp3.Response)2 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)2