diff options
Diffstat (limited to 'luni/src/test/java/libcore')
-rw-r--r-- | luni/src/test/java/libcore/java/net/URLConnectionTest.java | 60 |
1 files changed, 54 insertions, 6 deletions
diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java index 960a1b7..cd0dc7b 100644 --- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java +++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java @@ -29,7 +29,10 @@ import java.net.HttpRetryException; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.PasswordAuthentication; +import java.net.Proxy; import java.net.ResponseCache; +import java.net.ServerSocket; +import java.net.Socket; import java.net.SocketTimeoutException; import java.net.URI; import java.net.URISyntaxException; @@ -510,6 +513,33 @@ public class URLConnectionTest extends junit.framework.TestCase { assertContent("abc", server.getUrl("/").openConnection()); } + public void testConnectViaHttpProxyToHttpsUsingProxyArgWithNoProxy() throws Exception { + testConnectViaDirectProxyToHttps(ProxyConfig.NO_PROXY); + } + + public void testConnectViaHttpProxyToHttpsUsingHttpProxySystemProperty() throws Exception { + // https should not use http proxy + testConnectViaDirectProxyToHttps(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY); + } + + private void testConnectViaDirectProxyToHttps(ProxyConfig proxyConfig) throws Exception { + TestSSLContext testSSLContext = TestSSLContext.create(); + + server.useHttps(testSSLContext.serverContext.getSocketFactory(), false); + server.enqueue(new MockResponse().setBody("this response comes via HTTPS")); + server.play(); + + URL url = server.getUrl("/foo"); + HttpsURLConnection connection = (HttpsURLConnection) proxyConfig.connect(server, url); + connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory()); + + assertContent("this response comes via HTTPS", connection); + + RecordedRequest request = server.takeRequest(); + assertEquals("GET /foo HTTP/1.1", request.getRequestLine()); + } + + public void testConnectViaHttpProxyToHttpsUsingProxyArg() throws Exception { testConnectViaHttpProxyToHttps(ProxyConfig.CREATE_ARG); } @@ -522,10 +552,6 @@ public class URLConnectionTest extends junit.framework.TestCase { testConnectViaHttpProxyToHttps(ProxyConfig.PROXY_SYSTEM_PROPERTY); } - public void testConnectViaHttpProxyToHttpsUsingHttpProxySystemProperty() throws Exception { - testConnectViaHttpProxyToHttps(ProxyConfig.HTTP_PROXY_SYSTEM_PROPERTY); - } - public void testConnectViaHttpProxyToHttpsUsingHttpsProxySystemProperty() throws Exception { testConnectViaHttpProxyToHttps(ProxyConfig.HTTPS_PROXY_SYSTEM_PROPERTY); } @@ -1258,14 +1284,28 @@ public class URLConnectionTest extends junit.framework.TestCase { } public void testConnectTimeouts() throws IOException { - // 10.0.0.0 is non-routable and will time out on every network - URLConnection urlConnection = new URL("http://10.0.0.0/").openConnection(); + // Set a backlog and use it up so that we can expect the + // URLConnection to properly timeout. According to Steven's + // 4.5 "listen function", linux adds 3 to the specified + // backlog, so we need to connect 4 times before it will hang. + ServerSocket serverSocket = new ServerSocket(0, 1); + int serverPort = serverSocket.getLocalPort(); + Socket[] sockets = new Socket[4]; + for (int i = 0; i < sockets.length; i++) { + sockets[i] = new Socket("localhost", serverPort); + } + + URLConnection urlConnection = new URL("http://localhost:" + serverPort).openConnection(); urlConnection.setConnectTimeout(1000); try { urlConnection.getInputStream(); fail(); } catch (SocketTimeoutException expected) { } + + for (Socket s : sockets) { + s.close(); + } } public void testReadTimeouts() throws IOException { @@ -1441,6 +1481,7 @@ public class URLConnectionTest extends junit.framework.TestCase { */ private void assertContent(String expected, URLConnection connection, int limit) throws IOException { + connection.connect(); assertEquals(expected, readAscii(connection.getInputStream(), limit)); ((HttpURLConnection) connection).disconnect(); } @@ -1499,6 +1540,13 @@ public class URLConnectionTest extends junit.framework.TestCase { } enum ProxyConfig { + NO_PROXY() { + @Override public HttpURLConnection connect(MockWebServer server, URL url) + throws IOException { + return (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); + } + }, + CREATE_ARG() { @Override public HttpURLConnection connect(MockWebServer server, URL url) throws IOException { |