Search in sources :

Example 51 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project okhttp by square.

the class URLConnectionTest method inspectHandshakeThroughoutRequestLifecycle.

@Test
public void inspectHandshakeThroughoutRequestLifecycle() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse());
    urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    HttpsURLConnection httpsConnection = (HttpsURLConnection) urlFactory.open(server.url("/foo").url());
    // Prior to calling connect(), getting the cipher suite is forbidden.
    try {
        httpsConnection.getCipherSuite();
        fail();
    } catch (IllegalStateException expected) {
    }
    // Calling connect establishes a handshake...
    httpsConnection.connect();
    assertNotNull(httpsConnection.getCipherSuite());
    // ...which remains after we read the response body...
    assertContent("", httpsConnection);
    assertNotNull(httpsConnection.getCipherSuite());
    // ...and after we disconnect.
    httpsConnection.disconnect();
    assertNotNull(httpsConnection.getCipherSuite());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 52 with HttpsURLConnection

use of javax.net.ssl.HttpsURLConnection in project okhttp by square.

the class OkUrlFactoryTest method testURLFilterRedirect.

@Test
public void testURLFilterRedirect() throws Exception {
    MockWebServer cleartextServer = new MockWebServer();
    cleartextServer.enqueue(new MockResponse().setBody("Blocked!"));
    final URL blockedURL = cleartextServer.url("/").url();
    SslClient contextBuilder = SslClient.localhost();
    server.useHttps(contextBuilder.socketFactory, false);
    factory.setClient(factory.client().newBuilder().sslSocketFactory(contextBuilder.socketFactory, contextBuilder.trustManager).followSslRedirects(true).build());
    factory.setUrlFilter(new URLFilter() {

        @Override
        public void checkURLPermitted(URL url) throws IOException {
            if (blockedURL.equals(url)) {
                throw new IOException("Blocked");
            }
        }
    });
    server.enqueue(new MockResponse().setResponseCode(302).addHeader("Location: " + blockedURL).setBody("This page has moved"));
    URL destination = server.url("/").url();
    try {
        HttpsURLConnection httpsConnection = (HttpsURLConnection) factory.open(destination);
        httpsConnection.getInputStream();
        fail("Connection was successful");
    } catch (IOException expected) {
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SslClient(okhttp3.internal.tls.SslClient) URLFilter(okhttp3.internal.URLFilter) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 53 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 54 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 55 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)

Aggregations

HttpsURLConnection (javax.net.ssl.HttpsURLConnection)522 URL (java.net.URL)310 IOException (java.io.IOException)177 HttpURLConnection (java.net.HttpURLConnection)128 InputStreamReader (java.io.InputStreamReader)93 InputStream (java.io.InputStream)89 Test (org.junit.Test)83 BufferedReader (java.io.BufferedReader)78 SSLContext (javax.net.ssl.SSLContext)70 OutputStream (java.io.OutputStream)54 HostnameVerifier (javax.net.ssl.HostnameVerifier)50 MalformedURLException (java.net.MalformedURLException)48 URLConnection (java.net.URLConnection)47 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 HashMap (java.util.HashMap)34 DataOutputStream (java.io.DataOutputStream)32 KeyManagementException (java.security.KeyManagementException)32 JSONObject (org.json.JSONObject)29