test/jdk/java/net/httpclient/SpecialHeadersTest.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 53698 68ca8b201925
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
     1
/*
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
50681
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
 * @summary  Verify that some special headers - such as User-Agent
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    27
 *           can be specified by the caller.
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
    28
 * @bug 8203771 8218546
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    29
 * @modules java.base/sun.net.www.http
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    33
 *          java.logging
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    34
 *          jdk.httpserver
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 52106
diff changeset
    35
 * @library /test/lib http2/server
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    36
 * @build Http2TestServer HttpServerAdapters SpecialHeadersTest
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 52106
diff changeset
    37
 * @build jdk.test.lib.net.SimpleSSLContext
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    38
 * @run testng/othervm
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    39
 *       -Djdk.httpclient.HttpClient.log=requests,headers,errors
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    40
 *       SpecialHeadersTest
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
    41
 * @run testng/othervm -Djdk.httpclient.allowRestrictedHeaders=Host
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
    42
 *       -Djdk.httpclient.HttpClient.log=requests,headers,errors
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
    43
 *       SpecialHeadersTest
50681
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;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 52106
diff changeset
    49
import jdk.test.lib.net.SimpleSSLContext;
50681
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.InetAddress;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    60
import java.net.InetSocketAddress;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    61
import java.net.URI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    62
import java.net.http.HttpClient;
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
    63
import java.net.http.HttpHeaders;
50681
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;
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
    67
import java.time.Duration;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    68
import java.util.ArrayList;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    69
import java.util.Arrays;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    70
import java.util.List;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    71
import java.util.Locale;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    72
import java.util.Map;
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
    73
import java.util.Optional;
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
    74
import java.util.function.Function;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    75
import static java.lang.System.out;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    76
import static java.net.http.HttpClient.Builder.NO_PROXY;
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
    77
import static java.net.http.HttpClient.Version.HTTP_2;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    78
import static java.nio.charset.StandardCharsets.US_ASCII;
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
    79
import org.testng.Assert;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    80
import static org.testng.Assert.assertEquals;
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
    81
import static org.testng.Assert.assertTrue;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    82
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    83
public class SpecialHeadersTest implements HttpServerAdapters {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    84
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    85
    SSLContext sslContext;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    86
    HttpTestServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    87
    HttpTestServer httpsTestServer;        // HTTPS/1.1
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    88
    HttpTestServer http2TestServer;        // HTTP/2 ( h2c )
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    89
    HttpTestServer https2TestServer;       // HTTP/2 ( h2  )
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    90
    String httpURI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    91
    String httpsURI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    92
    String http2URI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    93
    String https2URI;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    94
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    95
    static final String[][] headerNamesAndValues = new String[][]{
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    96
            {"User-Agent: <DEFAULT>"},
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    97
            {"User-Agent: camel-cased"},
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    98
            {"user-agent: all-lower-case"},
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
    99
            {"user-Agent: mixed"},
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   100
            // headers which were restricted before and are now allowable
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   101
            {"referer: lower"},
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   102
            {"Referer: normal"},
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   103
            {"REFERER: upper"},
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   104
            {"origin: lower"},
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   105
            {"Origin: normal"},
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   106
            {"ORIGIN: upper"},
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   107
    };
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   108
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   109
    // Needs net.property enabled for this part of test
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   110
    static final String[][] headerNamesAndValues1 = new String[][]{
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   111
            {"Host: <DEFAULT>"},
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   112
            {"Host: camel-cased"},
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   113
            {"host: all-lower-case"},
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   114
            {"hoSt: mixed"}
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   115
    };
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   116
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   117
    @DataProvider(name = "variants")
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   118
    public Object[][] variants() {
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   119
        String prop = System.getProperty("jdk.httpclient.allowRestrictedHeaders");
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   120
        boolean hostTest = prop != null && prop.equalsIgnoreCase("host");
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   121
        final String[][] testInput = hostTest ? headerNamesAndValues1 : headerNamesAndValues;
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   122
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   123
        List<Object[]> list = new ArrayList<>();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   124
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   125
        for (boolean sameClient : new boolean[] { false, true }) {
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   126
            Arrays.asList(testInput).stream()
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   127
                    .map(e -> new Object[] {httpURI, e[0], sameClient})
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   128
                    .forEach(list::add);
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   129
            Arrays.asList(testInput).stream()
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   130
                    .map(e -> new Object[] {httpsURI, e[0], sameClient})
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   131
                    .forEach(list::add);
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   132
            Arrays.asList(testInput).stream()
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   133
                    .map(e -> new Object[] {http2URI, e[0], sameClient})
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   134
                    .forEach(list::add);
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   135
            Arrays.asList(testInput).stream()
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   136
                    .map(e -> new Object[] {https2URI, e[0], sameClient})
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   137
                    .forEach(list::add);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   138
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   139
        return list.stream().toArray(Object[][]::new);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   140
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   141
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   142
    static final int ITERATION_COUNT = 3; // checks upgrade and re-use
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   143
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   144
    static String userAgent() {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   145
        return "Java-http-client/" + System.getProperty("java.version");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   146
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   147
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   148
    static final Map<String, Function<URI,String>> DEFAULTS = Map.of(
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   149
        "USER-AGENT", u -> userAgent(), "HOST", u -> u.getRawAuthority());
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   150
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   151
    @Test(dataProvider = "variants")
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   152
    void test(String uriString,
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   153
              String headerNameAndValue,
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   154
              boolean sameClient)
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   155
        throws Exception
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   156
    {
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   157
        out.println("\n--- Starting ");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   158
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   159
        int index = headerNameAndValue.indexOf(":");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   160
        String name = headerNameAndValue.substring(0, index);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   161
        String v = headerNameAndValue.substring(index+1).trim();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   162
        String key = name.toUpperCase(Locale.ROOT);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   163
        boolean useDefault = "<DEFAULT>".equals(v);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   164
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   165
        URI uri = URI.create(uriString+"?name="+key);
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   166
        String value =  useDefault ? DEFAULTS.get(key).apply(uri) : v;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   167
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   168
        HttpClient client = null;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   169
        for (int i=0; i< ITERATION_COUNT; i++) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   170
            if (!sameClient || client == null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   171
                client = HttpClient.newBuilder()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   172
                        .proxy(NO_PROXY)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   173
                        .sslContext(sslContext)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   174
                        .build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   175
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   176
            HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   177
            if (!useDefault) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   178
                requestBuilder.header(name, value);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   179
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   180
            HttpRequest request = requestBuilder.build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   181
            HttpResponse<String> resp = client.send(request, BodyHandlers.ofString());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   182
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   183
            out.println("Got response: " + resp);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   184
            out.println("Got body: " + resp.body());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   185
            assertEquals(resp.statusCode(), 200,
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   186
                    "Expected 200, got:" + resp.statusCode());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   187
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   188
            boolean isInitialRequest = i == 0;
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   189
            boolean isSecure = uri.getScheme().equalsIgnoreCase("https");
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   190
            boolean isHTTP2 = resp.version() == HTTP_2;
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   191
            boolean isNotH2CUpgrade = isSecure || (sameClient == true && !isInitialRequest);
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   192
            boolean isDefaultHostHeader = name.equalsIgnoreCase("host") && useDefault;
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   193
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   194
            // By default, HTTP/2 sets the `:authority:` pseudo-header, instead
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   195
            // of the `Host` header. Therefore, there should be no "X-Host"
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   196
            // header in the response, except the response to the h2c Upgrade
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   197
            // request which will have been sent through HTTP/1.1.
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   198
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   199
            if (isDefaultHostHeader && isHTTP2 && isNotH2CUpgrade) {
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   200
                assertTrue(resp.headers().firstValue("X-" + key).isEmpty());
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   201
                assertTrue(resp.headers().allValues("X-" + key).isEmpty());
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   202
                out.println("No X-" + key + " header received, as expected");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   203
            } else {
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   204
                String receivedHeaderString = value == null ? null
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   205
                        : resp.headers().firstValue("X-"+key).get();
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   206
                out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key));
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   207
                if (value != null) {
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   208
                    assertEquals(receivedHeaderString, value);
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   209
                    assertEquals(resp.headers().allValues("X-"+key), List.of(value));
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   210
                } else {
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   211
                    assertEquals(resp.headers().allValues("X-"+key).size(), 0);
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   212
                }
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   213
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   214
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   215
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   216
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   217
    @Test(dataProvider = "variants")
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   218
    void testHomeMadeIllegalHeader(String uriString,
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   219
                                   String headerNameAndValue,
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   220
                                   boolean sameClient)
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   221
        throws Exception
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   222
    {
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   223
        out.println("\n--- Starting ");
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   224
        final URI uri = URI.create(uriString);
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   225
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   226
        HttpClient client = HttpClient.newBuilder()
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   227
                .proxy(NO_PROXY)
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   228
                .sslContext(sslContext)
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   229
                .build();
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   230
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   231
        // Test a request which contains an illegal header created
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   232
        HttpRequest req = new HttpRequest() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   233
            @Override public Optional<BodyPublisher> bodyPublisher() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   234
                return Optional.of(BodyPublishers.noBody());
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   235
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   236
            @Override public String method() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   237
                return "GET";
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   238
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   239
            @Override public Optional<Duration> timeout() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   240
                return Optional.empty();
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   241
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   242
            @Override public boolean expectContinue() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   243
                return false;
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   244
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   245
            @Override public URI uri() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   246
                return uri;
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   247
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   248
            @Override public Optional<HttpClient.Version> version() {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   249
                return Optional.empty();
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   250
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   251
            @Override public HttpHeaders headers() {
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   252
                Map<String, List<String>> map = Map.of("upgrade", List.of("http://foo.com"));
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   253
                return HttpHeaders.of(map, (x, y) -> true);
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   254
            }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   255
        };
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   256
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   257
        try {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   258
            HttpResponse<String> response = client.send(req, BodyHandlers.ofString());
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   259
            Assert.fail("Unexpected reply: " + response);
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   260
        } catch (IllegalArgumentException ee) {
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   261
            out.println("Got IAE as expected");
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   262
        }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   263
    }
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   264
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   265
    @Test(dataProvider = "variants")
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   266
    void testAsync(String uriString, String headerNameAndValue, boolean sameClient) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   267
        out.println("\n--- Starting ");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   268
        int index = headerNameAndValue.indexOf(":");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   269
        String name = headerNameAndValue.substring(0, index);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   270
        String v = headerNameAndValue.substring(index+1).trim();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   271
        String key = name.toUpperCase(Locale.ROOT);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   272
        boolean useDefault = "<DEFAULT>".equals(v);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   273
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   274
        URI uri = URI.create(uriString+"?name="+key);
52554
5f1ca46703f9 8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents: 52121
diff changeset
   275
        String value =  useDefault ? DEFAULTS.get(key).apply(uri) : v;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   276
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   277
        HttpClient client = null;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   278
        for (int i=0; i< ITERATION_COUNT; i++) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   279
            if (!sameClient || client == null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   280
                client = HttpClient.newBuilder()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   281
                        .proxy(NO_PROXY)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   282
                        .sslContext(sslContext)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   283
                        .build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   284
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   285
            HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   286
            if (!useDefault) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   287
                requestBuilder.header(name, value);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   288
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   289
            HttpRequest request = requestBuilder.build();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   290
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   291
            boolean isInitialRequest = i == 0;
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   292
            boolean isSecure = uri.getScheme().equalsIgnoreCase("https");
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   293
            boolean isNotH2CUpgrade = isSecure || (sameClient == true && !isInitialRequest);
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   294
            boolean isDefaultHostHeader = name.equalsIgnoreCase("host") && useDefault;
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   295
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   296
            client.sendAsync(request, BodyHandlers.ofString())
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   297
                    .thenApply(response -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   298
                        out.println("Got response: " + response);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   299
                        out.println("Got body: " + response.body());
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   300
                        assertEquals(response.statusCode(), 200);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   301
                        return response;})
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   302
                    .thenAccept(resp -> {
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   303
                        // By default, HTTP/2 sets the `:authority:` pseudo-header, instead
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   304
                        // of the `Host` header. Therefore, there should be no "X-Host"
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   305
                        // header in the response, except the response to the h2c Upgrade
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   306
                        // request which will have been sent through HTTP/1.1.
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   307
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   308
                        if (isDefaultHostHeader && resp.version() == HTTP_2 && isNotH2CUpgrade) {
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   309
                            assertTrue(resp.headers().firstValue("X-" + key).isEmpty());
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   310
                            assertTrue(resp.headers().allValues("X-" + key).isEmpty());
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   311
                            out.println("No X-" + key + " header received, as expected");
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   312
                        } else {
53698
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   313
                            String receivedHeaderString = value == null ? null
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   314
                                    : resp.headers().firstValue("X-"+key).get();
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   315
                            out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key));
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   316
                            if (value != null) {
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   317
                                assertEquals(receivedHeaderString, value);
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   318
                                assertEquals(resp.headers().allValues("X-" + key), List.of(value));
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   319
                            } else {
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   320
                                assertEquals(resp.headers().allValues("X-" + key).size(), 1);
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   321
                            }
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   322
                        }
68ca8b201925 8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents: 52554
diff changeset
   323
                    })
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   324
                    .join();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   325
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   326
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   327
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   328
    static String serverAuthority(HttpTestServer server) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   329
        return InetAddress.getLoopbackAddress().getHostName() + ":"
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   330
                + server.getAddress().getPort();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   331
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   332
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   333
    @BeforeTest
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   334
    public void setup() throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   335
        sslContext = new SimpleSSLContext().get();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   336
        if (sslContext == null)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   337
            throw new AssertionError("Unexpected null sslContext");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   338
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   339
        HttpTestHandler handler = new HttpUriStringHandler();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   340
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   341
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   342
        httpTestServer.addHandler(handler, "/http1");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   343
        httpURI = "http://" + serverAuthority(httpTestServer) + "/http1";
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   344
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   345
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   346
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   347
        httpsTestServer = HttpTestServer.of(httpsServer);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   348
        httpsTestServer.addHandler(handler, "/https1");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   349
        httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1";
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   350
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   351
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   352
        http2TestServer.addHandler(handler, "/http2");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   353
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   354
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   355
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   356
        https2TestServer.addHandler(handler, "/https2");
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   357
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2";
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   358
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   359
        httpTestServer.start();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   360
        httpsTestServer.start();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   361
        http2TestServer.start();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   362
        https2TestServer.start();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   363
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   364
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   365
    @AfterTest
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   366
    public void teardown() throws Exception {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   367
        httpTestServer.stop();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   368
        httpsTestServer.stop();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   369
        http2TestServer.stop();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   370
        https2TestServer.stop();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   371
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   372
52106
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   373
    /** A handler that returns, as its body, the exact received request URI.
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   374
     *  The header whose name is in the URI query and is set in the request is
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   375
     *  returned in the response with its name prefixed by X-
76d526565453 8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents: 50681
diff changeset
   376
     */
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   377
    static class HttpUriStringHandler implements HttpTestHandler {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   378
        @Override
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   379
        public void handle(HttpTestExchange t) throws IOException {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   380
            URI uri = t.getRequestURI();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   381
            String uriString = uri.toString();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   382
            out.println("Http1UriStringHandler received, uri: " + uriString);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   383
            String query = uri.getQuery();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   384
            String headerName = query.substring(query.indexOf("=")+1).trim();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   385
            try (InputStream is = t.getRequestBody();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   386
                 OutputStream os = t.getResponseBody()) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   387
                is.readAllBytes();
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   388
                byte[] bytes = uriString.getBytes(US_ASCII);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   389
                t.getRequestHeaders().keySet().stream()
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   390
                        .filter(headerName::equalsIgnoreCase)
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   391
                        .forEach(h -> {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   392
                            for (String v : t.getRequestHeaders().get(headerName)) {
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   393
                                t.getResponseHeaders().addHeader("X-"+h, v);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   394
                            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   395
                        });
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   396
                t.sendResponseHeaders(200, bytes.length);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   397
                os.write(bytes);
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   398
            }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   399
        }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   400
    }
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents:
diff changeset
   401
}