test/jdk/java/net/httpclient/ProxyAuthTest.java
branchhttp-client-branch
changeset 55764 34d7cc00f87a
parent 47216 71c04702a3d5
child 49765 ee6f7a61f3a5
child 56082 1da51fab3032
equal deleted inserted replaced
55763:634d8e14c172 55764:34d7cc00f87a
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27  * @test
    27  * @test
    28  * @bug 8163561
    28  * @bug 8163561
    29  * @modules java.base/sun.net.www
    29  * @modules java.base/sun.net.www
    30  *          jdk.incubator.httpclient
    30  *          jdk.incubator.httpclient
    31  * @summary Verify that Proxy-Authenticate header is correctly handled
    31  * @summary Verify that Proxy-Authenticate header is correctly handled
    32  *
       
    33  * @run main/othervm ProxyAuthTest
    32  * @run main/othervm ProxyAuthTest
    34  */
    33  */
    35 
    34 
    36 import java.io.BufferedWriter;
    35 import java.io.BufferedWriter;
    37 import java.io.IOException;
    36 import java.io.IOException;
    40 import java.io.OutputStreamWriter;
    39 import java.io.OutputStreamWriter;
    41 import java.io.PrintWriter;
    40 import java.io.PrintWriter;
    42 import java.net.Authenticator;
    41 import java.net.Authenticator;
    43 import java.net.InetSocketAddress;
    42 import java.net.InetSocketAddress;
    44 import java.net.PasswordAuthentication;
    43 import java.net.PasswordAuthentication;
       
    44 import java.net.Proxy;
    45 import java.net.ProxySelector;
    45 import java.net.ProxySelector;
    46 import java.net.ServerSocket;
    46 import java.net.ServerSocket;
    47 import java.net.Socket;
    47 import java.net.Socket;
       
    48 import java.net.SocketAddress;
    48 import java.net.URI;
    49 import java.net.URI;
    49 import jdk.incubator.http.HttpClient;
    50 import jdk.incubator.http.HttpClient;
    50 import jdk.incubator.http.HttpRequest;
    51 import jdk.incubator.http.HttpRequest;
    51 import jdk.incubator.http.HttpResponse;
    52 import jdk.incubator.http.HttpResponse;
    52 import java.util.Base64;
    53 import java.util.Base64;
       
    54 import java.util.List;
    53 import sun.net.www.MessageHeader;
    55 import sun.net.www.MessageHeader;
    54 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
    56 import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
    55 
    57 
    56 public class ProxyAuthTest {
    58 public class ProxyAuthTest {
    57     private static final String AUTH_USER = "user";
    59     private static final String AUTH_USER = "user";
    66 
    68 
    67             Auth auth = new Auth();
    69             Auth auth = new Auth();
    68             InetSocketAddress paddr = new InetSocketAddress("localhost", port);
    70             InetSocketAddress paddr = new InetSocketAddress("localhost", port);
    69 
    71 
    70             URI uri = new URI("http://www.google.ie/");
    72             URI uri = new URI("http://www.google.ie/");
       
    73             CountingProxySelector ps = CountingProxySelector.of(paddr);
    71             HttpClient client = HttpClient.newBuilder()
    74             HttpClient client = HttpClient.newBuilder()
    72                                           .proxy(ProxySelector.of(paddr))
    75                                           .proxy(ps)
    73                                           .authenticator(auth)
    76                                           .authenticator(auth)
    74                                           .build();
    77                                           .build();
    75             HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
    78             HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
    76             HttpResponse<?> resp = client.sendAsync(req, discard(null)).get();
    79             HttpResponse<?> resp = client.sendAsync(req, discard(null)).get();
    77             if (resp.statusCode() != 404) {
    80             if (resp.statusCode() != 404) {
    85             }
    88             }
    86 
    89 
    87             if (!proxy.matched) {
    90             if (!proxy.matched) {
    88                 throw new RuntimeException("Proxy authentication failed");
    91                 throw new RuntimeException("Proxy authentication failed");
    89             }
    92             }
       
    93             if (ps.count() > 1) {
       
    94                 throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count());
       
    95             }
    90         }
    96         }
    91     }
    97     }
    92 
    98 
    93     static class Auth extends Authenticator {
    99     static class Auth extends Authenticator {
    94         private volatile boolean isCalled;
   100         private volatile boolean isCalled;
    97         protected PasswordAuthentication getPasswordAuthentication() {
   103         protected PasswordAuthentication getPasswordAuthentication() {
    98             System.out.println("scheme: " + this.getRequestingScheme());
   104             System.out.println("scheme: " + this.getRequestingScheme());
    99             isCalled = true;
   105             isCalled = true;
   100             return new PasswordAuthentication(AUTH_USER,
   106             return new PasswordAuthentication(AUTH_USER,
   101                     AUTH_PASSWORD.toCharArray());
   107                     AUTH_PASSWORD.toCharArray());
       
   108         }
       
   109     }
       
   110 
       
   111     /**
       
   112      * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
       
   113      * of times its select method has been invoked. This can be used to ensure
       
   114      * that the Proxy Selector is invoked only once per HttpClient.sendXXX
       
   115      * invocation.
       
   116      */
       
   117     static class CountingProxySelector extends ProxySelector {
       
   118         private final ProxySelector proxySelector;
       
   119         private volatile int count; // 0
       
   120         private CountingProxySelector(InetSocketAddress proxyAddress) {
       
   121             proxySelector = ProxySelector.of(proxyAddress);
       
   122         }
       
   123 
       
   124         public static CountingProxySelector of(InetSocketAddress proxyAddress) {
       
   125             return new CountingProxySelector(proxyAddress);
       
   126         }
       
   127 
       
   128         int count() { return count; }
       
   129 
       
   130         @Override
       
   131         public List<Proxy> select(URI uri) {
       
   132             count++;
       
   133             return proxySelector.select(uri);
       
   134         }
       
   135 
       
   136         @Override
       
   137         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
       
   138             proxySelector.connectFailed(uri, sa, ioe);
   102         }
   139         }
   103     }
   140     }
   104 
   141 
   105     static class MyProxy implements Runnable {
   142     static class MyProxy implements Runnable {
   106         final ServerSocket ss;
   143         final ServerSocket ss;