test/jdk/java/net/httpclient/http2/ServerPush.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 42460 jdk/test/java/net/httpclient/http2/ServerPush.java@7133f144981a
child 48083 b1c1b4ef4be2
child 55763 634d8e14c172
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    26
 * @bug 8087112 8159814
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
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: 37720
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: 37720
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: 37720
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: 37720
diff changeset
    32
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush
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.io.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
import java.net.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
import java.nio.file.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
import java.nio.file.attribute.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    39
import jdk.incubator.http.*;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    40
import jdk.incubator.http.HttpResponse.MultiProcessor;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    41
import jdk.incubator.http.HttpResponse.BodyHandler;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
import java.util.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
import java.util.concurrent.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
import org.testng.annotations.Test;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
public class ServerPush {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
    static ExecutorService e = Executors.newCachedThreadPool();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
    static final int LOOPS = 13;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    51
    static final int FILE_SIZE = 512 * 1024 + 343;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
    static Path tempFile;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
    @Test(timeOut=30000)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
    public static void test() throws Exception {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
        Http2TestServer server = null;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    58
        final Path dir = Files.createTempDirectory("serverPush");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
        try {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    60
            server = new Http2TestServer(false, 0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    61
            server.addHandler(new PushHandler(FILE_SIZE, LOOPS), "/");
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
            tempFile = TestUtil.getAFile(FILE_SIZE);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
            System.err.println("Server listening on port " + server.getAddress().getPort());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
            server.start();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
            int port = server.getAddress().getPort();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    67
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    68
            // use multi-level path
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    69
            URI uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    70
            HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    71
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    72
            CompletableFuture<MultiMapResult<Path>> cf =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    73
                HttpClient.newBuilder().version(HttpClient.Version.HTTP_2)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    74
                    .executor(e).build().sendAsync(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    75
                        request, MultiProcessor.asMap((req) -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    76
                            URI u = req.uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    77
                            Path path = Paths.get(dir.toString(), u.getPath());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    78
                            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    79
                                Files.createDirectories(path.getParent());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    80
                            } catch (IOException ee) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    81
                                throw new UncheckedIOException(ee);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    82
                            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    83
                            return Optional.of(BodyHandler.asFile(path));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    84
                        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    85
                    ));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    86
            MultiMapResult<Path> results = cf.get();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    87
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    88
            //HttpResponse resp = request.response();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    89
            System.err.println(results.size());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    90
            Set<HttpRequest> requests = results.keySet();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    91
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    92
            for (HttpRequest r : requests) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    93
                URI u = r.uri();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    94
                Path result = results.get(r).get().body();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    95
                System.err.printf("%s -> %s\n", u.toString(), result.toString());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    96
                TestUtil.compareFiles(result, tempFile);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    97
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    98
            if (requests.size() != LOOPS + 1)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    99
                throw new RuntimeException("some results missing");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   100
            System.out.println("TEST OK: sleeping for 5 sec");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
   101
            Thread.sleep (5 * 1000);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   102
        } finally {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   103
            e.shutdownNow();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   104
            server.stop();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   105
            Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   106
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   107
                    dir.toFile().delete();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   108
                    return FileVisitResult.CONTINUE;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   109
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   110
                public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   111
                    path.toFile().delete();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   112
                    return FileVisitResult.CONTINUE;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   113
                }
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
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   117
}