test/jdk/java/net/httpclient/ProxyTest.java
branchhttp-client-branch
changeset 55764 34d7cc00f87a
parent 47216 71c04702a3d5
child 49765 ee6f7a61f3a5
child 56076 9a2855e0a796
--- a/test/jdk/java/net/httpclient/ProxyTest.java	Sun Nov 05 17:32:13 2017 +0000
+++ b/test/jdk/java/net/httpclient/ProxyTest.java	Sun Nov 05 21:19:55 2017 +0000
@@ -41,10 +41,12 @@
 import java.net.ProxySelector;
 import java.net.ServerSocket;
 import java.net.Socket;
+import java.net.SocketAddress;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
 import java.security.NoSuchAlgorithmException;
+import java.util.List;
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLContext;
@@ -121,6 +123,37 @@
         }
     }
 
+    /**
+     * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
+     * of times its select method has been invoked. This can be used to ensure
+     * that the Proxy Selector is invoked only once per HttpClient.sendXXX
+     * invocation.
+     */
+    static class CountingProxySelector extends ProxySelector {
+        private final ProxySelector proxySelector;
+        private volatile int count; // 0
+        private CountingProxySelector(InetSocketAddress proxyAddress) {
+            proxySelector = ProxySelector.of(proxyAddress);
+        }
+
+        public static CountingProxySelector of(InetSocketAddress proxyAddress) {
+            return new CountingProxySelector(proxyAddress);
+        }
+
+        int count() { return count; }
+
+        @Override
+        public List<Proxy> select(URI uri) {
+            count++;
+            return proxySelector.select(uri);
+        }
+
+        @Override
+        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
+            proxySelector.connectFailed(uri, sa, ioe);
+        }
+    }
+
     public static void test(HttpServer server, HttpClient.Version version)
             throws IOException,
             URISyntaxException,
@@ -158,7 +191,7 @@
             System.out.println("\nReal test begins here.");
             System.out.println("Setting up request with HttpClient for version: "
                     + version.name());
-            ProxySelector ps = ProxySelector.of(
+            CountingProxySelector ps = CountingProxySelector.of(
                     InetSocketAddress.createUnresolved("localhost", proxy.getAddress().getPort()));
             HttpClient client = HttpClient.newBuilder()
                 .version(version)
@@ -178,6 +211,9 @@
             if (!RESPONSE.equals(resp)) {
                 throw new AssertionError("Unexpected response");
             }
+            if (ps.count() > 1) {
+                throw new AssertionError("CountingProxySelector. Expected 1, got " + ps.count());
+            }
         } finally {
             System.out.println("Stopping proxy");
             proxy.stop();