diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2014-07-23 13:26:32 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-07-23 04:41:25 +0000 |
commit | eff66cb9d6efa2196785be41b2b6e7c8fadbe398 (patch) | |
tree | a156bf3baa14c1c19431c91b1f1fa538e9aed49c | |
parent | 993d42040f273dd7bc20038fbf068cb07a06a3cc (diff) | |
parent | a83ddf194ffbae04ce536967efff0ec72df70e10 (diff) | |
download | okhttp-eff66cb9d6efa2196785be41b2b6e7c8fadbe398.tar.gz |
Merge "Use the socket factory for direct connections as well."
-rw-r--r-- | okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java | 44 | ||||
-rw-r--r-- | okhttp/src/main/java/com/squareup/okhttp/Connection.java | 6 |
2 files changed, 47 insertions, 3 deletions
diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java index de3bd1b..2280e83 100644 --- a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java +++ b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/URLConnectionTest.java @@ -45,6 +45,7 @@ import java.net.ProtocolException; import java.net.Proxy; import java.net.ProxySelector; import java.net.ResponseCache; +import java.net.Socket; import java.net.SocketAddress; import java.net.SocketTimeoutException; import java.net.URI; @@ -66,6 +67,7 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicBoolean; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; +import javax.net.SocketFactory; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; @@ -706,6 +708,48 @@ public final class URLConnectionTest { assertContent("abc", client.open(server.getUrl("/"))); } + 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(sslContext.getSocketFactory(), false); + client.setSslSocketFactory(sslContext.getSocketFactory()); + client.setHostnameVerifier(new RecordingHostnameVerifier()); + } + + server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK")); + server.play(); + + client.setSocketFactory(uselessSocketFactory); + connection = client.open(server.getUrl("/")); + try { + connection.getResponseCode(); + fail(); + } catch (IllegalArgumentException expected) { + } + + client.setSocketFactory(SocketFactory.getDefault()); + connection = client.open(server.getUrl("/")); + assertEquals(200, connection.getResponseCode()); + } + + @Test public void connectHttpViaSocketFactory() throws Exception { + testConnectViaSocketFactory(false); + } + + @Test public void connectHttpsViaSocketFactory() throws Exception { + testConnectViaSocketFactory(true); + } + @Test public void contentDisagreesWithChunkedHeader() throws IOException { MockResponse mockResponse = new MockResponse(); mockResponse.setChunkedBody("abc", 3); diff --git a/okhttp/src/main/java/com/squareup/okhttp/Connection.java b/okhttp/src/main/java/com/squareup/okhttp/Connection.java index 9080ae8..39ca4d0 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/Connection.java +++ b/okhttp/src/main/java/com/squareup/okhttp/Connection.java @@ -138,10 +138,10 @@ public final class Connection implements Closeable { throws IOException { if (connected) throw new IllegalStateException("already connected"); - if (route.proxy.type() != Proxy.Type.HTTP) { - socket = new Socket(route.proxy); - } else { + if (route.proxy.type() == Proxy.Type.DIRECT || route.proxy.type() == Proxy.Type.HTTP) { socket = route.address.socketFactory.createSocket(); + } else { + socket = new Socket(route.proxy); } socket.setSoTimeout(readTimeout); |