jdk/test/java/net/httpclient/ProxyAuthTest.java
author ihse
Tue, 09 May 2017 12:57:30 +0200
changeset 45028 b0ea3c0bfb81
parent 42460 7133f144981a
permissions -rw-r--r--
8179889: Fix typographic errors in copyright headers Reviewed-by: erikj, dholmes
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
/*
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    30
 *          jdk.incubator.httpclient
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
 *
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    33
 * @run main/othervm ProxyAuthTest
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
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    36
import java.io.BufferedWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    37
import java.io.IOException;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    38
import java.io.InputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    39
import java.io.OutputStream;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    40
import java.io.OutputStreamWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    41
import java.io.PrintWriter;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    42
import java.net.Authenticator;
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;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    45
import java.net.ProxySelector;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    46
import java.net.ServerSocket;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    47
import java.net.Socket;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    48
import java.net.URI;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    49
import jdk.incubator.http.HttpClient;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    50
import jdk.incubator.http.HttpRequest;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    51
import jdk.incubator.http.HttpResponse;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    52
import java.util.Base64;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    53
import sun.net.www.MessageHeader;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    54
import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    55
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    56
public class ProxyAuthTest {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    57
    private static final String AUTH_USER = "user";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    58
    private static final String AUTH_PASSWORD = "password";
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    59
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    60
    public static void main(String[] args) throws Exception {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    61
        try (ServerSocket ss = new ServerSocket(0)) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    62
            int port = ss.getLocalPort();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    63
            MyProxy proxy = new MyProxy(ss);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    64
            (new Thread(proxy)).start();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    65
            System.out.println("Proxy listening port " + port);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    66
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    67
            Auth auth = new Auth();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    68
            InetSocketAddress paddr = new InetSocketAddress("localhost", port);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    69
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    70
            URI uri = new URI("http://www.google.ie/");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    71
            HttpClient client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    72
                                          .proxy(ProxySelector.of(paddr))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    73
                                          .authenticator(auth)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    74
                                          .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    75
            HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
    76
            HttpResponse<?> resp = client.sendAsync(req, discard(null)).get();
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    77
            if (resp.statusCode() != 404) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    78
                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
    79
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    80
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    81
            if (auth.isCalled) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    82
                System.out.println("Authenticator is called");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    83
            } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    84
                throw new RuntimeException("Authenticator is not called");
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 (!proxy.matched) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    88
                throw new RuntimeException("Proxy authentication failed");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    89
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    90
        }
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
    static class Auth extends Authenticator {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    94
        private volatile boolean isCalled;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    95
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    96
        @Override
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    97
        protected PasswordAuthentication getPasswordAuthentication() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    98
            System.out.println("scheme: " + this.getRequestingScheme());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
    99
            isCalled = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   100
            return new PasswordAuthentication(AUTH_USER,
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   101
                    AUTH_PASSWORD.toCharArray());
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   102
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   103
    }
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
    static class MyProxy implements Runnable {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   106
        final ServerSocket ss;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   107
        private volatile boolean matched;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   108
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   109
        MyProxy(ServerSocket ss) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   110
            this.ss = ss;
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
        public void run() {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   114
            for (int i = 0; i < 2; i++) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   115
                try (Socket s = ss.accept();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   116
                     InputStream in = s.getInputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   117
                     OutputStream os = s.getOutputStream();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   118
                     BufferedWriter writer = new BufferedWriter(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   119
                             new OutputStreamWriter(os));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   120
                     PrintWriter out = new PrintWriter(writer);) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   121
                    MessageHeader headers = new MessageHeader(in);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   122
                    System.out.println("Proxy: received " + headers);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   123
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 41103
diff changeset
   124
                    String authInfo = headers.findValue("Proxy-Authorization");
40560
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   125
                    if (authInfo != null) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   126
                        authenticate(authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   127
                        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
   128
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   129
                        System.out.println("Proxy: 404");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   130
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   131
                    } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   132
                        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
   133
                        out.print(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   134
                                "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
   135
                        out.print("\r\n");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   136
                        System.out.println("Proxy: Authorization required");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   137
                        out.flush();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   138
                    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   139
                } catch (IOException x) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   140
                    System.err.println("Unexpected IOException from proxy.");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   141
                    x.printStackTrace();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   142
                    break;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   143
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   144
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   145
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   146
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   147
        private void authenticate(String authInfo) throws IOException {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   148
            try {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   149
                authInfo.trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   150
                int ind = authInfo.indexOf(' ');
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   151
                String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   152
                // extract encoded username:passwd
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   153
                String value = new String(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   154
                        Base64.getMimeDecoder().decode(recvdUserPlusPass));
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   155
                String userPlusPassword = AUTH_USER + ":" + AUTH_PASSWORD;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   156
                if (userPlusPassword.equals(value)) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   157
                    matched = true;
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   158
                    System.out.println("Proxy: client authentication successful");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   159
                } else {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   160
                    System.err.println(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   161
                            "Proxy: client authentication failed, expected ["
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   162
                                    + userPlusPassword + "], actual [" + value
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   163
                                    + "]");
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   164
                }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   165
            } catch (Exception e) {
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   166
                throw new IOException(
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   167
                        "Proxy received invalid Proxy-Authorization value: "
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   168
                                + authInfo);
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   169
            }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   170
        }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   171
    }
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   172
5641adb8d196 8163561: Add a test for Proxy Authentication in HTTP/2 Client API
xiaofeya
parents:
diff changeset
   173
}