Search in sources :

Example 21 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class CallTest method proxyAuthenticateOnConnect.

/** Respond to a proxy authorization challenge. */
@Test
public void proxyAuthenticateOnConnect() throws Exception {
    server.useHttps(sslClient.socketFactory, true);
    server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("response body"));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
    Request request = new Request.Builder().url("https://android.com/foo").build();
    Response response = client.newCall(request).execute();
    assertEquals("response body", response.body().string());
    RecordedRequest connect1 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
    assertNull(connect1.getHeader("Proxy-Authorization"));
    RecordedRequest connect2 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
    assertEquals("password", connect2.getHeader("Proxy-Authorization"));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertNull(get.getHeader("Proxy-Authorization"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordingOkAuthenticator(okhttp3.internal.RecordingOkAuthenticator) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 22 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class CallTest method httpsWithIpAddress.

@Test
public void httpsWithIpAddress() throws Exception {
    String localIpAddress = InetAddress.getLoopbackAddress().getHostAddress();
    // Create a certificate with an IP address in the subject alt name.
    HeldCertificate heldCertificate = new HeldCertificate.Builder().commonName("example.com").subjectAlternativeName(localIpAddress).build();
    SslClient sslClient = new SslClient.Builder().certificateChain(heldCertificate.keyPair, heldCertificate.certificate).addTrustedCertificate(heldCertificate.certificate).build();
    // Use that certificate on the server and trust it on the client.
    server.useHttps(sslClient.socketFactory, false);
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).protocols(Collections.singletonList(Protocol.HTTP_1_1)).build();
    // Make a request.
    server.enqueue(new MockResponse());
    HttpUrl url = server.url("/").newBuilder().host(localIpAddress).build();
    Request request = new Request.Builder().url(url).build();
    executeSynchronously(request).assertCode(200);
    // Confirm that the IP address was used in the host header.
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(localIpAddress + ":" + server.getPort(), recordedRequest.getHeader("Host"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) HeldCertificate(okhttp3.internal.tls.HeldCertificate) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 23 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class WebSocketHttpTest method wssScheme.

@Test
public void wssScheme() throws IOException {
    webServer.useHttps(sslClient.socketFactory, false);
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build();
    websocketScheme("wss");
}
Also used : RecordingHostnameVerifier(okhttp3.RecordingHostnameVerifier) Test(org.junit.Test)

Example 24 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class CallTest method recoverFromTlsHandshakeFailure.

@Test
public void recoverFromTlsHandshakeFailure() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).dns(new SingleInetAddressDns()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
    executeSynchronously("/").assertBody("abc");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) Test(org.junit.Test)

Example 25 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class CallTest method ipv6HostHasSquareBraces.

/** https://github.com/square/okhttp/issues/2344 */
@Test
public void ipv6HostHasSquareBraces() throws Exception {
    // Use a proxy to fake IPv6 connectivity, even if localhost doesn't have IPv6.
    server.useHttps(sslClient.socketFactory, true);
    server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("response body"));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).proxy(server.toProxyAddress()).build();
    Request request = new Request.Builder().url("https://[::1]/").build();
    Response response = client.newCall(request).execute();
    assertEquals("response body", response.body().string());
    RecordedRequest connect = server.takeRequest();
    assertEquals("CONNECT [::1]:443 HTTP/1.1", connect.getRequestLine());
    assertEquals("[::1]:443", connect.getHeader("Host"));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET / HTTP/1.1", get.getRequestLine());
    assertEquals("[::1]", get.getHeader("Host"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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