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