use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class CallTest method noProactiveProxyAuthorization.
/**
* Confirm that we don't send the Proxy-Authorization header from the request to the proxy server.
* We used to have that behavior but it is problematic because unrelated requests end up sharing
* credentials. Worse, that approach leaks proxy credentials to the origin server.
*/
@Test
public void noProactiveProxyAuthorization() throws Exception {
server.useHttps(sslClient.socketFactory, true);
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()).hostnameVerifier(new RecordingHostnameVerifier()).build();
Request request = new Request.Builder().url("https://android.com/foo").header("Proxy-Authorization", "password").build();
Response response = client.newCall(request).execute();
assertEquals("response body", response.body().string());
RecordedRequest connect = server.takeRequest();
assertNull(connect.getHeader("Proxy-Authorization"));
RecordedRequest get = server.takeRequest();
assertEquals("password", get.getHeader("Proxy-Authorization"));
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method redirectedFromHttpsToHttpFollowingProtocolRedirects.
@Test
public void redirectedFromHttpsToHttpFollowingProtocolRedirects() throws Exception {
server2.enqueue(new MockResponse().setBody("This is insecure HTTP!"));
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/").url()).setBody("This page has moved!"));
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).followSslRedirects(true).build());
HttpsURLConnection connection = (HttpsURLConnection) urlFactory.open(server.url("/").url());
assertContent("This is insecure HTTP!", connection);
assertNull(connection.getCipherSuite());
assertNull(connection.getLocalCertificates());
assertNull(connection.getServerCertificates());
assertNull(connection.getPeerPrincipal());
assertNull(connection.getLocalPrincipal());
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method connectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache.
/** Tolerate bad https proxy response when using HttpResponseCache. Android bug 6754912. */
@Test
public void connectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache() throws Exception {
initResponseCache();
server.useHttps(sslClient.socketFactory, true);
// The inclusion of a body in the response to a CONNECT is key to reproducing b/6754912.
MockResponse badProxyResponse = new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).setBody("bogus proxy connect response content");
server.enqueue(badProxyResponse);
server.enqueue(new MockResponse().setBody("response"));
// Configure a single IP address for the host and a single configuration, so we only need one
// failure to fail permanently.
urlFactory.setClient(urlFactory.client().newBuilder().dns(new SingleInetAddressDns()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).connectionSpecs(Util.immutableList(ConnectionSpec.MODERN_TLS)).hostnameVerifier(new RecordingHostnameVerifier()).proxy(server.toProxyAddress()).build());
URL url = new URL("https://android.com/foo");
connection = urlFactory.open(url);
assertContent("response", connection);
RecordedRequest connect = server.takeRequest();
assertEquals("CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
assertEquals("android.com:443", connect.getHeader("Host"));
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method testSecureStreamingPost.
/**
* Users have reported problems using HTTPS with streaming request bodies.
* http://code.google.com/p/android/issues/detail?id=12860
*/
private void testSecureStreamingPost(StreamingMode streamingMode) throws Exception {
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("Success!"));
urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
connection = urlFactory.open(server.url("/").url());
connection.setDoOutput(true);
byte[] requestBody = { 'A', 'B', 'C', 'D' };
if (streamingMode == StreamingMode.FIXED_LENGTH) {
connection.setFixedLengthStreamingMode(requestBody.length);
} else if (streamingMode == StreamingMode.CHUNKED) {
connection.setChunkedStreamingMode(0);
}
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
assertEquals("Success!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest request = server.takeRequest();
assertEquals("POST / HTTP/1.1", request.getRequestLine());
if (streamingMode == StreamingMode.FIXED_LENGTH) {
assertEquals(Collections.<Integer>emptyList(), request.getChunkSizes());
} else if (streamingMode == StreamingMode.CHUNKED) {
assertEquals(Arrays.asList(4), request.getChunkSizes());
}
assertEquals("ABCD", request.getBody().readUtf8());
}
use of okhttp3.RecordingHostnameVerifier in project okhttp by square.
the class URLConnectionTest method connectViaHttpsReusingConnections.
private void connectViaHttpsReusingConnections(boolean rebuildClient) 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"));
// The pool will only reuse sockets if the SSL socket factories are the same.
SSLSocketFactory clientSocketFactory = sslClient.socketFactory;
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
CookieJar cookieJar = new JavaNetCookieJar(new CookieManager());
ConnectionPool connectionPool = new ConnectionPool();
urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
connection = urlFactory.open(server.url("/").url());
assertContent("this response comes via HTTPS", connection);
if (rebuildClient) {
urlFactory.setClient(new OkHttpClient.Builder().cache(cache).connectionPool(connectionPool).cookieJar(cookieJar).sslSocketFactory(clientSocketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build());
}
connection = urlFactory.open(server.url("/").url());
assertContent("another response via HTTPS", connection);
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
Aggregations