test/jdk/java/net/httpclient/http2/BasicTest.java
author chegar
Tue, 17 Apr 2018 08:54:17 -0700
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 50681 4254bed3c09d
child 56451 9585061fdb04
permissions -rw-r--r--
8197564: HTTP Client implementation Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * questions.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    23
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
 * @test
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
 * @bug 8087112
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    27
 * @library /lib/testlibrary server
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
 * @build jdk.testlibrary.SimpleSSLContext
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    29
 * @modules java.base/sun.net.www.http
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    33
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors BasicTest
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    36
import java.io.IOException;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
import java.net.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
import javax.net.ssl.*;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    39
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    40
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    41
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    42
import java.net.http.HttpRequest.BodyPublishers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    43
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    44
import java.net.http.HttpResponse.BodyHandlers;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
import java.nio.file.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
import java.util.concurrent.*;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    47
import java.util.Collections;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    48
import java.util.LinkedList;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    49
import java.util.List;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
import jdk.testlibrary.SimpleSSLContext;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
import org.testng.annotations.Test;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    52
import static java.net.http.HttpClient.Version.HTTP_2;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
@Test
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
public class BasicTest {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
    static int httpPort, httpsPort;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
    static Http2TestServer httpServer, httpsServer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
    static HttpClient client = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    59
    static ExecutorService clientExec;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    60
    static ExecutorService serverExec;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
    static SSLContext sslContext;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    63
    static String pingURIString, httpURIString, httpsURIString;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
    static void initialize() throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
            SimpleSSLContext sslct = new SimpleSSLContext();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
            sslContext = sslct.get();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    69
            client = getClient();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    70
            httpServer = new Http2TestServer(false, 0, serverExec, sslContext);
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 43788
diff changeset
    71
            httpServer.addHandler(new Http2EchoHandler(), "/");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    72
            httpServer.addHandler(new EchoWithPingHandler(), "/ping");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    73
            httpPort = httpServer.getAddress().getPort();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    75
            httpsServer = new Http2TestServer(true, 0, serverExec, sslContext);
45535
4b19310ae4ee 8181867: [tests] Reorganize EchoHandlers
dfuchs
parents: 43788
diff changeset
    76
            httpsServer.addHandler(new Http2EchoHandler(), "/");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
            httpsPort = httpsServer.getAddress().getPort();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    79
            httpURIString = "http://localhost:" + httpPort + "/foo/";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    80
            pingURIString = "http://localhost:" + httpPort + "/ping/";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    81
            httpsURIString = "https://localhost:" + httpsPort + "/bar/";
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
            httpServer.start();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    84
            httpsServer.start();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
        } catch (Throwable e) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
            System.err.println("Throwing now");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
            e.printStackTrace();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
            throw e;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    90
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    91
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    92
    static List<CompletableFuture<Long>> cfs = Collections
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    93
        .synchronizedList( new LinkedList<>());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    94
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    95
    static CompletableFuture<Long> currentCF;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    96
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    97
    static class EchoWithPingHandler extends Http2EchoHandler {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    98
        private final Object lock = new Object();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
    99
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   100
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   101
        public void handle(Http2TestExchange exchange) throws IOException {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   102
            // for now only one ping active at a time. don't want to saturate
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   103
            synchronized(lock) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   104
                CompletableFuture<Long> cf = currentCF;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   105
                if (cf == null || cf.isDone()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   106
                    cf = exchange.sendPing();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   107
                    assert cf != null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   108
                    cfs.add(cf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   109
                    currentCF = cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   110
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   111
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   112
            super.handle(exchange);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   113
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   114
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   115
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   116
    @Test
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   117
    public static void test() throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   118
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   119
            initialize();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   120
            warmup(false);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   121
            warmup(true);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   122
            simpleTest(false, false);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   123
            simpleTest(false, true);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   124
            simpleTest(true, false);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   125
            streamTest(false);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   126
            streamTest(true);
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   127
            paramsTest();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   128
            CompletableFuture.allOf(cfs.toArray(new CompletableFuture[0])).join();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   129
            synchronized (cfs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   130
                for (CompletableFuture<Long> cf : cfs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   131
                    System.out.printf("Ping ack received in %d millisec\n", cf.get());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   132
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   133
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   134
        } catch (Throwable tt) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   135
            System.err.println("tt caught");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   136
            tt.printStackTrace();
43788
22a618ec8268 8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents: 42460
diff changeset
   137
            throw tt;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   138
        } finally {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   139
            httpServer.stop();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   140
            httpsServer.stop();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   141
            //clientExec.shutdown();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   142
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   143
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   144
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   145
    static HttpClient getClient() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   146
        if (client == null) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   147
            serverExec = Executors.newCachedThreadPool();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   148
            clientExec = Executors.newCachedThreadPool();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   149
            client = HttpClient.newBuilder()
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   150
                               .executor(clientExec)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   151
                               .sslContext(sslContext)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   152
                               .version(HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   153
                               .build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   154
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   155
        return client;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   156
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   157
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   158
    static URI getURI(boolean secure) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   159
        return getURI(secure, false);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   160
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   161
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   162
    static URI getURI(boolean secure, boolean ping) {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   163
        if (secure)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   164
            return URI.create(httpsURIString);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   165
        else
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   166
            return URI.create(ping ? pingURIString: httpURIString);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   167
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   168
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   169
    static void checkStatus(int expected, int found) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   170
        if (expected != found) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   171
            System.err.printf ("Test failed: wrong status code %d/%d\n",
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   172
                expected, found);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   173
            throw new RuntimeException("Test failed");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   174
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   175
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   176
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   177
    static void checkStrings(String expected, String found) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   178
        if (!expected.equals(found)) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   179
            System.err.printf ("Test failed: wrong string %s/%s\n",
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   180
                expected, found);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   181
            throw new RuntimeException("Test failed");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   182
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   183
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   184
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   185
    static Void compareFiles(Path path1, Path path2) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   186
        return TestUtil.compareFiles(path1, path2);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   187
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   188
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   189
    static Path tempFile() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   190
        return TestUtil.tempFile();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   191
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   192
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   193
    static final String SIMPLE_STRING = "Hello world Goodbye world";
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   194
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   195
    static final int LOOPS = 13;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   196
    static final int FILESIZE = 64 * 1024 + 200;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   197
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   198
    static void streamTest(boolean secure) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   199
        URI uri = getURI(secure);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   200
        System.err.printf("streamTest %b to %s\n" , secure, uri);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   201
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   202
        HttpClient client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   203
        Path src = TestUtil.getAFile(FILESIZE * 4);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   204
        HttpRequest req = HttpRequest.newBuilder(uri)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   205
                                     .POST(BodyPublishers.ofFile(src))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   206
                                     .build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   207
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   208
        Path dest = Paths.get("streamtest.txt");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   209
        dest.toFile().delete();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   210
        CompletableFuture<Path> response = client.sendAsync(req, BodyHandlers.ofFile(dest))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   211
                .thenApply(resp -> {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   212
                    if (resp.statusCode() != 200)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   213
                        throw new RuntimeException();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   214
                    return resp.body();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   215
                });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   216
        response.join();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   217
        compareFiles(src, dest);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   218
        System.err.println("streamTest: DONE");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   219
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   220
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   221
    static void paramsTest() throws Exception {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   222
        httpsServer.addHandler((t -> {
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   223
            SSLSession s = t.getSSLSession();
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   224
            String prot = s.getProtocol();
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   225
            if (prot.equals("TLSv1.2")) {
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   226
                t.sendResponseHeaders(200, -1);
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   227
            } else {
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   228
                System.err.printf("Protocols =%s\n", prot);
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   229
                t.sendResponseHeaders(500, -1);
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   230
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   231
        }), "/");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   232
        URI u = new URI("https://localhost:"+httpsPort+"/foo");
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   233
        HttpClient client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   234
        HttpRequest req = HttpRequest.newBuilder(u).build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   235
        HttpResponse<String> resp = client.send(req, BodyHandlers.ofString());
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   236
        int stat = resp.statusCode();
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   237
        if (stat != 200) {
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   238
            throw new RuntimeException("paramsTest failed "
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   239
                + Integer.toString(stat));
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   240
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   241
        System.err.println("paramsTest: DONE");
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   242
    }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   243
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   244
    static void warmup(boolean secure) throws Exception {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   245
        URI uri = getURI(secure);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   246
        System.err.println("Request to " + uri);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   247
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   248
        // Do a simple warmup request
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   249
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   250
        HttpClient client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   251
        HttpRequest req = HttpRequest.newBuilder(uri)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   252
                                     .POST(BodyPublishers.ofString(SIMPLE_STRING))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   253
                                     .build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   254
        HttpResponse<String> response = client.send(req, BodyHandlers.ofString());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   255
        checkStatus(200, response.statusCode());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   256
        String responseBody = response.body();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   257
        HttpHeaders h = response.headers();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   258
        checkStrings(SIMPLE_STRING, responseBody);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   259
        checkStrings(h.firstValue("x-hello").get(), "world");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   260
        checkStrings(h.firstValue("x-bye").get(), "universe");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   261
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   262
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   263
    static void simpleTest(boolean secure, boolean ping) throws Exception {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   264
        URI uri = getURI(secure, ping);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   265
        System.err.println("Request to " + uri);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   266
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   267
        // Do loops asynchronously
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   268
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   269
        CompletableFuture[] responses = new CompletableFuture[LOOPS];
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   270
        final Path source = TestUtil.getAFile(FILESIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   271
        HttpRequest request = HttpRequest.newBuilder(uri)
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   272
                                         .POST(BodyPublishers.ofFile(source))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   273
                                         .build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   274
        for (int i = 0; i < LOOPS; i++) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   275
            responses[i] = client.sendAsync(request, BodyHandlers.ofFile(tempFile()))
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   276
                //.thenApply(resp -> compareFiles(resp.body(), source));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   277
                .thenApply(resp -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   278
                    System.out.printf("Resp status %d body size %d\n",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   279
                                      resp.statusCode(), resp.body().toFile().length());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   280
                    return compareFiles(resp.body(), source);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   281
                });
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   282
            Thread.sleep(100);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   283
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   284
        CompletableFuture.allOf(responses).join();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47242
diff changeset
   285
        System.err.println("simpleTest: DONE");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   286
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   287
}