Search in sources :

Example 6 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.

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 {
    TestSSLContext testSSLContext = TestSSLContext.create();
    RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setBody("this response comes via a secure proxy"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(hostnameVerifier);
    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());
    assertContains(connect.getHeaders(), "Host: android.com");
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContains(get.getHeaders(), "Host: android.com");
    assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 7 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttpsWithSSLFallback.

public void testConnectViaHttpsWithSSLFallback() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setSocketPolicy(DISCONNECT_AT_START));
    server.enqueue(new MockResponse().setBody("this response comes via SSL"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via SSL", connection);
    // The first request will be an incomplete (bookkeeping) request
    // that the server disconnected from at start.
    server.takeRequest();
    // The request will be retried.
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 8 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.

the class URLConnectionTest method testSslFallback.

public void testSslFallback() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("This required a 2nd handshake"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertEquals("This required a 2nd handshake", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest first = server.takeRequest();
    assertEquals(0, first.getSequenceNumber());
    RecordedRequest retry = server.takeRequest();
    assertEquals(0, retry.getSequenceNumber());
    assertEquals("SSLv3", retry.getSslProtocol());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 9 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache.

/**
     * Tolerate bad https proxy response when using HttpResponseCache. http://b/6754912
     */
public void testConnectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache() throws Exception {
    ProxyConfig proxyConfig = ProxyConfig.PROXY_SYSTEM_PROPERTY;
    TestSSLContext testSSLContext = TestSSLContext.create();
    initResponseCache();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    MockResponse badProxyResponse = new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders().setBody(// Key to reproducing b/6754912
    "bogus proxy connect response content");
    // We enqueue the bad response twice because the connection will
    // be retried with TLS_MODE_COMPATIBLE after the first connection
    // fails.
    server.enqueue(badProxyResponse);
    server.enqueue(badProxyResponse);
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    try {
        connection.connect();
        fail();
    } catch (SSLHandshakeException expected) {
    // Thrown when the connect causes SSLSocket.startHandshake() to throw
    // when it sees the "bogus proxy connect response content"
    // instead of a ServerHello handshake message.
    }
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy", "CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertContains(connect.getHeaders(), "Host: android.com");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 10 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.

the class URLConnectionTest method testConnectViaDirectProxyToHttps.

private void testConnectViaDirectProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.play();
    URL url = server.getUrl("/foo");
    HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url);
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via HTTPS", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

MockResponse (com.google.mockwebserver.MockResponse)32 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)32 RecordedRequest (com.google.mockwebserver.RecordedRequest)22 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)19 HttpResponse (org.apache.http.HttpResponse)12 HttpClient (org.apache.http.client.HttpClient)12 HttpGet (org.apache.http.client.methods.HttpGet)12 Scheme (org.apache.http.conn.scheme.Scheme)12 AllowAllHostnameVerifier (org.apache.http.conn.ssl.AllowAllHostnameVerifier)12 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)12 URL (java.net.URL)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 GZIPOutputStream (java.util.zip.GZIPOutputStream)3 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLContext (javax.net.ssl.SSLContext)1