jdk/test/java/net/httpclient/http2/BasicTest.java
author jjg
Wed, 04 Jan 2017 18:33:20 -0800
changeset 43029 1cd1c816581e
parent 42460 7133f144981a
child 43788 22a618ec8268
permissions -rw-r--r--
8172260: remove tests from ProblemList Reviewed-by: rfield
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
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
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    29
 * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    30
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    31
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    32
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=ssl,requests,responses,errors BasicTest
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
import java.net.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    36
import jdk.incubator.http.*;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    37
import static jdk.incubator.http.HttpClient.Version.HTTP_2;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
import javax.net.ssl.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
import java.nio.file.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
import java.util.concurrent.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
import jdk.testlibrary.SimpleSSLContext;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    42
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromFile;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    43
import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    44
import static jdk.incubator.http.HttpResponse.BodyHandler.asFile;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    45
import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
import org.testng.annotations.Test;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
@Test
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
public class BasicTest {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
    static int httpPort, httpsPort;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
    static Http2TestServer httpServer, httpsServer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
    static HttpClient client = null;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
    static ExecutorService exec;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
    static SSLContext sslContext;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
    static String httpURIString, httpsURIString;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
    static void initialize() throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
            SimpleSSLContext sslct = new SimpleSSLContext();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
            sslContext = sslct.get();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
            client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    64
            httpServer = new Http2TestServer(false, 0, exec, sslContext);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    65
            httpServer.addHandler(new EchoHandler(), "/");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
            httpPort = httpServer.getAddress().getPort();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    68
            httpsServer = new Http2TestServer(true, 0, exec, sslContext);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    69
            httpsServer.addHandler(new EchoHandler(), "/");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    70
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
            httpsPort = httpsServer.getAddress().getPort();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    72
            httpURIString = "http://127.0.0.1:" + httpPort + "/foo/";
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    73
            httpsURIString = "https://127.0.0.1:" + httpsPort + "/bar/";
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    74
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    75
            httpServer.start();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    76
            httpsServer.start();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    77
        } catch (Throwable e) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    78
            System.err.println("Throwing now");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    79
            e.printStackTrace();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
            throw e;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    81
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    82
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    83
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    84
    @Test(timeOut=3000000)
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    85
    public static void test() throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    86
        try {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
            initialize();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
            simpleTest(false);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
            simpleTest(true);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    90
            streamTest(false);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    91
            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
    92
            paramsTest();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    93
            Thread.sleep(1000 * 4);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    94
        } catch (Throwable tt) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    95
            System.err.println("tt caught");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
    96
            tt.printStackTrace();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    97
        } finally {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    98
            httpServer.stop();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    99
            httpsServer.stop();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   100
            exec.shutdownNow();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   101
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   102
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   103
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   104
    static HttpClient getClient() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   105
        if (client == null) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   106
            exec = Executors.newCachedThreadPool();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   107
            client = HttpClient.newBuilder()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   108
                               .executor(exec)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   109
                               .sslContext(sslContext)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   110
                               .version(HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   111
                               .build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   112
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   113
        return client;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   114
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   115
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   116
    static URI getURI(boolean secure) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   117
        if (secure)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   118
            return URI.create(httpsURIString);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   119
        else
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   120
            return URI.create(httpURIString);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   121
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   122
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   123
    static void checkStatus(int expected, int found) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   124
        if (expected != found) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   125
            System.err.printf ("Test failed: wrong status code %d/%d\n",
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   126
                expected, found);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   127
            throw new RuntimeException("Test failed");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   128
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   129
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   130
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   131
    static void checkStrings(String expected, String found) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   132
        if (!expected.equals(found)) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   133
            System.err.printf ("Test failed: wrong string %s/%s\n",
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   134
                expected, found);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   135
            throw new RuntimeException("Test failed");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   136
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   137
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   138
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   139
    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
   140
        return TestUtil.compareFiles(path1, path2);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   141
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   142
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   143
    static Path tempFile() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   144
        return TestUtil.tempFile();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   145
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   146
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   147
    static final String SIMPLE_STRING = "Hello world Goodbye world";
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   148
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   149
    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
   150
    static final int FILESIZE = 64 * 1024 + 200;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   151
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   152
    static void streamTest(boolean secure) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   153
        URI uri = getURI(secure);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   154
        System.err.printf("streamTest %b to %s\n" , secure, uri);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   155
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   156
        HttpClient client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   157
        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
   158
        HttpRequest req = HttpRequest.newBuilder(uri)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   159
                                     .POST(fromFile(src))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   160
                                     .build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   161
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   162
        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
   163
        dest.toFile().delete();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   164
        CompletableFuture<Path> response = client.sendAsync(req, asFile(dest))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   165
                .thenApply(resp -> {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   166
                    if (resp.statusCode() != 200)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   167
                        throw new RuntimeException();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   168
                    return resp.body();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   169
                });
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   170
        response.join();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   171
        compareFiles(src, dest);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   172
        System.err.println("DONE");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   173
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   174
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   175
    static void paramsTest() throws Exception {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   176
        Http2TestServer server = new Http2TestServer(true, 0, exec, sslContext);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   177
        server.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
   178
            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
   179
            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
   180
            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
   181
                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
   182
            } else {
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   183
                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
   184
                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
   185
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   186
        }), "/");
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   187
        server.start();
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   188
        int port = server.getAddress().getPort();
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   189
        URI u = new URI("https://127.0.0.1:"+port+"/foo");
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   190
        HttpClient client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   191
        HttpRequest req = HttpRequest.newBuilder(u).build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   192
        HttpResponse<String> resp = client.send(req, asString());
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   193
        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
   194
        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
   195
            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
   196
                + 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
   197
        }
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   198
    }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   199
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   200
    static void simpleTest(boolean secure) throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   201
        URI uri = getURI(secure);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   202
        System.err.println("Request to " + uri);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   203
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   204
        // Do a simple warmup request
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   205
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   206
        HttpClient client = getClient();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   207
        HttpRequest req = HttpRequest.newBuilder(uri)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   208
                                     .POST(fromString(SIMPLE_STRING))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   209
                                     .build();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   210
        HttpResponse<String> response = client.send(req, asString());
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   211
        HttpHeaders h = response.headers();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   212
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   213
        checkStatus(200, response.statusCode());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   214
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   215
        String responseBody = response.body();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   216
        checkStrings(SIMPLE_STRING, responseBody);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   217
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   218
        checkStrings(h.firstValue("x-hello").get(), "world");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   219
        checkStrings(h.firstValue("x-bye").get(), "universe");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   220
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   221
        // Do loops asynchronously
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   222
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   223
        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
   224
        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
   225
        HttpRequest request = HttpRequest.newBuilder(uri)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   226
                                         .POST(fromFile(tempFile()))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   227
                                         .build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   228
        for (int i = 0; i < LOOPS; i++) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   229
            responses[i] = client.sendAsync(request, asFile(tempFile()))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   230
                //.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
   231
                .thenApply(resp -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   232
                    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
   233
                                      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
   234
                    return compareFiles(resp.body(), source);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 38745
diff changeset
   235
                });
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   236
            Thread.sleep(100);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   237
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   238
        CompletableFuture.allOf(responses).join();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   239
        System.err.println("DONE");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   240
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   241
}