test/jdk/java/net/httpclient/ThrowingPushPromises.java
author dfuchs
Fri, 09 Mar 2018 11:24:37 +0000
branchhttp-client-branch
changeset 56268 481d8c9acc7f
child 56399 a0929d5dd63f
permissions -rw-r--r--
http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56268
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     1
/*
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     4
 *
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     8
 *
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    14
 *
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    18
 *
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    21
 * questions.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    22
 */
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    23
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    24
/*
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    25
 * @test
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    26
 * @summary Tests what happens when push promise handlers and their
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    27
 *          response body handlers and subscribers throw unexpected exceptions.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    28
 * @library /lib/testlibrary http2/server
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    29
 * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters ThrowingPushPromises
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    30
 * @modules java.base/sun.net.www.http
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.common
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.frame
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.hpack
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    34
 * @run testng/othervm -Djdk.internal.httpclient.debug=true ThrowingPushPromises
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    35
 */
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    36
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    37
import jdk.internal.net.http.common.HttpHeadersImpl;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    38
import jdk.testlibrary.SimpleSSLContext;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    39
import org.testng.annotations.AfterTest;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    40
import org.testng.annotations.AfterClass;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    41
import org.testng.annotations.BeforeTest;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    42
import org.testng.annotations.DataProvider;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    43
import org.testng.annotations.Test;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    44
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    45
import javax.net.ssl.SSLContext;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    46
import java.io.BufferedReader;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    47
import java.io.IOException;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    48
import java.io.InputStream;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    49
import java.io.InputStreamReader;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    50
import java.io.OutputStream;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    51
import java.io.UncheckedIOException;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    52
import java.net.URI;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    53
import java.net.URISyntaxException;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    54
import java.net.http.HttpClient;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    55
import java.net.http.HttpHeaders;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    56
import java.net.http.HttpRequest;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    57
import java.net.http.HttpResponse;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    58
import java.net.http.HttpResponse.BodyHandler;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    59
import java.net.http.HttpResponse.BodyHandlers;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    60
import java.net.http.HttpResponse.BodySubscriber;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    61
import java.net.http.HttpResponse.PushPromiseHandler;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    62
import java.nio.ByteBuffer;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    63
import java.nio.charset.StandardCharsets;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    64
import java.util.List;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    65
import java.util.Map;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    66
import java.util.concurrent.CompletableFuture;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    67
import java.util.concurrent.CompletionException;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    68
import java.util.concurrent.CompletionStage;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    69
import java.util.concurrent.ConcurrentHashMap;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    70
import java.util.concurrent.ConcurrentMap;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    71
import java.util.concurrent.Executor;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    72
import java.util.concurrent.Executors;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    73
import java.util.concurrent.Flow;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    74
import java.util.concurrent.atomic.AtomicLong;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    75
import java.util.function.Consumer;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    76
import java.util.function.Function;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    77
import java.util.function.Predicate;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    78
import java.util.function.Supplier;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    79
import java.util.stream.Collectors;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    80
import java.util.stream.Stream;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    81
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    82
import static java.lang.System.out;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    83
import static java.lang.System.err;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    84
import static java.lang.String.format;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    85
import static java.nio.charset.StandardCharsets.UTF_8;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    86
import static org.testng.Assert.assertEquals;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    87
import static org.testng.Assert.assertTrue;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    88
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    89
public class ThrowingPushPromises implements HttpServerAdapters {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    90
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    91
    SSLContext sslContext;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    92
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    93
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    94
    String http2URI_fixed;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    95
    String http2URI_chunk;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    96
    String https2URI_fixed;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    97
    String https2URI_chunk;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    98
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
    99
    static final int ITERATION_COUNT = 1;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   100
    // a shared executor helps reduce the amount of threads created by the test
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   101
    static final Executor executor = new TestExecutor(Executors.newCachedThreadPool());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   102
    static final ConcurrentMap<String, Throwable> FAILURES = new ConcurrentHashMap<>();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   103
    static volatile boolean tasksFailed;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   104
    static final AtomicLong serverCount = new AtomicLong();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   105
    static final AtomicLong clientCount = new AtomicLong();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   106
    static final long start = System.nanoTime();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   107
    public static String now() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   108
        long now = System.nanoTime() - start;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   109
        long secs = now / 1000_000_000;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   110
        long mill = (now % 1000_000_000) / 1000_000;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   111
        long nan = now % 1000_000;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   112
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   113
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   114
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   115
    private volatile HttpClient sharedClient;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   116
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   117
    static class TestExecutor implements Executor {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   118
        final AtomicLong tasks = new AtomicLong();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   119
        Executor executor;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   120
        TestExecutor(Executor executor) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   121
            this.executor = executor;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   122
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   123
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   124
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   125
        public void execute(Runnable command) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   126
            long id = tasks.incrementAndGet();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   127
            executor.execute(() -> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   128
                try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   129
                    command.run();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   130
                } catch (Throwable t) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   131
                    tasksFailed = true;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   132
                    out.printf(now() + "Task %s failed: %s%n", id, t);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   133
                    err.printf(now() + "Task %s failed: %s%n", id, t);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   134
                    FAILURES.putIfAbsent("Task " + id, t);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   135
                    throw t;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   136
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   137
            });
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   138
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   139
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   140
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   141
    @AfterClass
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   142
    static final void printFailedTests() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   143
        out.println("\n=========================");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   144
        try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   145
            out.printf("%n%sCreated %d servers and %d clients%n",
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   146
                    now(), serverCount.get(), clientCount.get());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   147
            if (FAILURES.isEmpty()) return;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   148
            out.println("Failed tests: ");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   149
            FAILURES.entrySet().forEach((e) -> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   150
                out.printf("\t%s: %s%n", e.getKey(), e.getValue());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   151
                e.getValue().printStackTrace(out);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   152
                e.getValue().printStackTrace();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   153
            });
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   154
            if (tasksFailed) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   155
                out.println("WARNING: Some tasks failed");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   156
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   157
        } finally {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   158
            out.println("\n=========================\n");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   159
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   160
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   161
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   162
    private String[] uris() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   163
        return new String[] {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   164
                http2URI_fixed,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   165
                http2URI_chunk,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   166
                https2URI_fixed,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   167
                https2URI_chunk,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   168
        };
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   169
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   170
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   171
    @DataProvider(name = "noThrows")
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   172
    public Object[][] noThrows() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   173
        String[] uris = uris();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   174
        Object[][] result = new Object[uris.length * 2][];
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   175
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   176
        int i = 0;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   177
        for (boolean sameClient : List.of(false, true)) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   178
            for (String uri: uris()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   179
                result[i++] = new Object[] {uri, sameClient};
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   180
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   181
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   182
        assert i == uris.length * 2;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   183
        return result;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   184
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   185
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   186
    @DataProvider(name = "variants")
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   187
    public Object[][] variants() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   188
        String[] uris = uris();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   189
        Object[][] result = new Object[uris.length * 2 * 2][];
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   190
        int i = 0;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   191
        for (Thrower thrower : List.of(
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   192
                new UncheckedIOExceptionThrower(),
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   193
                new UncheckedCustomExceptionThrower())) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   194
            for (boolean sameClient : List.of(false, true)) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   195
                for (String uri : uris()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   196
                    result[i++] = new Object[]{uri, sameClient, thrower};
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   197
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   198
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   199
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   200
        assert i == uris.length * 2 * 2;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   201
        return result;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   202
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   203
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   204
    private HttpClient makeNewClient() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   205
        clientCount.incrementAndGet();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   206
        return HttpClient.newBuilder()
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   207
                .executor(executor)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   208
                .sslContext(sslContext)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   209
                .build();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   210
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   211
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   212
    HttpClient newHttpClient(boolean share) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   213
        if (!share) return makeNewClient();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   214
        HttpClient shared = sharedClient;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   215
        if (shared != null) return shared;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   216
        synchronized (this) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   217
            shared = sharedClient;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   218
            if (shared == null) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   219
                shared = sharedClient = makeNewClient();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   220
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   221
            return shared;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   222
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   223
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   224
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   225
    @Test(dataProvider = "noThrows")
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   226
    public void testNoThrows(String uri, boolean sameClient)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   227
            throws Exception {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   228
        HttpClient client = null;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   229
        out.printf("%ntestNoThrows(%s, %b)%n", uri, sameClient);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   230
        for (int i=0; i< ITERATION_COUNT; i++) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   231
            if (!sameClient || client == null)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   232
                client = newHttpClient(sameClient);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   233
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   234
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   235
                    .build();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   236
            BodyHandler<Stream<String>> handler =
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   237
                    new ThrowingBodyHandler((w) -> {},
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   238
                                            BodyHandlers.ofLines());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   239
            Map<HttpRequest, CompletableFuture<HttpResponse<Stream<String>>>> pushPromises =
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   240
                    new ConcurrentHashMap<>();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   241
            PushPromiseHandler<Stream<String>> pushHandler = new PushPromiseHandler<>() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   242
                @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   243
                public void applyPushPromise(HttpRequest initiatingRequest,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   244
                                             HttpRequest pushPromiseRequest,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   245
                                             Function<BodyHandler<Stream<String>>,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   246
                                                     CompletableFuture<HttpResponse<Stream<String>>>>
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   247
                                                     acceptor) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   248
                    pushPromises.putIfAbsent(pushPromiseRequest, acceptor.apply(handler));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   249
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   250
            };
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   251
            HttpResponse<Stream<String>> response =
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   252
                    client.sendAsync(req, BodyHandlers.ofLines(), pushHandler).get();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   253
            String body = response.body().collect(Collectors.joining("|"));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   254
            assertEquals(URI.create(body).getPath(), URI.create(uri).getPath());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   255
            for (HttpRequest promised : pushPromises.keySet()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   256
                out.printf("%s Received promise: %s%n\tresponse: %s%n",
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   257
                        now(), promised, pushPromises.get(promised).get());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   258
                String promisedBody = pushPromises.get(promised).get().body()
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   259
                        .collect(Collectors.joining("|"));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   260
                assertEquals(promisedBody, promised.uri().toASCIIString());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   261
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   262
            assertEquals(3, pushPromises.size());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   263
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   264
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   265
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   266
    @Test(dataProvider = "variants")
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   267
    public void testThrowingAsString(String uri,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   268
                                     boolean sameClient,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   269
                                     Thrower thrower)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   270
            throws Exception
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   271
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   272
        String test = format("testThrowingAsString(%s, %b, %s)",
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   273
                             uri, sameClient, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   274
        testThrowing(test, uri, sameClient, BodyHandlers::ofString,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   275
                this::checkAsString, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   276
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   277
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   278
    @Test(dataProvider = "variants")
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   279
    public void testThrowingAsLines(String uri,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   280
                                    boolean sameClient,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   281
                                    Thrower thrower)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   282
            throws Exception
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   283
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   284
        String test =  format("testThrowingAsLines(%s, %b, %s)",
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   285
                uri, sameClient, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   286
        testThrowing(test, uri, sameClient, BodyHandlers::ofLines,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   287
                this::checkAsLines, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   288
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   289
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   290
    @Test(dataProvider = "variants")
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   291
    public void testThrowingAsInputStream(String uri,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   292
                                          boolean sameClient,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   293
                                          Thrower thrower)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   294
            throws Exception
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   295
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   296
        String test = format("testThrowingAsInputStream(%s, %b, %s)",
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   297
                uri, sameClient, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   298
        testThrowing(test, uri, sameClient, BodyHandlers::ofInputStream,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   299
                this::checkAsInputStream,  thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   300
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   301
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   302
    private <T,U> void testThrowing(String name, String uri, boolean sameClient,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   303
                                    Supplier<BodyHandler<T>> handlers,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   304
                                    Finisher finisher, Thrower thrower)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   305
            throws Exception
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   306
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   307
        out.printf("%n%s%s%n", now(), name);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   308
        try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   309
            testThrowing(uri, sameClient, handlers, finisher, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   310
        } catch (Error | Exception x) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   311
            FAILURES.putIfAbsent(name, x);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   312
            throw x;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   313
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   314
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   315
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   316
    private <T,U> void testThrowing(String uri, boolean sameClient,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   317
                                    Supplier<BodyHandler<T>> handlers,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   318
                                    Finisher finisher, Thrower thrower)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   319
            throws Exception
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   320
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   321
        HttpClient client = null;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   322
        for (Where where : Where.values()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   323
            if (where == Where.ON_ERROR) continue;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   324
            if (!sameClient || client == null)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   325
                client = newHttpClient(sameClient);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   326
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   327
            HttpRequest req = HttpRequest.
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   328
                    newBuilder(URI.create(uri))
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   329
                    .build();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   330
            ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<T>>> promiseMap =
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   331
                    new ConcurrentHashMap<>();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   332
            Supplier<BodyHandler<T>> throwing = () ->
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   333
                    new ThrowingBodyHandler(where.select(thrower), handlers.get());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   334
            PushPromiseHandler<T> pushHandler = new ThrowingPromiseHandler<>(
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   335
                    where.select(thrower),
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   336
                    PushPromiseHandler.of((r) -> throwing.get(), promiseMap));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   337
            out.println("try throwing in " + where);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   338
            HttpResponse<T> response = null;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   339
            try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   340
                response = client.sendAsync(req, handlers.get(), pushHandler).join();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   341
            } catch (Error | Exception x) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   342
                throw x;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   343
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   344
            if (response != null) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   345
                finisher.finish(where, req.uri(), response, thrower, promiseMap);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   346
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   347
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   348
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   349
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   350
    enum Where {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   351
        BODY_HANDLER, ON_SUBSCRIBE, ON_NEXT, ON_COMPLETE, ON_ERROR, GET_BODY, BODY_CF,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   352
        BEFORE_ACCEPTING, AFTER_ACCEPTING;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   353
        public Consumer<Where> select(Consumer<Where> consumer) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   354
            return new Consumer<Where>() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   355
                @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   356
                public void accept(Where where) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   357
                    if (Where.this == where) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   358
                        consumer.accept(where);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   359
                    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   360
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   361
            };
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   362
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   363
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   364
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   365
    interface Thrower extends Consumer<Where>, Predicate<Throwable> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   366
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   367
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   368
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   369
    interface Finisher<T,U> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   370
        U finish(Where w, URI requestURI, HttpResponse<T> resp, Thrower thrower,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   371
                 Map<HttpRequest, CompletableFuture<HttpResponse<T>>> promises);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   372
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   373
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   374
    final <T,U> U shouldHaveThrown(Where w, HttpResponse<T> resp, Thrower thrower) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   375
        throw new RuntimeException("Expected exception not thrown in " + w);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   376
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   377
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   378
    final List<String> checkAsString(Where w, URI reqURI,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   379
                                    HttpResponse<String> resp,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   380
                                    Thrower thrower,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   381
                                    Map<HttpRequest, CompletableFuture<HttpResponse<String>>> promises) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   382
        Function<HttpResponse<String>, List<String>> extractor =
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   383
                (r) -> List.of(r.body());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   384
        return check(w, reqURI, resp, thrower, promises, extractor);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   385
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   386
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   387
    final List<String> checkAsLines(Where w, URI reqURI,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   388
                                    HttpResponse<Stream<String>> resp,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   389
                                    Thrower thrower,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   390
                                    Map<HttpRequest, CompletableFuture<HttpResponse<Stream<String>>>> promises) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   391
        Function<HttpResponse<Stream<String>>, List<String>> extractor =
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   392
                (r) -> r.body().collect(Collectors.toList());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   393
        return check(w, reqURI, resp, thrower, promises, extractor);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   394
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   395
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   396
    final List<String> checkAsInputStream(Where w, URI reqURI,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   397
                                          HttpResponse<InputStream> resp,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   398
                                          Thrower thrower,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   399
                                          Map<HttpRequest, CompletableFuture<HttpResponse<InputStream>>> promises)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   400
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   401
        Function<HttpResponse<InputStream>, List<String>> extractor = (r) -> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   402
            List<String> result;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   403
            try (InputStream is = r.body()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   404
                result = new BufferedReader(new InputStreamReader(is))
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   405
                        .lines().collect(Collectors.toList());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   406
            } catch (Throwable t) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   407
                throw new CompletionException(t);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   408
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   409
            return result;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   410
        };
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   411
        return check(w, reqURI, resp, thrower, promises, extractor);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   412
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   413
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   414
    private final <T> List<String> check(Where w, URI reqURI,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   415
                                 HttpResponse<T> resp,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   416
                                 Thrower thrower,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   417
                                 Map<HttpRequest, CompletableFuture<HttpResponse<T>>> promises,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   418
                                 Function<HttpResponse<T>, List<String>> extractor)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   419
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   420
        List<String> result = extractor.apply(resp);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   421
        for (HttpRequest req : promises.keySet()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   422
            switch (w) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   423
                case BEFORE_ACCEPTING:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   424
                    throw new RuntimeException("No push promise should have been received" +
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   425
                            " for " + reqURI + " in " + w + ": got " + promises.keySet());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   426
                default:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   427
                    break;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   428
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   429
            HttpResponse<T> presp;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   430
            try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   431
                presp = promises.get(req).join();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   432
            } catch (Error | Exception x) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   433
                Throwable cause = findCause(x, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   434
                if (cause != null) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   435
                    out.println(now() + "Got expected exception in "
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   436
                            + w + ": " + cause);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   437
                    continue;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   438
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   439
                throw x;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   440
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   441
            switch (w) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   442
                case BEFORE_ACCEPTING:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   443
                case AFTER_ACCEPTING:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   444
                case BODY_HANDLER:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   445
                case ON_SUBSCRIBE:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   446
                case GET_BODY:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   447
                case BODY_CF:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   448
                    return shouldHaveThrown(w, presp, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   449
                default:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   450
                    break;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   451
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   452
            List<String> presult = null;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   453
            try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   454
                presult = extractor.apply(presp);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   455
            } catch (Error | Exception x) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   456
                Throwable cause = findCause(x, thrower);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   457
                if (cause != null) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   458
                    out.println(now() + "Got expected exception for "
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   459
                            + req + " in " + w + ": " + cause);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   460
                    continue;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   461
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   462
                throw x;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   463
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   464
            throw new RuntimeException("Expected exception not thrown for "
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   465
                    + req + " in " + w);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   466
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   467
        final int expectedCount;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   468
        switch (w) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   469
            case BEFORE_ACCEPTING:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   470
                expectedCount = 0;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   471
                break;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   472
            default:
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   473
                expectedCount = 3;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   474
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   475
        assertEquals(promises.size(), expectedCount,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   476
                "bad promise count for " + reqURI + " with " + w);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   477
        assertEquals(result, List.of(reqURI.toASCIIString()));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   478
        return result;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   479
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   480
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   481
    private static Throwable findCause(Throwable x,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   482
                                       Predicate<Throwable> filter) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   483
        while (x != null && !filter.test(x)) x = x.getCause();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   484
        return x;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   485
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   486
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   487
    static final class UncheckedCustomExceptionThrower implements Thrower {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   488
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   489
        public void accept(Where where) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   490
            out.println(now() + "Throwing in " + where);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   491
            throw new UncheckedCustomException(where.name());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   492
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   493
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   494
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   495
        public boolean test(Throwable throwable) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   496
            return UncheckedCustomException.class.isInstance(throwable);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   497
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   498
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   499
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   500
        public String toString() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   501
            return "UncheckedCustomExceptionThrower";
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   502
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   503
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   504
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   505
    static final class UncheckedIOExceptionThrower implements Thrower {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   506
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   507
        public void accept(Where where) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   508
            out.println(now() + "Throwing in " + where);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   509
            throw new UncheckedIOException(new CustomIOException(where.name()));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   510
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   511
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   512
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   513
        public boolean test(Throwable throwable) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   514
            return UncheckedIOException.class.isInstance(throwable)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   515
                    && CustomIOException.class.isInstance(throwable.getCause());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   516
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   517
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   518
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   519
        public String toString() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   520
            return "UncheckedIOExceptionThrower";
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   521
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   522
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   523
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   524
    static final class UncheckedCustomException extends RuntimeException {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   525
        UncheckedCustomException(String message) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   526
            super(message);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   527
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   528
        UncheckedCustomException(String message, Throwable cause) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   529
            super(message, cause);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   530
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   531
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   532
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   533
    static final class CustomIOException extends IOException {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   534
        CustomIOException(String message) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   535
            super(message);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   536
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   537
        CustomIOException(String message, Throwable cause) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   538
            super(message, cause);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   539
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   540
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   541
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   542
    static final class ThrowingPromiseHandler<T> implements PushPromiseHandler<T> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   543
        final Consumer<Where> throwing;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   544
        final PushPromiseHandler<T> pushHandler;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   545
        ThrowingPromiseHandler(Consumer<Where> throwing, PushPromiseHandler<T> pushHandler) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   546
            this.throwing = throwing;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   547
            this.pushHandler = pushHandler;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   548
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   549
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   550
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   551
        public void applyPushPromise(HttpRequest initiatingRequest,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   552
                                     HttpRequest pushPromiseRequest,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   553
                                     Function<BodyHandler<T>,
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   554
                                             CompletableFuture<HttpResponse<T>>> acceptor) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   555
            throwing.accept(Where.BEFORE_ACCEPTING);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   556
            pushHandler.applyPushPromise(initiatingRequest, pushPromiseRequest, acceptor);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   557
            throwing.accept(Where.AFTER_ACCEPTING);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   558
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   559
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   560
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   561
    static final class ThrowingBodyHandler<T> implements BodyHandler<T> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   562
        final Consumer<Where> throwing;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   563
        final BodyHandler<T> bodyHandler;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   564
        ThrowingBodyHandler(Consumer<Where> throwing, BodyHandler<T> bodyHandler) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   565
            this.throwing = throwing;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   566
            this.bodyHandler = bodyHandler;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   567
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   568
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   569
        public BodySubscriber<T> apply(int statusCode, HttpHeaders responseHeaders) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   570
            throwing.accept(Where.BODY_HANDLER);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   571
            BodySubscriber<T> subscriber = bodyHandler.apply(statusCode, responseHeaders);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   572
            return new ThrowingBodySubscriber(throwing, subscriber);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   573
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   574
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   575
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   576
    static final class ThrowingBodySubscriber<T> implements BodySubscriber<T> {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   577
        private final BodySubscriber<T> subscriber;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   578
        volatile boolean onSubscribeCalled;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   579
        final Consumer<Where> throwing;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   580
        ThrowingBodySubscriber(Consumer<Where> throwing, BodySubscriber<T> subscriber) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   581
            this.throwing = throwing;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   582
            this.subscriber = subscriber;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   583
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   584
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   585
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   586
        public void onSubscribe(Flow.Subscription subscription) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   587
            //out.println("onSubscribe ");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   588
            onSubscribeCalled = true;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   589
            throwing.accept(Where.ON_SUBSCRIBE);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   590
            subscriber.onSubscribe(subscription);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   591
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   592
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   593
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   594
        public void onNext(List<ByteBuffer> item) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   595
           // out.println("onNext " + item);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   596
            assertTrue(onSubscribeCalled);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   597
            throwing.accept(Where.ON_NEXT);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   598
            subscriber.onNext(item);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   599
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   600
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   601
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   602
        public void onError(Throwable throwable) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   603
            //out.println("onError");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   604
            assertTrue(onSubscribeCalled);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   605
            throwing.accept(Where.ON_ERROR);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   606
            subscriber.onError(throwable);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   607
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   608
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   609
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   610
        public void onComplete() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   611
            //out.println("onComplete");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   612
            assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   613
            throwing.accept(Where.ON_COMPLETE);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   614
            subscriber.onComplete();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   615
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   616
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   617
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   618
        public CompletionStage<T> getBody() {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   619
            throwing.accept(Where.GET_BODY);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   620
            try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   621
                throwing.accept(Where.BODY_CF);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   622
            } catch (Throwable t) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   623
                return CompletableFuture.failedFuture(t);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   624
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   625
            return subscriber.getBody();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   626
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   627
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   628
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   629
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   630
    @BeforeTest
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   631
    public void setup() throws Exception {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   632
        sslContext = new SimpleSSLContext().get();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   633
        if (sslContext == null)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   634
            throw new AssertionError("Unexpected null sslContext");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   635
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   636
        // HTTP/2
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   637
        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   638
        HttpTestHandler h2_chunkedHandler = new HTTP_ChunkedHandler();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   639
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   640
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   641
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   642
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   643
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed/x";
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   644
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk/x";
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   645
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   646
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, 0));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   647
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   648
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   649
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed/x";
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   650
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk/x";
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   651
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   652
        serverCount.addAndGet(2);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   653
        http2TestServer.start();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   654
        https2TestServer.start();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   655
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   656
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   657
    @AfterTest
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   658
    public void teardown() throws Exception {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   659
        sharedClient = null;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   660
        http2TestServer.stop();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   661
        https2TestServer.stop();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   662
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   663
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   664
    private static void pushPromiseFor(HttpTestExchange t, URI requestURI, String pushPath, boolean fixed)
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   665
            throws IOException
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   666
    {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   667
        try {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   668
            URI promise = new URI(requestURI.getScheme(),
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   669
                    requestURI.getAuthority(),
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   670
                    pushPath, null, null);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   671
            byte[] promiseBytes = promise.toASCIIString().getBytes(UTF_8);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   672
            out.printf("TestServer: %s Pushing promise: %s%n", now(), promise);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   673
            err.printf("TestServer: %s Pushing promise: %s%n", now(), promise);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   674
            HttpTestHeaders headers =  HttpTestHeaders.of(new HttpHeadersImpl());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   675
            if (fixed) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   676
                headers.addHeader("Content-length", String.valueOf(promiseBytes.length));
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   677
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   678
            t.serverPush(promise, headers, promiseBytes);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   679
        } catch (URISyntaxException x) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   680
            throw new IOException(x.getMessage(), x);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   681
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   682
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   683
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   684
    static class HTTP_FixedLengthHandler implements HttpTestHandler {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   685
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   686
        public void handle(HttpTestExchange t) throws IOException {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   687
            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   688
            try (InputStream is = t.getRequestBody()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   689
                is.readAllBytes();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   690
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   691
            URI requestURI = t.getRequestURI();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   692
            for (int i = 1; i<2; i++) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   693
                String path = requestURI.getPath() + "/before/promise-" + i;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   694
                pushPromiseFor(t, requestURI, path, true);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   695
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   696
            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   697
            t.sendResponseHeaders(200, resp.length);  //fixed content length
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   698
            try (OutputStream os = t.getResponseBody()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   699
                int bytes = resp.length/3;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   700
                for (int i = 0; i<2; i++) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   701
                    String path = requestURI.getPath() + "/after/promise-" + (i + 2);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   702
                    os.write(resp, i * bytes, bytes);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   703
                    os.flush();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   704
                    pushPromiseFor(t, requestURI, path, true);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   705
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   706
                os.write(resp, 2*bytes, resp.length - 2*bytes);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   707
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   708
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   709
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   710
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   711
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   712
    static class HTTP_ChunkedHandler implements HttpTestHandler {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   713
        @Override
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   714
        public void handle(HttpTestExchange t) throws IOException {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   715
            out.println("HTTP_ChunkedHandler received request to " + t.getRequestURI());
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   716
            byte[] resp = t.getRequestURI().toString().getBytes(StandardCharsets.UTF_8);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   717
            try (InputStream is = t.getRequestBody()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   718
                is.readAllBytes();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   719
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   720
            URI requestURI = t.getRequestURI();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   721
            for (int i = 1; i<2; i++) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   722
                String path = requestURI.getPath() + "/before/promise-" + i;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   723
                pushPromiseFor(t, requestURI, path, false);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   724
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   725
            t.sendResponseHeaders(200, -1); // chunked/variable
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   726
            try (OutputStream os = t.getResponseBody()) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   727
                int bytes = resp.length/3;
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   728
                for (int i = 0; i<2; i++) {
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   729
                    String path = requestURI.getPath() + "/after/promise-" + (i + 2);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   730
                    os.write(resp, i * bytes, bytes);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   731
                    os.flush();
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   732
                    pushPromiseFor(t, requestURI, path, false);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   733
                }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   734
                os.write(resp, 2*bytes, resp.length - 2*bytes);
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   735
            }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   736
        }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   737
    }
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   738
481d8c9acc7f http-client-branch: Add a test that throws unexpected exception in PushPromiseHandler
dfuchs
parents:
diff changeset
   739
}