Search in sources :

Example 11 with TestSSLContext

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

the class URLConnectionTest method testSecureStreamingPost.

/**
     * Users have reported problems using HTTPS with streaming request bodies.
     * http://code.google.com/p/android/issues/detail?id=12860
     */
private void testSecureStreamingPost(StreamingMode streamingMode) throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("Success!"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setDoOutput(true);
    byte[] requestBody = { 'A', 'B', 'C', 'D' };
    if (streamingMode == StreamingMode.FIXED_LENGTH) {
        connection.setFixedLengthStreamingMode(requestBody.length);
    } else if (streamingMode == StreamingMode.CHUNKED) {
        connection.setChunkedStreamingMode(0);
    }
    OutputStream outputStream = connection.getOutputStream();
    outputStream.write(requestBody);
    outputStream.close();
    assertEquals("Success!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
    RecordedRequest request = server.takeRequest();
    assertEquals("POST / HTTP/1.1", request.getRequestLine());
    if (streamingMode == StreamingMode.FIXED_LENGTH) {
        assertEquals(Collections.<Integer>emptyList(), request.getChunkSizes());
    } else if (streamingMode == StreamingMode.CHUNKED) {
        assertEquals(Arrays.asList(4), request.getChunkSizes());
    }
    assertEquals(Arrays.toString(requestBody), Arrays.toString(request.getBody()));
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 12 with TestSSLContext

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

the class AbstractProxyTest method testConnectToHttps.

public void testConnectToHttps() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setResponseCode(200).setBody("this response comes via HTTPS"));
    server.play();
    HttpClient httpClient = newHttpClient();
    SSLSocketFactory sslSocketFactory = newSslSocketFactory(testSSLContext);
    sslSocketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
    httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, server.getPort()));
    HttpResponse response = httpClient.execute(new HttpGet("https://localhost:" + server.getPort() + "/foo"));
    assertEquals("this response comes via HTTPS", contentToString(response));
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
}
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 13 with TestSSLContext

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

the class URLConnectionTest method testNonRetryableRequestBodyAfterBrokenConnection.

public void testNonRetryableRequestBodyAfterBrokenConnection() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(DISCONNECT_AFTER_READING_REQUEST));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/a").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setDoOutput(true);
    connection.setFixedLengthStreamingMode(3);
    OutputStream out = connection.getOutputStream();
    out.write(new byte[] { 1, 2, 3 });
    out.close();
    try {
        connection.getInputStream();
        fail();
    } catch (IOException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) IOException(java.io.IOException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 14 with TestSSLContext

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

the class URLConnectionTest method testConnectViaHttpsReusingConnectionsDifferentFactories.

public void testConnectViaHttpsReusingConnectionsDifferentFactories() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.enqueue(new MockResponse().setBody("another response via HTTPS"));
    server.play();
    // install a custom SSL socket factory so the server can be authorized
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via HTTPS", connection);
    connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    try {
        readAscii(connection.getInputStream(), Integer.MAX_VALUE);
        fail("without an SSL socket factory, the connection should fail");
    } catch (SSLException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) SSLException(javax.net.ssl.SSLException) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 15 with TestSSLContext

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

the class URLConnectionTest method testInspectSslBeforeConnect.

public void testInspectSslBeforeConnect() throws Exception {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse());
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertNotNull(connection.getHostnameVerifier());
    try {
        connection.getLocalCertificates();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.getServerCertificates();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.getCipherSuite();
        fail();
    } catch (IllegalStateException expected) {
    }
    try {
        connection.getPeerPrincipal();
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) 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