test/jdk/java/net/httpclient/SmokeTest.java
branchhttp-client-branch
changeset 55764 34d7cc00f87a
parent 55763 634d8e14c172
child 55807 2337a59be92a
equal deleted inserted replaced
55763:634d8e14c172 55764:34d7cc00f87a
    41 import com.sun.net.httpserver.HttpHandler;
    41 import com.sun.net.httpserver.HttpHandler;
    42 import com.sun.net.httpserver.HttpServer;
    42 import com.sun.net.httpserver.HttpServer;
    43 import com.sun.net.httpserver.HttpsConfigurator;
    43 import com.sun.net.httpserver.HttpsConfigurator;
    44 import com.sun.net.httpserver.HttpsParameters;
    44 import com.sun.net.httpserver.HttpsParameters;
    45 import com.sun.net.httpserver.HttpsServer;
    45 import com.sun.net.httpserver.HttpsServer;
       
    46 import java.net.Proxy;
       
    47 import java.net.SocketAddress;
    46 import java.util.concurrent.atomic.AtomicInteger;
    48 import java.util.concurrent.atomic.AtomicInteger;
    47 import java.net.InetSocketAddress;
    49 import java.net.InetSocketAddress;
    48 import java.net.PasswordAuthentication;
    50 import java.net.PasswordAuthentication;
    49 import java.net.ProxySelector;
    51 import java.net.ProxySelector;
    50 import java.net.URI;
    52 import java.net.URI;
   369         }
   371         }
   370         System.out.printf(" (count: %d) ", handler.count());
   372         System.out.printf(" (count: %d) ", handler.count());
   371         System.out.println(" OK");
   373         System.out.println(" OK");
   372     }
   374     }
   373 
   375 
       
   376     /**
       
   377      * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
       
   378      * of times its select method has been invoked. This can be used to ensure
       
   379      * that the Proxy Selector is invoked only once per HttpClient.sendXXX
       
   380      * invocation.
       
   381      */
       
   382     static class CountingProxySelector extends ProxySelector {
       
   383         private final ProxySelector proxySelector;
       
   384         private volatile int count; // 0
       
   385         private CountingProxySelector(InetSocketAddress proxyAddress) {
       
   386             proxySelector = ProxySelector.of(proxyAddress);
       
   387         }
       
   388 
       
   389         public static CountingProxySelector of(InetSocketAddress proxyAddress) {
       
   390             return new CountingProxySelector(proxyAddress);
       
   391         }
       
   392 
       
   393         int count() { return count; }
       
   394 
       
   395         @Override
       
   396         public List<Proxy> select(URI uri) {
       
   397             count++;
       
   398             return proxySelector.select(uri);
       
   399         }
       
   400 
       
   401         @Override
       
   402         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
       
   403             proxySelector.connectFailed(uri, sa, ioe);
       
   404         }
       
   405     }
       
   406 
   374     // Proxies
   407     // Proxies
   375     static void test4(String s) throws Exception {
   408     static void test4(String s) throws Exception {
   376         System.out.print("test4: " + s);
   409         System.out.print("test4: " + s);
   377         URI uri = new URI(s);
   410         URI uri = new URI(s);
   378         InetSocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", proxyPort);
   411         InetSocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", proxyPort);
   379         String filename = fileroot + uri.getPath();
   412         String filename = fileroot + uri.getPath();
   380 
   413 
   381         ExecutorService e = Executors.newCachedThreadPool();
   414         ExecutorService e = Executors.newCachedThreadPool();
   382 
   415 
       
   416         CountingProxySelector ps = CountingProxySelector.of(proxyAddr);
   383         HttpClient cl = HttpClient.newBuilder()
   417         HttpClient cl = HttpClient.newBuilder()
   384                                   .executor(e)
   418                                   .executor(e)
   385                                   .proxy(ProxySelector.of(proxyAddr))
   419                                   .proxy(ps)
   386                                   .sslContext(ctx)
   420                                   .sslContext(ctx)
   387                                   .sslParameters(sslparams)
   421                                   .sslParameters(sslparams)
   388                                   .build();
   422                                   .build();
   389 
   423 
   390         HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
   424         HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
   397         String fc = getFileContent(filename);
   431         String fc = getFileContent(filename);
   398 
   432 
   399         if (!body.equals(fc)) {
   433         if (!body.equals(fc)) {
   400             throw new RuntimeException(
   434             throw new RuntimeException(
   401                     "Body mismatch: expected [" + body + "], got [" + fc + "]");
   435                     "Body mismatch: expected [" + body + "], got [" + fc + "]");
       
   436         }
       
   437         if (ps.count() > 1) {
       
   438             throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count());
   402         }
   439         }
   403         e.shutdownNow();
   440         e.shutdownNow();
   404         System.out.println(" OK");
   441         System.out.println(" OK");
   405     }
   442     }
   406 
   443