use of libcore.javax.net.ssl.TestSSLContext 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.*");
}
use of libcore.javax.net.ssl.TestSSLContext 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());
}
use of libcore.javax.net.ssl.TestSSLContext in project robovm by robovm.
the class URLConnectionTest method testHttpsWithCustomTrustManager.
public void testHttpsWithCustomTrustManager() throws Exception {
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
RecordingTrustManager trustManager = new RecordingTrustManager();
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom());
HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
try {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().setBody("GHI"));
server.play();
URL url = server.getUrl("/");
assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls);
assertEquals(Arrays.asList("checkServerTrusted [" + "CN=" + hostName + " 1, " + "CN=Test Intermediate Certificate Authority 1, " + "CN=Test Root Certificate Authority 1" + "] RSA"), trustManager.calls);
} finally {
HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
}
}
use of libcore.javax.net.ssl.TestSSLContext in project android_frameworks_base by crdroidandroid.
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");
}
use of libcore.javax.net.ssl.TestSSLContext in project android_frameworks_base by ParanoidAndroid.
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");
}
Aggregations