test/jdk/java/net/httpclient/UnauthorizedTest.java
author chegar
Tue, 19 Jun 2018 09:13:58 +0100
branchhttp-client-branch
changeset 56771 73a6534bce94
parent 56643 54e7acad0058
child 56795 03ece2518428
permissions -rw-r--r--
http-client-branch: prepare tests for TLS1.3 Contributed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56643
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     1
/*
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     4
 *
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     8
 *
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    14
 *
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    18
 *
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    21
 * questions.
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    22
 */
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    23
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    24
/*
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    25
 * @test
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    26
 * @bug 8203882
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    27
 * @summary (httpclient) Check that HttpClient throws IOException when
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    28
 *      receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    29
 *      header only in the case where an authenticator is configured
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    30
 *      for the client. If no authenticator is configured the client
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    31
 *      should simply let the caller deal with the unauthorized response.
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    32
 * @modules java.base/sun.net.www.http
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.common
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    34
 *          java.net.http/jdk.internal.net.http.frame
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    35
 *          java.net.http/jdk.internal.net.http.hpack
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    36
 *          java.logging
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    37
 *          jdk.httpserver
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    38
 * @library /lib/testlibrary /test/lib http2/server
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    39
 * @build Http2TestServer
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    40
 * @build jdk.testlibrary.SimpleSSLContext
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    41
 * @run testng/othervm
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    42
 *       -Djdk.httpclient.HttpClient.log=headers
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    43
 *       UnauthorizedTest
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    44
 */
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    45
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    46
import com.sun.net.httpserver.HttpServer;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    47
import com.sun.net.httpserver.HttpsConfigurator;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    48
import com.sun.net.httpserver.HttpsServer;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    49
import jdk.testlibrary.SimpleSSLContext;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    50
import org.testng.annotations.AfterTest;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    51
import org.testng.annotations.BeforeTest;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    52
import org.testng.annotations.DataProvider;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    53
import org.testng.annotations.Test;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    54
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    55
import javax.net.ssl.SSLContext;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    56
import java.io.IOException;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    57
import java.io.InputStream;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    58
import java.io.OutputStream;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    59
import java.net.Authenticator;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    60
import java.net.InetAddress;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    61
import java.net.InetSocketAddress;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    62
import java.net.URI;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    63
import java.net.http.HttpClient;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    64
import java.net.http.HttpRequest;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    65
import java.net.http.HttpResponse;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    66
import java.net.http.HttpResponse.BodyHandlers;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    67
import java.util.concurrent.ExecutionException;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    68
import java.util.concurrent.atomic.AtomicLong;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    69
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    70
import static java.lang.System.out;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    71
import static java.nio.charset.StandardCharsets.UTF_8;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    72
import static org.testng.Assert.assertEquals;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    73
import static org.testng.Assert.assertTrue;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    74
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    75
public class UnauthorizedTest implements HttpServerAdapters {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    76
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    77
    SSLContext sslContext;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    78
    HttpTestServer httpTestServer;        // HTTP/1.1
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    79
    HttpTestServer httpsTestServer;       // HTTPS/1.1
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    80
    HttpTestServer http2TestServer;       // HTTP/2 ( h2c )
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    81
    HttpTestServer https2TestServer;      // HTTP/2 ( h2  )
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    82
    String httpURI;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    83
    String httpsURI;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    84
    String http2URI;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    85
    String https2URI;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    86
    HttpClient authClient;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    87
    HttpClient noAuthClient;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    88
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    89
    static final int ITERATIONS = 3;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    90
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    91
    /*
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    92
     * NOT_MODIFIED status code results from a conditional GET where
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    93
     * the server does not (must not) return a response body because
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    94
     * the condition specified in the request disallows it
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    95
     */
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    96
    static final int UNAUTHORIZED = 401;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    97
    static final int PROXY_UNAUTHORIZED = 407;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    98
    static final int HTTP_OK = 200;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
    99
    static final String MESSAGE = "Unauthorized";
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   100
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   101
    @DataProvider(name = "all")
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   102
    public Object[][] positive() {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   103
        return new Object[][] {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   104
                { httpURI   + "/server", UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   105
                { httpsURI  + "/server", UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   106
                { http2URI  + "/server", UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   107
                { https2URI + "/server", UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   108
                { httpURI   + "/proxy",  PROXY_UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   109
                { httpsURI  + "/proxy",  PROXY_UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   110
                { http2URI  + "/proxy",  PROXY_UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   111
                { https2URI + "/proxy",  PROXY_UNAUTHORIZED, true, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   112
                { httpURI   + "/server", UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   113
                { httpsURI  + "/server", UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   114
                { http2URI  + "/server", UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   115
                { https2URI + "/server", UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   116
                { httpURI   + "/proxy",  PROXY_UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   117
                { httpsURI  + "/proxy",  PROXY_UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   118
                { http2URI  + "/proxy",  PROXY_UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   119
                { https2URI + "/proxy",  PROXY_UNAUTHORIZED, false, authClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   120
                { httpURI   + "/server", UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   121
                { httpsURI  + "/server", UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   122
                { http2URI  + "/server", UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   123
                { https2URI + "/server", UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   124
                { httpURI   + "/proxy",  PROXY_UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   125
                { httpsURI  + "/proxy",  PROXY_UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   126
                { http2URI  + "/proxy",  PROXY_UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   127
                { https2URI + "/proxy",  PROXY_UNAUTHORIZED, true, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   128
                { httpURI   + "/server", UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   129
                { httpsURI  + "/server", UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   130
                { http2URI  + "/server", UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   131
                { https2URI + "/server", UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   132
                { httpURI   + "/proxy",  PROXY_UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   133
                { httpsURI  + "/proxy",  PROXY_UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   134
                { http2URI  + "/proxy",  PROXY_UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   135
                { https2URI + "/proxy",  PROXY_UNAUTHORIZED, false, noAuthClient},
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   136
        };
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   137
    }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   138
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   139
    static final AtomicLong requestCounter = new AtomicLong();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   140
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   141
    static final Authenticator authenticator = new Authenticator() {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   142
    };
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   143
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   144
    @Test(dataProvider = "all")
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   145
    void test(String uriString, int code, boolean async, HttpClient client) throws Throwable {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   146
        out.printf("%n---- starting (%s, %d, %s, %s) ----%n",
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   147
                uriString, code, async ? "async" : "sync",
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   148
                client.authenticator().isPresent() ? "authClient" : "noAuthClient");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   149
        URI uri = URI.create(uriString);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   150
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   151
        HttpRequest.Builder requestBuilder = HttpRequest
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   152
                .newBuilder(uri)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   153
                .GET();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   154
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   155
        HttpRequest request = requestBuilder.build();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   156
        out.println("Initial request: " + request.uri());
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   157
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   158
        boolean shouldThrow = client.authenticator().isPresent();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   159
        String header = (code==UNAUTHORIZED)?"WWW-Authenticate":"Proxy-Authenticate";
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   160
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   161
        HttpResponse<String> response = null;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   162
        try {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   163
           if (async) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   164
                response = client.send(request, BodyHandlers.ofString());
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   165
            } else {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   166
               try {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   167
                   response = client.sendAsync(request, BodyHandlers.ofString()).get();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   168
               } catch (ExecutionException ex) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   169
                   throw ex.getCause();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   170
               }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   171
           }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   172
        } catch (IOException ex) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   173
            if (shouldThrow && ex.getMessage().contains(header)) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   174
                System.out.println("Got expected exception: " + ex);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   175
                return;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   176
            } else throw ex;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   177
        }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   178
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   179
        out.println("  Got response: " + response);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   180
        assertEquals(response.statusCode(), code);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   181
        assertEquals(response.body(),
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   182
                (code == UNAUTHORIZED ? "WWW-" : "Proxy-") + MESSAGE);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   183
        if (shouldThrow) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   184
            throw new RuntimeException("Expected IOException not thrown.");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   185
        }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   186
    }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   187
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   188
    // -- Infrastructure
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   189
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   190
    @BeforeTest
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   191
    public void setup() throws Exception {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   192
        sslContext = new SimpleSSLContext().get();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   193
        if (sslContext == null)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   194
            throw new AssertionError("Unexpected null sslContext");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   195
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   196
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   197
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   198
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   199
        httpTestServer.addHandler(new UnauthorizedHandler(), "/http1/");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   200
        httpURI = "http://" + httpTestServer.serverAuthority() + "/http1";
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   201
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   202
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   203
        httpsTestServer = HttpTestServer.of(httpsServer);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   204
        httpsTestServer.addHandler(new UnauthorizedHandler(),"/https1/");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   205
        httpsURI = "https://" + httpsTestServer.serverAuthority() + "/https1";
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   206
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   207
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   208
        http2TestServer.addHandler(new UnauthorizedHandler(), "/http2/");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   209
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
56771
73a6534bce94 http-client-branch: prepare tests for TLS1.3
chegar
parents: 56643
diff changeset
   210
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
56643
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   211
        https2TestServer.addHandler(new UnauthorizedHandler(), "/https2/");
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   212
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2";
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   213
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   214
        authClient = HttpClient.newBuilder()
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   215
                .proxy(HttpClient.Builder.NO_PROXY)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   216
                .sslContext(sslContext)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   217
                .authenticator(authenticator)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   218
                .build();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   219
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   220
        noAuthClient = HttpClient.newBuilder()
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   221
                .proxy(HttpClient.Builder.NO_PROXY)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   222
                .sslContext(sslContext)
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   223
                .build();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   224
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   225
        httpTestServer.start();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   226
        httpsTestServer.start();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   227
        http2TestServer.start();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   228
        https2TestServer.start();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   229
    }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   230
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   231
    @AfterTest
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   232
    public void teardown() throws Exception {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   233
        httpTestServer.stop();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   234
        httpsTestServer.stop();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   235
        http2TestServer.stop();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   236
        https2TestServer.stop();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   237
    }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   238
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   239
    static class UnauthorizedHandler implements HttpTestHandler {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   240
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   241
        @Override
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   242
        public void handle(HttpTestExchange t) throws IOException {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   243
            readAllRequestData(t); // shouldn't be any
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   244
            String method = t.getRequestMethod();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   245
            String path = t.getRequestURI().getPath();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   246
            HttpTestResponseHeaders rsph = t.getResponseHeaders();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   247
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   248
            int code;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   249
            if (path.contains("server")) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   250
                code = UNAUTHORIZED;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   251
            } else {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   252
                code = PROXY_UNAUTHORIZED;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   253
            }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   254
            String message = (code == UNAUTHORIZED ? "WWW-" : "Proxy-") + MESSAGE;
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   255
            byte[] bytes = message.getBytes(UTF_8);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   256
            t.sendResponseHeaders(code, bytes.length);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   257
            try (OutputStream os = t.getResponseBody()) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   258
                os.write(bytes);
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   259
            }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   260
        }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   261
    }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   262
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   263
    static void readAllRequestData(HttpTestExchange t) throws IOException {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   264
        try (InputStream is = t.getRequestBody()) {
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   265
            is.readAllBytes();
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   266
        }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   267
    }
54e7acad0058 http-client-branch: HttpClient throws IOException when receiving 401/407 with no WWW-Authenticate/Proxy-Authenticate header
dfuchs
parents:
diff changeset
   268
}