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