test/jdk/java/net/httpclient/ProxyAuthTest.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56233 1753108d07b9
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     1
/*
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     4
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    10
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    15
 * accompanied this code).
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    16
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    20
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45028
b0ea3c0bfb81 8179889: Fix typographic errors in copyright headers
ihse
parents: 42460
diff changeset
    23
 * questions.
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    24
 */
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    25
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    26
/*
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    27
 * @test
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    28
 * @bug 8163561
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    29
 * @modules java.base/sun.net.www
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    30
 *          java.net.http
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    31
 * @summary Verify that Proxy-Authenticate header is correctly handled
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    32
 * @run main/othervm ProxyAuthTest
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    33
 */
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    34
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    35
import java.io.BufferedWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    36
import java.io.IOException;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    37
import java.io.InputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    38
import java.io.OutputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    39
import java.io.OutputStreamWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    40
import java.io.PrintWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    41
import java.net.Authenticator;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
    42
import java.net.InetAddress;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    43
import java.net.InetSocketAddress;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    44
import java.net.PasswordAuthentication;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    45
import java.net.Proxy;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    46
import java.net.ProxySelector;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    47
import java.net.ServerSocket;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    48
import java.net.Socket;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    49
import java.net.SocketAddress;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    50
import java.net.URI;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    51
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    52
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    53
import java.net.http.HttpResponse;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    54
import java.net.http.HttpResponse.BodyHandlers;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    55
import java.util.Base64;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    56
import java.util.List;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    57
import sun.net.www.MessageHeader;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    58
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    59
public class ProxyAuthTest {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    60
    private static final String AUTH_USER = "user";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    61
    private static final String AUTH_PASSWORD = "password";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    62
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    63
    public static void main(String[] args) throws Exception {
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    64
        try (ServerSocket ss = new ServerSocket()) {
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    65
            ss.setReuseAddress(false);
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
    66
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    67
            int port = ss.getLocalPort();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    68
            MyProxy proxy = new MyProxy(ss);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    69
            (new Thread(proxy)).start();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    70
            System.out.println("Proxy listening port " + port);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    71
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    72
            Auth auth = new Auth();
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
    73
            InetSocketAddress paddr = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    74
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    75
            URI uri = new URI("http://www.google.ie/");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    76
            CountingProxySelector ps = CountingProxySelector.of(paddr);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    77
            HttpClient client = HttpClient.newBuilder()
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    78
                                          .proxy(ps)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    79
                                          .authenticator(auth)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    80
                                          .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    81
            HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56089
diff changeset
    82
            HttpResponse<?> resp = client.sendAsync(req, BodyHandlers.discarding()).get();
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    83
            if (resp.statusCode() != 404) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    84
                throw new RuntimeException("Unexpected status code: " + resp.statusCode());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    85
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    86
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    87
            if (auth.isCalled) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    88
                System.out.println("Authenticator is called");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    89
            } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    90
                throw new RuntimeException("Authenticator is not called");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    91
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    92
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    93
            if (!proxy.matched) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    94
                throw new RuntimeException("Proxy authentication failed");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    95
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
            if (ps.count() > 1) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
                throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    98
            }
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    99
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   100
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   101
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   102
    static class Auth extends Authenticator {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   103
        private volatile boolean isCalled;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   104
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   105
        @Override
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   106
        protected PasswordAuthentication getPasswordAuthentication() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   107
            System.out.println("scheme: " + this.getRequestingScheme());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   108
            isCalled = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   109
            return new PasswordAuthentication(AUTH_USER,
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   110
                    AUTH_PASSWORD.toCharArray());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   111
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   112
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   113
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
     * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
     * of times its select method has been invoked. This can be used to ensure
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
     * that the Proxy Selector is invoked only once per HttpClient.sendXXX
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
     * invocation.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   120
    static class CountingProxySelector extends ProxySelector {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
        private final ProxySelector proxySelector;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
        private volatile int count; // 0
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
        private CountingProxySelector(InetSocketAddress proxyAddress) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   124
            proxySelector = ProxySelector.of(proxyAddress);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   125
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
        public static CountingProxySelector of(InetSocketAddress proxyAddress) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
            return new CountingProxySelector(proxyAddress);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   131
        int count() { return count; }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   132
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   133
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   134
        public List<Proxy> select(URI uri) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   135
            count++;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   136
            return proxySelector.select(uri);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
            proxySelector.connectFailed(uri, sa, ioe);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   142
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   143
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   145
    static class MyProxy implements Runnable {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   146
        final ServerSocket ss;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   147
        private volatile boolean matched;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   148
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   149
        MyProxy(ServerSocket ss) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   150
            this.ss = ss;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   151
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   152
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   153
        public void run() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   154
            for (int i = 0; i < 2; i++) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   155
                try (Socket s = ss.accept();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   156
                     InputStream in = s.getInputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   157
                     OutputStream os = s.getOutputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   158
                     BufferedWriter writer = new BufferedWriter(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   159
                             new OutputStreamWriter(os));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   160
                     PrintWriter out = new PrintWriter(writer);) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   161
                    MessageHeader headers = new MessageHeader(in);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   162
                    System.out.println("Proxy: received " + headers);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   163
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   164
                    String authInfo = headers.findValue("Proxy-Authorization");
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   165
                    if (authInfo != null) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   166
                        authenticate(authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   167
                        out.print("HTTP/1.1 404 Not found\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   168
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   169
                        System.out.println("Proxy: 404");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   170
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   171
                    } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   172
                        out.print("HTTP/1.1 407 Proxy Authorization Required\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   173
                        out.print(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   174
                                "Proxy-Authenticate: Basic realm=\"a fake realm\"\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   175
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   176
                        System.out.println("Proxy: Authorization required");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   177
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   178
                    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   179
                } catch (IOException x) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   180
                    System.err.println("Unexpected IOException from proxy.");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   181
                    x.printStackTrace();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   182
                    break;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   183
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   184
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   185
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   186
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   187
        private void authenticate(String authInfo) throws IOException {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   188
            try {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   189
                authInfo.trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   190
                int ind = authInfo.indexOf(' ');
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   191
                String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   192
                // extract encoded username:passwd
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   193
                String value = new String(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   194
                        Base64.getMimeDecoder().decode(recvdUserPlusPass));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   195
                String userPlusPassword = AUTH_USER + ":" + AUTH_PASSWORD;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   196
                if (userPlusPassword.equals(value)) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   197
                    matched = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   198
                    System.out.println("Proxy: client authentication successful");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   199
                } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   200
                    System.err.println(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   201
                            "Proxy: client authentication failed, expected ["
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   202
                                    + userPlusPassword + "], actual [" + value
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   203
                                    + "]");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   204
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   205
            } catch (Exception e) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   206
                throw new IOException(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   207
                        "Proxy received invalid Proxy-Authorization value: "
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   208
                                + authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   209
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   210
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   211
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   212
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   213
}