Search in sources :

Example 26 with TestSSLContext

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

the class URLConnectionTest method testNotRedirectedFromHttpsToHttp.

public void testNotRedirectedFromHttpsToHttp() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: http://anyhost/foo").setBody("This page has moved!"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertEquals("This page has moved!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 27 with TestSSLContext

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

the class URLConnectionTest method testConnectViaHttpsToUntrustedServer.

/**
     * Verify that we don't retry connections on certificate verification errors.
     *
     * http://code.google.com/p/android/issues/detail?id=13178
     */
public void testConnectViaHttpsToUntrustedServer() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create(TestKeyStore.getClientCA2(), TestKeyStore.getServer());
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    // unused
    server.enqueue(new MockResponse());
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    try {
        connection.getInputStream();
        fail();
    } catch (SSLHandshakeException expected) {
        assertTrue(expected.getCause() instanceof CertificateException);
    }
    assertEquals(0, server.getRequestCount());
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) CertificateException(java.security.cert.CertificateException) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 28 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project platform_frameworks_base by android.

the class AbstractProxyTest method testConnectViaHttpProxyToHttps.

private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via a secure proxy"));
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpProxyClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443));
    HttpGet request = new HttpGet("https://android.com/foo");
    proxyConfig.configure(server, httpProxyClient, request);
    HttpResponse response = httpProxyClient.execute(request);
    assertEquals("this response comes via a secure proxy", contentToString(response));
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy " + proxyConfig, "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");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 29 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project android_frameworks_base by AOSPA.

the class AbstractProxyTest method testConnectViaHttpProxyToHttps.

private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via a secure proxy"));
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpProxyClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443));
    HttpGet request = new HttpGet("https://android.com/foo");
    proxyConfig.configure(server, httpProxyClient, request);
    HttpResponse response = httpProxyClient.execute(request);
    assertEquals("this response comes via a secure proxy", contentToString(response));
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy " + proxyConfig, "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");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

Example 30 with TestSSLContext

use of libcore.javax.net.ssl.TestSSLContext in project android_frameworks_base by DirtyUnicorns.

the class AbstractProxyTest method testConnectViaHttpProxyToHttps.

private void testConnectViaHttpProxyToHttps(ProxyConfig proxyConfig) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), true);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via a secure proxy"));
    server.play();
    HttpClient httpProxyClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpProxyClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443));
    HttpGet request = new HttpGet("https://android.com/foo");
    proxyConfig.configure(server, httpProxyClient, request);
    HttpResponse response = httpProxyClient.execute(request);
    assertEquals("this response comes via a secure proxy", contentToString(response));
    RecordedRequest connect = server.takeRequest();
    assertEquals("Connect line failure on proxy " + proxyConfig, "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");
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) Scheme(org.apache.http.conn.scheme.Scheme) AllowAllHostnameVerifier(org.apache.http.conn.ssl.AllowAllHostnameVerifier) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory)

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