test/jdk/java/net/httpclient/http2/ServerPush.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56093 22d94c4a3641
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
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
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
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    29
 * @modules java.base/sun.net.www.http
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    33
 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
import java.io.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
import java.net.*;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    38
import java.nio.ByteBuffer;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
import java.nio.file.*;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    40
import java.net.http.*;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    41
import java.net.http.HttpResponse.BodyHandler;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    42
import java.net.http.HttpResponse.BodySubscriber;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    43
import java.net.http.HttpResponse.PushPromiseHandler;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
import java.util.*;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
import java.util.concurrent.*;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    46
import java.util.function.Consumer;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    47
import java.util.function.UnaryOperator;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    48
import org.testng.annotations.AfterTest;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    49
import org.testng.annotations.BeforeTest;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
import org.testng.annotations.Test;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    51
import static java.nio.charset.StandardCharsets.UTF_8;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    52
import static java.net.http.HttpResponse.BodyHandler.asByteArrayConsumer;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    53
import static java.net.http.HttpResponse.BodyHandler.asFile;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56037
diff changeset
    54
import static java.net.http.HttpResponse.BodyHandler.asString;
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    55
import static org.testng.Assert.*;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    56
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
public class ServerPush {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
    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
    61
    static final int FILE_SIZE = 512 * 1024 + 343;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
    static Path tempFile;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    65
    Http2TestServer server;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    66
    URI uri;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    68
    @BeforeTest
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    69
    public void setup() throws Exception {
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    70
        tempFile = TestUtil.getAFile(FILE_SIZE);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    71
        server = new Http2TestServer(false, 0);
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    72
        server.addHandler(new PushHandler(tempFile, LOOPS), "/");
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    73
        System.out.println("Using temp file:" + tempFile);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    74
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    75
        System.err.println("Server listening on port " + server.getAddress().getPort());
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    76
        server.start();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    77
        int port = server.getAddress().getPort();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    78
        uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c");
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    79
    }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    80
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    81
    @AfterTest
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    82
    public void teardown() {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    83
        server.stop();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    84
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents: 37720
diff changeset
    85
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    86
    // Test 1 - custom written push promise handler, everything as a String
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    87
    @Test
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    88
    public void testTypeString() throws Exception {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    89
        String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8);
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    90
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>>
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    91
                resultMap = new ConcurrentHashMap<>();
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    92
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    93
        PushPromiseHandler<String> pph = (initial, pushRequest, acceptor) -> {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    94
            BodyHandler<String> s = BodyHandler.asString(UTF_8);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    95
            CompletableFuture<HttpResponse<String>> cf = acceptor.apply(s);
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    96
            resultMap.put(pushRequest, cf);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    97
        };
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
    98
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
    99
        HttpClient client = HttpClient.newHttpClient();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   100
        HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   101
        CompletableFuture<HttpResponse<String>> cf =
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   102
                client.sendAsync(request, asString(UTF_8), pph);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   103
        cf.join();
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   104
        resultMap.put(request, cf);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   105
        System.err.println("results.size: " + resultMap.size());
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   106
        for (HttpRequest r : resultMap.keySet()) {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   107
            HttpResponse<String> response = resultMap.get(r).join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   108
            assertEquals(response.statusCode(), 200);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   109
            assertEquals(response.body(), tempFileAsString);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   110
        }
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   111
        assertEquals(resultMap.size(), LOOPS + 1);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   112
    }
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   113
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   114
    // Test 2 - of(...) populating the given Map, everything as a String
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   115
    @Test
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   116
    public void testTypeStringOfMap() throws Exception {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   117
        String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   118
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>>
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   119
                resultMap = new ConcurrentHashMap<>();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   120
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   121
        PushPromiseHandler<String> pph =
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   122
                PushPromiseHandler.of(pushPromise -> asString(UTF_8),
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   123
                                      resultMap);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   124
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   125
        HttpClient client = HttpClient.newHttpClient();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   126
        HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   127
        CompletableFuture<HttpResponse<String>> cf =
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   128
                client.sendAsync(request, asString(UTF_8), pph);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   129
        cf.join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   130
        resultMap.put(request, cf);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   131
        System.err.println("results.size: " + resultMap.size());
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   132
        for (HttpRequest r : resultMap.keySet()) {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   133
            HttpResponse<String> response = resultMap.get(r).join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   134
            assertEquals(response.statusCode(), 200);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   135
            assertEquals(response.body(), tempFileAsString);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   136
        }
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   137
        assertEquals(resultMap.size(), LOOPS + 1);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   138
    }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   139
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   140
    // --- Path ---
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   141
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   142
    static final Path dir = Paths.get(".", "serverPush");
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   143
    static BodyHandler<Path> requestToPath(HttpRequest req) {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   144
        URI u = req.uri();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   145
        Path path = Paths.get(dir.toString(), u.getPath());
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   146
        try {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   147
            Files.createDirectories(path.getParent());
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   148
        } catch (IOException ee) {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   149
            throw new UncheckedIOException(ee);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   150
        }
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   151
        return asFile(path);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   152
    }
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   153
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   154
    // Test 3 - custom written push promise handler, everything as a Path
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   155
    @Test
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   156
    public void testTypePath() throws Exception {
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   157
        String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   158
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Path>>> resultsMap
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   159
                = new ConcurrentHashMap<>();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   160
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   161
        PushPromiseHandler<Path> pushPromiseHandler = (initial, pushRequest, acceptor) -> {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   162
            BodyHandler<Path> pp = requestToPath(pushRequest);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   163
            CompletableFuture<HttpResponse<Path>> cf = acceptor.apply(pp);
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   164
            resultsMap.put(pushRequest, cf);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   165
        };
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   166
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   167
        HttpClient client = HttpClient.newHttpClient();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   168
        HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   169
        CompletableFuture<HttpResponse<Path>> cf =
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   170
                client.sendAsync(request, requestToPath(request), pushPromiseHandler);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   171
        cf.join();
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   172
        resultsMap.put(request, cf);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   173
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   174
        for (HttpRequest r : resultsMap.keySet()) {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   175
            HttpResponse<Path> response = resultsMap.get(r).join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   176
            assertEquals(response.statusCode(), 200);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   177
            String fileAsString = new String(Files.readAllBytes(response.body()), UTF_8);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   178
            assertEquals(fileAsString, tempFileAsString);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   179
        }
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   180
        assertEquals(resultsMap.size(),  LOOPS + 1);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   181
    }
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   182
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   183
    // Test 4 - of(...) populating the given Map, everything as a Path
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   184
    @Test
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   185
    public void testTypePathOfMap() throws Exception {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   186
        String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   187
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Path>>> resultsMap
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   188
                = new ConcurrentHashMap<>();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   189
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   190
        PushPromiseHandler<Path> pushPromiseHandler =
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   191
                PushPromiseHandler.of(pushRequest -> requestToPath(pushRequest),
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   192
                        resultsMap);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   193
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   194
        HttpClient client = HttpClient.newHttpClient();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   195
        HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   196
        CompletableFuture<HttpResponse<Path>> cf =
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   197
                client.sendAsync(request, requestToPath(request), pushPromiseHandler);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   198
        cf.join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   199
        resultsMap.put(request, cf);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   200
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   201
        for (HttpRequest r : resultsMap.keySet()) {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   202
            HttpResponse<Path> response = resultsMap.get(r).join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   203
            assertEquals(response.statusCode(), 200);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   204
            String fileAsString = new String(Files.readAllBytes(response.body()), UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   205
            assertEquals(fileAsString, tempFileAsString);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   206
        }
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   207
        assertEquals(resultsMap.size(),  LOOPS + 1);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   208
    }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   209
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   210
    // ---  Consumer<byte[]> ---
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   211
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   212
    static class ByteArrayConsumer implements Consumer<Optional<byte[]>> {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   213
        volatile List<byte[]> listByteArrays = new ArrayList<>();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   214
        volatile byte[] accumulatedBytes;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   215
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   216
        public byte[] getAccumulatedBytes() { return accumulatedBytes; }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   217
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   218
        @Override
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   219
        public void accept(Optional<byte[]> optionalBytes) {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   220
            assert accumulatedBytes == null;
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   221
            if (!optionalBytes.isPresent()) {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   222
                int size = listByteArrays.stream().mapToInt(ba -> ba.length).sum();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   223
                ByteBuffer bb = ByteBuffer.allocate(size);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   224
                listByteArrays.stream().forEach(ba -> bb.put(ba));
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   225
                accumulatedBytes = bb.array();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   226
            } else {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   227
                listByteArrays.add(optionalBytes.get());
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   228
            }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   229
        }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   230
    }
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   231
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   232
    // Test 5 - custom written handler, everything as a consumer of optional byte[]
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   233
    @Test
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   234
    public void testTypeByteArrayConsumer() throws Exception {
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   235
        String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   236
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Void>>> resultsMap
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   237
                = new ConcurrentHashMap<>();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   238
        Map<HttpRequest,ByteArrayConsumer> byteArrayConsumerMap
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   239
                = new ConcurrentHashMap<>();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   240
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   241
        HttpClient client = HttpClient.newHttpClient();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   242
        HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   243
        ByteArrayConsumer bac = new ByteArrayConsumer();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   244
        byteArrayConsumerMap.put(request, bac);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   245
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   246
        PushPromiseHandler<Void> pushPromiseHandler = (initial, pushRequest, acceptor) -> {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   247
            CompletableFuture<HttpResponse<Void>> cf = acceptor.apply(
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   248
                    (statusCode, headers) -> {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   249
                        ByteArrayConsumer bc = new ByteArrayConsumer();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   250
                        byteArrayConsumerMap.put(pushRequest, bc);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   251
                        return BodySubscriber.asByteArrayConsumer(bc); } );
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   252
            resultsMap.put(pushRequest, cf);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   253
        };
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   254
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   255
        CompletableFuture<HttpResponse<Void>> cf =
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   256
                client.sendAsync(request, asByteArrayConsumer(bac), pushPromiseHandler);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   257
        cf.join();
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   258
        resultsMap.put(request, cf);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   259
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   260
        for (HttpRequest r : resultsMap.keySet()) {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   261
            HttpResponse<Void> response = resultsMap.get(r).join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   262
            assertEquals(response.statusCode(), 200);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   263
            byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes();
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   264
            String result = new String(ba, UTF_8);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   265
            assertEquals(result, tempFileAsString);
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   266
        }
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   267
        assertEquals(resultsMap.size(), LOOPS + 1);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   268
    }
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   269
56037
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   270
    // Test 6 - of(...) populating the given Map, everything as a consumer of optional byte[]
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   271
    @Test
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   272
    public void testTypeByteArrayConsumerOfMap() throws Exception {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   273
        String tempFileAsString = new String(Files.readAllBytes(tempFile), UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   274
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<Void>>> resultsMap
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   275
                = new ConcurrentHashMap<>();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   276
        Map<HttpRequest,ByteArrayConsumer> byteArrayConsumerMap
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   277
                = new ConcurrentHashMap<>();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   278
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   279
        HttpClient client = HttpClient.newHttpClient();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   280
        HttpRequest request = HttpRequest.newBuilder(uri).GET().build();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   281
        ByteArrayConsumer bac = new ByteArrayConsumer();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   282
        byteArrayConsumerMap.put(request, bac);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   283
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   284
        PushPromiseHandler<Void> pushPromiseHandler =
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   285
                PushPromiseHandler.of(
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   286
                        pushRequest -> {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   287
                            ByteArrayConsumer bc = new ByteArrayConsumer();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   288
                            byteArrayConsumerMap.put(pushRequest, bc);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   289
                            return BodyHandler.asByteArrayConsumer(bc);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   290
                        },
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   291
                        resultsMap);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   292
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   293
        CompletableFuture<HttpResponse<Void>> cf =
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   294
                client.sendAsync(request, asByteArrayConsumer(bac), pushPromiseHandler);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   295
        cf.join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   296
        resultsMap.put(request, cf);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   297
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   298
        for (HttpRequest r : resultsMap.keySet()) {
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   299
            HttpResponse<Void> response = resultsMap.get(r).join();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   300
            assertEquals(response.statusCode(), 200);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   301
            byte[] ba = byteArrayConsumerMap.get(r).getAccumulatedBytes();
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   302
            String result = new String(ba, UTF_8);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   303
            assertEquals(result, tempFileAsString);
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   304
        }
c026ce4a1a81 http-client-branch: clean up and expand push promise tests
chegar
parents: 56034
diff changeset
   305
        assertEquals(resultsMap.size(), LOOPS + 1);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 55973
diff changeset
   306
    }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
   307
}