test/jdk/java/net/httpclient/HttpsTunnelTest.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56167 96fa4f49a9ff
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     1
/*
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     4
 *
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     8
 *
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    14
 *
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    18
 *
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    21
 * questions.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    22
 */
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    23
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    24
import com.sun.net.httpserver.HttpsConfigurator;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    25
import com.sun.net.httpserver.HttpsServer;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    26
import jdk.testlibrary.SimpleSSLContext;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    27
import javax.net.ssl.SSLContext;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
    28
import java.net.InetAddress;
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    29
import java.net.InetSocketAddress;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    30
import java.net.ProxySelector;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    31
import java.net.URI;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    32
import java.net.http.HttpClient;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    33
import java.net.http.HttpClient.Version;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    34
import java.net.http.HttpRequest;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    35
import java.net.http.HttpResponse;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56128
diff changeset
    36
import java.net.http.HttpResponse.BodyHandlers;
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    37
import java.util.Arrays;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    38
import java.util.List;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    39
import java.util.stream.Collectors;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    40
import java.util.stream.Stream;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    41
import static java.lang.String.format;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    42
import static java.lang.System.out;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    43
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    44
/**
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    45
 * @test
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    46
 * @summary This test verifies that if an h2 connection going through a
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    47
 *          proxy P is downgraded to HTTP/1.1, then a new h2 request
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    48
 *          going to a different host through the same proxy will not
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    49
 *          be preemptively downgraded. That, is the stack should attempt
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    50
 *          a new h2 connection to the new host.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    51
 * @bug 8196967
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    52
 * @library /lib/testlibrary http2/server
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    53
 * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters DigestEchoServer HttpsTunnelTest
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    54
 * @modules java.net.http/jdk.internal.net.http.common
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    55
 *          java.net.http/jdk.internal.net.http.frame
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    56
 *          java.net.http/jdk.internal.net.http.hpack
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    57
 *          java.logging
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    58
 *          java.base/sun.net.www.http
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    59
 *          java.base/sun.net.www
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    60
 *          java.base/sun.net
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    61
 * @run main/othervm -Djdk.internal.httpclient.debug=true HttpsTunnelTest
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    62
 */
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    63
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    64
public class HttpsTunnelTest implements HttpServerAdapters {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    65
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    66
    static final String data[] = {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    67
        "Lorem ipsum",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    68
        "dolor sit amet",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    69
        "consectetur adipiscing elit, sed do eiusmod tempor",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    70
        "quis nostrud exercitation ullamco",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    71
        "laboris nisi",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    72
        "ut",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    73
        "aliquip ex ea commodo consequat." +
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    74
        "Duis aute irure dolor in reprehenderit in voluptate velit esse" +
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    75
        "cillum dolore eu fugiat nulla pariatur.",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    76
        "Excepteur sint occaecat cupidatat non proident."
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    77
    };
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    78
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    79
    static final SSLContext context;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    80
    static {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    81
        try {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    82
            context = new SimpleSSLContext().get();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    83
            SSLContext.setDefault(context);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    84
        } catch (Exception x) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    85
            throw new ExceptionInInitializerError(x);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    86
        }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    87
    }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    88
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    89
    HttpsTunnelTest() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    90
    }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    91
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    92
    public HttpClient newHttpClient(ProxySelector ps) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    93
        HttpClient.Builder builder = HttpClient
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    94
                .newBuilder()
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    95
                .sslContext(context)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    96
                .proxy(ps);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    97
        return builder.build();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    98
    }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
    99
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   100
    public static void main(String[] args) throws Exception {
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   101
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   102
        HttpsServer server1 = HttpsServer.create(sa, 0);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   103
        server1.setHttpsConfigurator(new HttpsConfigurator(context));
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   104
        HttpTestServer http1Server =
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   105
                HttpTestServer.of(server1);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   106
        http1Server.addHandler(new HttpTestEchoHandler(), "/");
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   107
        http1Server.start();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   108
        HttpTestServer http2Server = HttpTestServer.of(
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   109
                new Http2TestServer("localhost", true, 0));
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   110
        http2Server.addHandler(new HttpTestEchoHandler(), "/");
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   111
        http2Server.start();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   112
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   113
        DigestEchoServer.TunnelingProxy proxy = DigestEchoServer.createHttpsProxyTunnel(
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   114
                DigestEchoServer.HttpAuthSchemeType.NONE);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   115
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   116
        try {
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   117
            URI uri1 = new URI("https://" + http1Server.serverAuthority() + "/foo/https1");
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56167
diff changeset
   118
            URI uri2 = new URI("https://" + http2Server.serverAuthority() + "/foo/https2");
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   119
            ProxySelector ps = ProxySelector.of(proxy.getProxyAddress());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   120
                    //HttpClient.Builder.NO_PROXY;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   121
            HttpsTunnelTest test = new HttpsTunnelTest();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   122
            HttpClient client = test.newHttpClient(ps);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   123
            out.println("Proxy is: " + ps.select(uri2));
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   124
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   125
            List<String> lines = List.of(Arrays.copyOfRange(data, 0, data.length));
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   126
            assert lines.size() == data.length;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   127
            String body = lines.stream().collect(Collectors.joining("\r\n"));
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56128
diff changeset
   128
            HttpRequest.BodyPublisher reqBody = HttpRequest.BodyPublishers.ofString(body);
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   129
            HttpRequest req1 = HttpRequest
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   130
                    .newBuilder(uri1)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   131
                    .version(Version.HTTP_2)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   132
                    .POST(reqBody)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   133
                    .build();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   134
            out.println("\nPosting to HTTP/1.1 server at: " + req1);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56128
diff changeset
   135
            HttpResponse<Stream<String>> response = client.send(req1, BodyHandlers.ofLines());
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   136
            out.println("Checking response...");
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   137
            if (response.statusCode() != 200) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   138
                throw new RuntimeException("Unexpected status code: " + response);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   139
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   140
            if (response.version() != Version.HTTP_1_1) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   141
                throw new RuntimeException("Unexpected protocol version: "
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   142
                        + response.version());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   143
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   144
            List<String> respLines = response.body().collect(Collectors.toList());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   145
            if (!lines.equals(respLines)) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   146
                throw new RuntimeException("Unexpected response 1: " + respLines);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   147
            }
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56128
diff changeset
   148
            HttpRequest.BodyPublisher reqBody2 = HttpRequest.BodyPublishers.ofString(body);
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   149
            HttpRequest req2 = HttpRequest
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   150
                    .newBuilder(uri2)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   151
                    .version(Version.HTTP_2)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   152
                    .POST(reqBody2)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   153
                    .build();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   154
            out.println("\nPosting to HTTP/2 server at: " + req2);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56128
diff changeset
   155
            response = client.send(req2, BodyHandlers.ofLines());
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   156
            out.println("Checking response...");
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   157
            if (response.statusCode() != 200) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   158
                throw new RuntimeException("Unexpected status code: " + response);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   159
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   160
            if (response.version() != Version.HTTP_2) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   161
                throw new RuntimeException("Unexpected protocol version: "
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   162
                        + response.version());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   163
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   164
            respLines = response.body().collect(Collectors.toList());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   165
            if (!lines.equals(respLines)) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   166
                throw new RuntimeException("Unexpected response 2: " + respLines);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   167
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   168
        } catch(Throwable t) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   169
            out.println("Unexpected exception: exiting: " + t);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   170
            t.printStackTrace();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   171
            throw t;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   172
        } finally {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   173
            proxy.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   174
            http1Server.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   175
            http2Server.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   176
        }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   177
    }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   178
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
diff changeset
   179
}