Search in sources :

Example 31 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection 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 32 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection 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 33 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection 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)

Example 34 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testProxyAuthenticateOnConnect.

public void testProxyAuthenticateOnConnect() throws Exception {
    Authenticator.setDefault(new SimpleAuthenticator());
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), 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("A"));
    server.play();
    URL url = new URL("https://android.com/foo");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(server.toProxyAddress());
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    connection.setHostnameVerifier(new RecordingHostnameVerifier());
    assertContent("A", connection);
    RecordedRequest connect1 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
    assertContainsNoneMatching(connect1.getHeaders(), "Proxy\\-Authorization.*");
    RecordedRequest connect2 = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
    assertContains(connect2.getHeaders(), "Proxy-Authorization: Basic " + SimpleAuthenticator.BASE_64_CREDENTIALS);
    RecordedRequest get = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
    assertContainsNoneMatching(get.getHeaders(), "Proxy\\-Authorization.*");
}
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 35 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project robovm by robovm.

the class URLConnectionTest method testConnectViaHttps.

public void testConnectViaHttps() throws IOException, InterruptedException {
    TestSSLContext testSSLContext = TestSSLContext.create();
    server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
    server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
    server.play();
    HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/foo").openConnection();
    connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
    assertContent("this response comes via HTTPS", connection);
    RecordedRequest request = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
    assertEquals("TLSv1", request.getSslProtocol());
}
Also used : RecordedRequest(com.google.mockwebserver.RecordedRequest) MockResponse(com.google.mockwebserver.MockResponse) TestSSLContext(libcore.javax.net.ssl.TestSSLContext) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)209 URL (java.net.URL)113 HttpURLConnection (java.net.HttpURLConnection)51 IOException (java.io.IOException)50 Test (org.junit.Test)39 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)29 InputStream (java.io.InputStream)27 HostnameVerifier (javax.net.ssl.HostnameVerifier)23 SSLContext (javax.net.ssl.SSLContext)23 OutputStream (java.io.OutputStream)20 MockResponse (com.google.mockwebserver.MockResponse)19 InputStreamReader (java.io.InputStreamReader)19 TestSSLContext (libcore.javax.net.ssl.TestSSLContext)19 URLConnection (java.net.URLConnection)17 BufferedReader (java.io.BufferedReader)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 MalformedURLException (java.net.MalformedURLException)13 Proxy (java.net.Proxy)13 MockResponse (okhttp3.mockwebserver.MockResponse)13