use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method httpsWithCustomTrustManager.
@Test
public void httpsWithCustomTrustManager() throws Exception {
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
RecordingTrustManager trustManager = new RecordingTrustManager(sslClient.trustManager);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { trustManager }, null);
urlFactory.setClient(urlFactory.client().newBuilder().hostnameVerifier(hostnameVerifier).sslSocketFactory(sslContext.getSocketFactory(), trustManager).build());
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().setBody("GHI"));
URL url = server.url("/").url();
assertContent("ABC", urlFactory.open(url));
assertContent("DEF", urlFactory.open(url));
assertContent("GHI", urlFactory.open(url));
assertEquals(Arrays.asList("verify " + server.getHostName()), hostnameVerifier.calls);
assertEquals(Arrays.asList("checkServerTrusted [CN=" + server.getHostName() + " 1]"), trustManager.calls);
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method proxyWithConnectionClose.
// Don't disconnect after building a tunnel with CONNECT
// http://code.google.com/p/android/issues/detail?id=37221
@Test
public void proxyWithConnectionClose() throws IOException {
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("this response comes via a proxy"));
urlFactory.setClient(urlFactory.client().newBuilder().proxy(server.toProxyAddress()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
URL url = new URL("https://android.com/foo");
connection = urlFactory.open(url);
connection.setRequestProperty("Connection", "close");
assertContent("this response comes via a proxy", connection);
}
use of okhttp3.RecordingHostnameVerifier 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 okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method proxyAuthenticateOnConnect.
@Test
public void proxyAuthenticateOnConnect() throws Exception {
Authenticator.setDefault(new RecordingAuthenticator());
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\""));
server.enqueue(new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("A"));
urlFactory.setClient(urlFactory.client().newBuilder().proxyAuthenticator(new JavaNetAuthenticator()).proxy(server.toProxyAddress()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
URL url = new URL("https://android.com/foo");
connection = urlFactory.open(url);
assertContent("A", connection);
RecordedRequest connect1 = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect1.getRequestLine());
assertNull(connect1.getHeader("Proxy-Authorization"));
RecordedRequest connect2 = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect2.getRequestLine());
assertEquals("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS, connect2.getHeader("Proxy-Authorization"));
RecordedRequest get = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", get.getRequestLine());
assertNull(get.getHeader("Proxy-Authorization"));
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method tooManyProxyAuthFailuresWithConnectionClose.
@Test
public void tooManyProxyAuthFailuresWithConnectionClose() throws IOException {
server.useHttps(sslClient.socketFactory, true);
server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
for (int i = 0; i < 21; i++) {
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
}
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).proxyAuthenticator(new RecordingOkAuthenticator("password")).hostnameVerifier(new RecordingHostnameVerifier()).build();
Request request = new Request.Builder().url("https://android.com/foo").build();
try {
client.newCall(request).execute();
fail();
} catch (ProtocolException expected) {
}
}
Aggregations