use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method recoverFromTlsHandshakeFailure_Async.
@Test
public void recoverFromTlsHandshakeFailure_Async() throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
server.enqueue(new MockResponse().setBody("abc"));
client = client.newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
Request request = new Request.Builder().url(server.url("/")).build();
client.newCall(request).enqueue(callback);
callback.await(request.url()).assertBody("abc");
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method noRecoveryFromTlsHandshakeFailureWhenTlsFallbackIsDisabled.
@Test
public void noRecoveryFromTlsHandshakeFailureWhenTlsFallbackIsDisabled() throws Exception {
client = client.newBuilder().connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT)).hostnameVerifier(new RecordingHostnameVerifier()).dns(new SingleInetAddressDns()).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (SSLProtocolException expected) {
// RI response to the FAIL_HANDSHAKE
} catch (SSLHandshakeException expected) {
// Android's response to the FAIL_HANDSHAKE
}
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method recoverFromTlsHandshakeFailure_tlsFallbackScsvEnabled.
@Test
public void recoverFromTlsHandshakeFailure_tlsFallbackScsvEnabled() throws Exception {
final String tlsFallbackScsv = "TLS_FALLBACK_SCSV";
List<String> supportedCiphers = Arrays.asList(sslClient.socketFactory.getSupportedCipherSuites());
if (!supportedCiphers.contains(tlsFallbackScsv)) {
// This only works if the client socket supports TLS_FALLBACK_SCSV.
return;
}
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
RecordingSSLSocketFactory clientSocketFactory = new RecordingSSLSocketFactory(sslClient.socketFactory);
client = client.newBuilder().sslSocketFactory(clientSocketFactory, sslClient.trustManager).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).hostnameVerifier(new RecordingHostnameVerifier()).dns(new SingleInetAddressDns()).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (SSLHandshakeException expected) {
}
List<SSLSocket> clientSockets = clientSocketFactory.getSocketsCreated();
SSLSocket firstSocket = clientSockets.get(0);
assertFalse(Arrays.asList(firstSocket.getEnabledCipherSuites()).contains(tlsFallbackScsv));
SSLSocket secondSocket = clientSockets.get(1);
assertTrue(Arrays.asList(secondSocket.getEnabledCipherSuites()).contains(tlsFallbackScsv));
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method proxyConnectOmitsApplicationHeaders.
/** Test which headers are sent unencrypted to the HTTP proxy. */
@Test
public void proxyConnectOmitsApplicationHeaders() throws Exception {
server.useHttps(sslClient.socketFactory, true);
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("encrypted response from the origin server"));
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).proxy(server.toProxyAddress()).hostnameVerifier(hostnameVerifier).build();
Request request = new Request.Builder().url("https://android.com/foo").header("Private", "Secret").header("User-Agent", "App 1.0").build();
Response response = client.newCall(request).execute();
assertEquals("encrypted response from the origin server", response.body().string());
RecordedRequest connect = server.takeRequest();
assertNull(connect.getHeader("Private"));
assertEquals(Version.userAgent(), connect.getHeader("User-Agent"));
assertEquals("Keep-Alive", connect.getHeader("Proxy-Connection"));
assertEquals("android.com:443", connect.getHeader("Host"));
RecordedRequest get = server.takeRequest();
assertEquals("Secret", get.getHeader("Private"));
assertEquals("App 1.0", get.getHeader("User-Agent"));
assertEquals(Arrays.asList("verify android.com"), hostnameVerifier.calls);
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method proxyAuthenticateOnConnectWithConnectionClose.
/**
* OkHttp has a bug where a `Connection: close` response header is not honored when establishing a
* TLS tunnel. https://github.com/square/okhttp/issues/2426
*/
@Test
public void proxyAuthenticateOnConnectWithConnectionClose() throws Exception {
server.useHttps(sslClient.socketFactory, true);
server.setProtocols(Collections.singletonList(Protocol.HTTP_1_1));
server.enqueue(new MockResponse().setResponseCode(407).addHeader("Proxy-Authenticate: Basic realm=\"localhost\"").addHeader("Connection: close"));
server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
server.enqueue(new MockResponse().setBody("response body"));
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();
Response response = client.newCall(request).execute();
assertEquals("response body", response.body().string());
// First CONNECT call needs a new connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Second CONNECT call needs a new connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// GET reuses the connection from the second connect.
assertEquals(1, server.takeRequest().getSequenceNumber());
}
Aggregations