test/jdk/java/net/httpclient/NonAsciiCharsInURI.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
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:
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     1
/*
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     4
 *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     8
 *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    13
 * accompanied this code).
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    14
 *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    18
 *
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    21
 * questions.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    22
 */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    23
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    24
/*
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    25
 * @test
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    26
 * @summary Verify that non-US-ASCII chars are replaced with a sequence of
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    27
 *          escaped octets that represent that char in the UTF-8 character set.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    28
 * @bug 8201238
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    29
 * @modules java.base/sun.net.www.http
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    33
 *          java.logging
4690a2871b44 8202423: Small 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: 50681
diff changeset
    35
 * @library /test/lib http2/server
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    36
 * @build Http2TestServer
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    37
 * @build jdk.test.lib.net.SimpleSSLContext
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    38
 * @compile -encoding utf-8 NonAsciiCharsInURI.java
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    39
 * @run testng/othervm
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    40
 *       -Djdk.httpclient.HttpClient.log=reqeusts,headers
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    41
 *       NonAsciiCharsInURI
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    42
 */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    43
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpServer;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    45
import com.sun.net.httpserver.HttpsConfigurator;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    46
import com.sun.net.httpserver.HttpsServer;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    47
import java.io.IOException;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    48
import java.io.InputStream;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    49
import java.io.OutputStream;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    50
import java.net.InetAddress;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    51
import java.net.InetSocketAddress;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    52
import java.net.URI;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    53
import javax.net.ssl.SSLContext;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    54
import java.net.http.HttpClient;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    55
import java.net.http.HttpRequest;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    56
import java.net.http.HttpResponse;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    57
import java.net.http.HttpResponse.BodyHandlers;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    58
import java.util.ArrayList;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    59
import java.util.Arrays;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    60
import java.util.List;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    61
import jdk.test.lib.net.SimpleSSLContext;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    62
import org.testng.annotations.AfterTest;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    63
import org.testng.annotations.BeforeTest;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    64
import org.testng.annotations.DataProvider;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    65
import org.testng.annotations.Test;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    66
import static java.lang.System.err;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    67
import static java.lang.System.out;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    68
import static java.nio.charset.StandardCharsets.US_ASCII;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    69
import static java.net.http.HttpClient.Builder.NO_PROXY;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    70
import static org.testng.Assert.assertEquals;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    71
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    72
public class NonAsciiCharsInURI implements HttpServerAdapters {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    73
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    74
    SSLContext sslContext;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    75
    HttpTestServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    76
    HttpTestServer httpsTestServer;        // HTTPS/1.1
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    77
    HttpTestServer http2TestServer;        // HTTP/2 ( h2c )
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    78
    HttpTestServer https2TestServer;       // HTTP/2 ( h2  )
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    79
    String httpURI;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    80
    String httpsURI;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    81
    String http2URI;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    82
    String https2URI;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    83
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    84
    // € = '\u20AC' => 0xE20x820xAC
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    85
    static final String[][] pathsAndQueryStrings = new String[][] {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    86
               // partial-path
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    87
            {  "/001/plain"                                                            },
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    88
            {  "/002/plain?plainQuery"                                                 },
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    89
            {  "/003/withEuroSymbol/€"                                                 },
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    90
            {  "/004/withEuroSymbol/€?euroSymbol=€"                                    },
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    91
            {  "/005/wiki/エリザベス1世_(イングランド女王)"                                },
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    92
            {  "/006/x?url=https://ja.wikipedia.org/wiki/エリザベス1世_(イングランド女王)" },
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    93
    };
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    94
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    95
    @DataProvider(name = "variants")
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    96
    public Object[][] variants() {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    97
        List<Object[]> list = new ArrayList<>();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    98
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
    99
        for (boolean sameClient : new boolean[] { false, true }) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   100
            Arrays.asList(pathsAndQueryStrings).stream()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   101
                    .map(e -> new Object[] {httpURI + e[0], sameClient})
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   102
                    .forEach(list::add);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   103
            Arrays.asList(pathsAndQueryStrings).stream()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   104
                    .map(e -> new Object[] {httpsURI + e[0], sameClient})
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   105
                    .forEach(list::add);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   106
            Arrays.asList(pathsAndQueryStrings).stream()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   107
                    .map(e -> new Object[] {http2URI + e[0], sameClient})
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   108
                    .forEach(list::add);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   109
            Arrays.asList(pathsAndQueryStrings).stream()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   110
                    .map(e -> new Object[] {https2URI + e[0], sameClient})
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   111
                    .forEach(list::add);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   112
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   113
        return list.stream().toArray(Object[][]::new);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   114
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   115
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   116
    static final int ITERATION_COUNT = 3; // checks upgrade and re-use
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   117
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   118
    @Test(dataProvider = "variants")
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   119
    void test(String uriString, boolean sameClient) throws Exception {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   120
        out.println("\n--- Starting ");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   121
        // The single-argument factory requires any illegal characters in its
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   122
        // argument to be quoted and preserves any escaped octets and other
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   123
        // characters that are present.
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   124
        URI uri = URI.create(uriString);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   125
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   126
        HttpClient client = null;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   127
        for (int i=0; i< ITERATION_COUNT; i++) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   128
            if (!sameClient || client == null)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   129
                client = HttpClient.newBuilder()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   130
                        .proxy(NO_PROXY)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   131
                        .sslContext(sslContext)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   132
                        .build();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   133
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   134
            HttpRequest request = HttpRequest.newBuilder(uri).build();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   135
            HttpResponse<String> resp = client.send(request, BodyHandlers.ofString());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   136
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   137
            out.println("Got response: " + resp);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   138
            out.println("Got body: " + resp.body());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   139
            assertEquals(resp.statusCode(), 200,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   140
                    "Expected 200, got:" + resp.statusCode());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   141
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   142
            // the response body should contain the toASCIIString
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   143
            // representation of the URI
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   144
            String expectedURIString = uri.toASCIIString();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   145
            if (!expectedURIString.contains(resp.body())) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   146
                err.println("Test failed: " + resp);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   147
                throw new AssertionError(expectedURIString +
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   148
                                         " does not contain '" + resp.body() + "'");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   149
            } else {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   150
                out.println("Found expected " + resp.body() + " in " + expectedURIString);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   151
            }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   152
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   153
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   154
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   155
    @Test(dataProvider = "variants")
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   156
    void testAsync(String uriString, boolean sameClient) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   157
        out.println("\n--- Starting ");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   158
        URI uri = URI.create(uriString);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   159
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   160
        HttpClient client = null;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   161
        for (int i=0; i< ITERATION_COUNT; i++) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   162
            if (!sameClient || client == null)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   163
                client = HttpClient.newBuilder()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   164
                        .proxy(NO_PROXY)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   165
                        .sslContext(sslContext)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   166
                        .build();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   167
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   168
            HttpRequest request = HttpRequest.newBuilder(uri).build();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   169
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   170
            client.sendAsync(request, BodyHandlers.ofString())
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   171
                    .thenApply(response -> {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   172
                        out.println("Got response: " + response);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   173
                        out.println("Got body: " + response.body());
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   174
                        assertEquals(response.statusCode(), 200);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   175
                        return response.body(); })
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   176
                    .thenAccept(body -> {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   177
                        // the response body should contain the toASCIIString
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   178
                        // representation of the URI
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   179
                        String expectedURIString = uri.toASCIIString();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   180
                        if (!expectedURIString.contains(body)) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   181
                            err.println("Test failed: " + body);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   182
                            throw new AssertionError(expectedURIString +
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   183
                                    " does not contain '" + body + "'");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   184
                        } else {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   185
                            out.println("Found expected " + body + " in "
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   186
                                        + expectedURIString);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   187
                        } })
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   188
                    .join();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   189
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   190
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   191
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   192
    static String serverAuthority(HttpTestServer server) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   193
        return InetAddress.getLoopbackAddress().getHostName() + ":"
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   194
                + server.getAddress().getPort();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   195
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   196
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   197
    @BeforeTest
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   198
    public void setup() throws Exception {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   199
        sslContext = new SimpleSSLContext().get();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   200
        if (sslContext == null)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   201
            throw new AssertionError("Unexpected null sslContext");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   202
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   203
        HttpTestHandler handler = new HttpUriStringHandler();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   204
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   205
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   206
        httpTestServer.addHandler(handler, "/http1");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   207
        httpURI = "http://" + serverAuthority(httpTestServer) + "/http1";
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   208
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   209
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   210
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   211
        httpsTestServer = HttpTestServer.of(httpsServer);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   212
        httpsTestServer.addHandler(handler, "/https1");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   213
        httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1";
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   214
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   215
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   216
        http2TestServer.addHandler(handler, "/http2");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   217
        http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   218
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49944
diff changeset
   219
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   220
        https2TestServer.addHandler(handler, "/https2");
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   221
        https2URI = "https://" + https2TestServer.serverAuthority() + "/https2";
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   222
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   223
        httpTestServer.start();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   224
        httpsTestServer.start();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   225
        http2TestServer.start();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   226
        https2TestServer.start();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   227
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   228
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   229
    @AfterTest
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   230
    public void teardown() throws Exception {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   231
        httpTestServer.stop();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   232
        httpsTestServer.stop();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   233
        http2TestServer.stop();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   234
        https2TestServer.stop();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   235
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   236
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   237
    /** A handler that returns, as its body, the exact received request URI. */
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   238
    static class HttpUriStringHandler implements HttpTestHandler {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   239
        @Override
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   240
        public void handle(HttpTestExchange t) throws IOException {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   241
            String uri = t.getRequestURI().toString();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   242
            out.println("Http1UriStringHandler received, uri: " + uri);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   243
            try (InputStream is = t.getRequestBody();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   244
                 OutputStream os = t.getResponseBody()) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   245
                is.readAllBytes();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   246
                byte[] bytes = uri.getBytes(US_ASCII);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   247
                t.sendResponseHeaders(200, bytes.length);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   248
                os.write(bytes);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   249
            }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   250
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   251
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents:
diff changeset
   252
}