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());
}
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) {
}
}
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) {
}
}
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) {
}
}
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) {
}
}
Aggregations