test/jdk/java/net/httpclient/ProxyTest.java
branchhttp-client-branch
changeset 55764 34d7cc00f87a
parent 47216 71c04702a3d5
child 49765 ee6f7a61f3a5
child 56076 9a2855e0a796
equal deleted inserted replaced
55763:634d8e14c172 55764:34d7cc00f87a
    39 import java.net.InetSocketAddress;
    39 import java.net.InetSocketAddress;
    40 import java.net.Proxy;
    40 import java.net.Proxy;
    41 import java.net.ProxySelector;
    41 import java.net.ProxySelector;
    42 import java.net.ServerSocket;
    42 import java.net.ServerSocket;
    43 import java.net.Socket;
    43 import java.net.Socket;
       
    44 import java.net.SocketAddress;
    44 import java.net.URI;
    45 import java.net.URI;
    45 import java.net.URISyntaxException;
    46 import java.net.URISyntaxException;
    46 import java.nio.charset.StandardCharsets;
    47 import java.nio.charset.StandardCharsets;
    47 import java.security.NoSuchAlgorithmException;
    48 import java.security.NoSuchAlgorithmException;
       
    49 import java.util.List;
    48 import javax.net.ssl.HostnameVerifier;
    50 import javax.net.ssl.HostnameVerifier;
    49 import javax.net.ssl.HttpsURLConnection;
    51 import javax.net.ssl.HttpsURLConnection;
    50 import javax.net.ssl.SSLContext;
    52 import javax.net.ssl.SSLContext;
    51 import javax.net.ssl.SSLSession;
    53 import javax.net.ssl.SSLSession;
    52 import jdk.incubator.http.HttpClient;
    54 import jdk.incubator.http.HttpClient;
   119             server.stop(0);
   121             server.stop(0);
   120             System.out.println("Server stopped");
   122             System.out.println("Server stopped");
   121         }
   123         }
   122     }
   124     }
   123 
   125 
       
   126     /**
       
   127      * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
       
   128      * of times its select method has been invoked. This can be used to ensure
       
   129      * that the Proxy Selector is invoked only once per HttpClient.sendXXX
       
   130      * invocation.
       
   131      */
       
   132     static class CountingProxySelector extends ProxySelector {
       
   133         private final ProxySelector proxySelector;
       
   134         private volatile int count; // 0
       
   135         private CountingProxySelector(InetSocketAddress proxyAddress) {
       
   136             proxySelector = ProxySelector.of(proxyAddress);
       
   137         }
       
   138 
       
   139         public static CountingProxySelector of(InetSocketAddress proxyAddress) {
       
   140             return new CountingProxySelector(proxyAddress);
       
   141         }
       
   142 
       
   143         int count() { return count; }
       
   144 
       
   145         @Override
       
   146         public List<Proxy> select(URI uri) {
       
   147             count++;
       
   148             return proxySelector.select(uri);
       
   149         }
       
   150 
       
   151         @Override
       
   152         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
       
   153             proxySelector.connectFailed(uri, sa, ioe);
       
   154         }
       
   155     }
       
   156 
   124     public static void test(HttpServer server, HttpClient.Version version)
   157     public static void test(HttpServer server, HttpClient.Version version)
   125             throws IOException,
   158             throws IOException,
   126             URISyntaxException,
   159             URISyntaxException,
   127             NoSuchAlgorithmException,
   160             NoSuchAlgorithmException,
   128             InterruptedException
   161             InterruptedException
   156             }
   189             }
   157             System.out.println("Communication with proxy OK");
   190             System.out.println("Communication with proxy OK");
   158             System.out.println("\nReal test begins here.");
   191             System.out.println("\nReal test begins here.");
   159             System.out.println("Setting up request with HttpClient for version: "
   192             System.out.println("Setting up request with HttpClient for version: "
   160                     + version.name());
   193                     + version.name());
   161             ProxySelector ps = ProxySelector.of(
   194             CountingProxySelector ps = CountingProxySelector.of(
   162                     InetSocketAddress.createUnresolved("localhost", proxy.getAddress().getPort()));
   195                     InetSocketAddress.createUnresolved("localhost", proxy.getAddress().getPort()));
   163             HttpClient client = HttpClient.newBuilder()
   196             HttpClient client = HttpClient.newBuilder()
   164                 .version(version)
   197                 .version(version)
   165                 .proxy(ps)
   198                 .proxy(ps)
   166                 .build();
   199                 .build();
   175             System.out.println("Got response");
   208             System.out.println("Got response");
   176             String resp = response.body();
   209             String resp = response.body();
   177             System.out.println("Received: " + resp);
   210             System.out.println("Received: " + resp);
   178             if (!RESPONSE.equals(resp)) {
   211             if (!RESPONSE.equals(resp)) {
   179                 throw new AssertionError("Unexpected response");
   212                 throw new AssertionError("Unexpected response");
       
   213             }
       
   214             if (ps.count() > 1) {
       
   215                 throw new AssertionError("CountingProxySelector. Expected 1, got " + ps.count());
   180             }
   216             }
   181         } finally {
   217         } finally {
   182             System.out.println("Stopping proxy");
   218             System.out.println("Stopping proxy");
   183             proxy.stop();
   219             proxy.stop();
   184             System.out.println("Proxy stopped");
   220             System.out.println("Proxy stopped");