use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method connectViaHttpsReusingConnectionsDifferentFactories.
@Test
public void connectViaHttpsReusingConnectionsDifferentFactories() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
// install a custom SSL socket factory so the server can be authorized
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
assertContent("this response comes via HTTPS", connection1);
SSLContext sslContext2 = SSLContext.getInstance("TLS");
sslContext2.init(null, null, null);
SSLSocketFactory sslSocketFactory2 = sslContext2.getSocketFactory();
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
X509TrustManager trustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslSocketFactory2, trustManager).build());
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
try {
readAscii(connection2.getInputStream(), Integer.MAX_VALUE);
fail("without an SSL socket factory, the connection should fail");
} catch (SSLException expected) {
}
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method testClientConfiguredGzipContentEncodingAndConnectionReuse.
/**
* Test a bug where gzip input streams weren't exhausting the input stream, which corrupted the
* request that followed or prevented connection reuse.
* http://code.google.com/p/android/issues/detail?id=7059
* http://code.google.com/p/android/issues/detail?id=38817
*/
private void testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind transferKind, boolean tls) throws Exception {
if (tls) {
SSLSocketFactory socketFactory = sslClient.socketFactory;
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
server.useHttps(socketFactory, false);
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
}
MockResponse responseOne = new MockResponse();
responseOne.addHeader("Content-Encoding: gzip");
transferKind.setBody(responseOne, gzip("one (gzipped)"), 5);
server.enqueue(responseOne);
MockResponse responseTwo = new MockResponse();
transferKind.setBody(responseTwo, "two (identity)", 5);
server.enqueue(responseTwo);
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
connection1.addRequestProperty("Accept-Encoding", "gzip");
InputStream gunzippedIn = new GZIPInputStream(connection1.getInputStream());
assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
assertEquals(0, server.takeRequest().getSequenceNumber());
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals("two (identity)", readAscii(connection2.getInputStream(), Integer.MAX_VALUE));
assertEquals(1, server.takeRequest().getSequenceNumber());
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method testConnectViaSocketFactory.
public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
SocketFactory uselessSocketFactory = new SocketFactory() {
public Socket createSocket() {
throw new IllegalArgumentException("useless");
}
public Socket createSocket(InetAddress host, int port) {
return null;
}
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {
return null;
}
public Socket createSocket(String host, int port) {
return null;
}
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) {
return null;
}
};
if (useHttps) {
server.useHttps(sslClient.socketFactory, false);
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
}
server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(uselessSocketFactory).build());
connection = urlFactory.open(server.url("/").url());
try {
connection.getResponseCode();
fail();
} catch (IllegalArgumentException expected) {
}
urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(SocketFactory.getDefault()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals(200, connection.getResponseCode());
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method connectViaHttpsWithSSLFallback.
// TODO(jwilson): tests below this marker need to be migrated to OkHttp's request/response API.
@Test
public void connectViaHttpsWithSSLFallback() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setBody("this response comes via SSL"));
urlFactory.setClient(urlFactory.client().newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build());
connection = urlFactory.open(server.url("/foo").url());
assertContent("this response comes via SSL", connection);
RecordedRequest failHandshakeRequest = server.takeRequest();
assertNull(failHandshakeRequest.getRequestLine());
RecordedRequest fallbackRequest = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", fallbackRequest.getRequestLine());
assertEquals(TlsVersion.TLS_1_0, fallbackRequest.getTlsVersion());
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method redirectedOnHttps.
@Test
public void redirectedOnHttps() throws IOException, InterruptedException {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: /foo").setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("This is the new location!"));
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
connection = urlFactory.open(server.url("/").url());
assertEquals("This is the new location!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest first = server.takeRequest();
assertEquals("GET / HTTP/1.1", first.getRequestLine());
RecordedRequest retry = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", retry.getRequestLine());
assertEquals("Expected connection reuse", 1, retry.getSequenceNumber());
}
Aggregations