Search in sources :

Example 31 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method proxyConnectIncludesProxyHeadersOnly.

/** Test which headers are sent unencrypted to the HTTP proxy. */
@Test
public void proxyConnectIncludesProxyHeadersOnly() throws IOException, InterruptedException {
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    server.useHttps(sslClient.socketFactory, true);
    server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
    urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    URL url = new URL("https://android.com/foo");
    connection = urlFactory.open(url);
    connection.addRequestProperty("Private", "Secret");
    connection.addRequestProperty("Proxy-Authorization", "bar");
    connection.addRequestProperty("User-Agent", "baz");
    assertContent("encrypted response from the origin server", connection);
    RecordedRequest connect = server.takeRequest();
    assertNull(connect.getHeader("Private"));
    assertNull(connect.getHeader("Proxy-Authorization"));
    assertEquals(Version.userAgent(), connect.getHeader("User-Agent"));
    assertEquals("android.com:443", connect.getHeader("Host"));
    assertEquals("Keep-Alive", connect.getHeader("Proxy-Connection"));
    RecordedRequest get = server.takeRequest();
    assertEquals("Secret", get.getHeader("Private"));
    assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL) Test(org.junit.Test)

Example 32 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method proxyWithConnectionReuse.

@Test
public void proxyWithConnectionReuse() throws IOException {
    SSLSocketFactory socketFactory = sslClient.socketFactory;
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    server.useHttps(socketFactory, true);
    server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("response 1"));
    server.enqueue(new MockResponse().setBody("response 2"));
    urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    URL url = new URL("https://android.com/foo");
    assertContent("response 1", urlFactory.open(url));
    assertContent("response 2", urlFactory.open(url));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) Test(org.junit.Test)

Example 33 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method connectViaHttps.

@Test
public void connectViaHttps() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    connection = urlFactory.open(server.url("/foo").url());
    assertContent("this response comes via HTTPS", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 34 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method testConnectViaDirectProxyToHttps.

private void testConnectViaDirectProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    URL url = server.url("/foo").url();
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    connection = proxyConfig.connect(server, urlFactory, url);
    assertContent("this response comes via HTTPS", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL)

Example 35 with RecordingHostnameVerifier

use of okhttp3.RecordingHostnameVerifier in project okhttp by square.

the class URLConnectionTest method testConnectViaHttpProxyToHttps.

/**
   * We were verifying the wrong hostname when connecting to an HTTPS site through a proxy.
   * http://b/3097277
   */
private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    server.useHttps(sslClient.socketFactory, true);
    server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
    URL url = new URL("https://android.com/foo");
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
    connection = proxyConfig.connect(server, urlFactory, url);
    assertContent("this response comes via a secure proxy", connection);
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertEquals("android.com:443", connect.getHeader("Host"));
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertEquals("android.com", get.getHeader("Host"));
    assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL)

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