test/jdk/java/net/httpclient/FlowAdapterSubscriberTest.java
author dfuchs
Fri, 23 Feb 2018 16:25:32 +0000
branchhttp-client-branch
changeset 56166 56c52d6417d1
parent 56164 4db4bec0e5bb
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     1
/*
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     4
 *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     8
 *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    13
 * accompanied this code).
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    14
 *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    18
 *
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    21
 * questions.
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    22
 */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    23
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    25
import java.io.IOException;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    26
import java.io.InputStream;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    27
import java.io.OutputStream;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    28
import java.io.UncheckedIOException;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    29
import java.net.InetSocketAddress;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    30
import java.net.URI;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    31
import java.nio.ByteBuffer;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    32
import java.util.Collection;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    33
import java.util.List;
56050
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
    34
import java.util.concurrent.CompletableFuture;
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    35
import java.util.concurrent.Flow;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    36
import java.util.concurrent.Flow.Subscriber;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    37
import java.util.function.Function;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    38
import java.util.function.Supplier;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    39
import com.sun.net.httpserver.HttpExchange;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    40
import com.sun.net.httpserver.HttpHandler;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    41
import com.sun.net.httpserver.HttpServer;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    42
import com.sun.net.httpserver.HttpsConfigurator;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpsServer;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    44
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    45
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    46
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    47
import java.net.http.HttpResponse.BodyHandler;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    48
import java.net.http.HttpResponse.BodySubscriber;
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    49
import jdk.testlibrary.SimpleSSLContext;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    50
import org.testng.annotations.AfterTest;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    51
import org.testng.annotations.BeforeTest;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    52
import org.testng.annotations.DataProvider;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    53
import org.testng.annotations.Test;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    54
import javax.net.ssl.SSLContext;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    55
import static java.nio.charset.StandardCharsets.UTF_8;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    56
import static java.net.http.HttpRequest.BodyPublisher.fromString;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    57
import static java.net.http.HttpResponse.BodySubscriber.asByteArray;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56050
diff changeset
    58
import static java.net.http.HttpResponse.BodySubscriber.asInputStream;
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    59
import static org.testng.Assert.assertEquals;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    60
import static org.testng.Assert.assertThrows;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    61
import static org.testng.Assert.assertTrue;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    62
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    63
/*
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    64
 * @test
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    65
 * @summary Basic tests for Flow adapter Subscribers
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    66
 * @modules java.base/sun.net.www.http
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    67
 *          java.net.http/jdk.internal.net.http.common
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    68
 *          java.net.http/jdk.internal.net.http.frame
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    69
 *          java.net.http/jdk.internal.net.http.hpack
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    70
 *          java.logging
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    71
 *          jdk.httpserver
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    72
 * @library /lib/testlibrary http2/server
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    73
 * @build Http2TestServer
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    74
 * @build jdk.testlibrary.SimpleSSLContext
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    75
 * @run testng/othervm -Djdk.internal.httpclient.debug=true FlowAdapterSubscriberTest
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    76
 */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    77
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    78
public class FlowAdapterSubscriberTest {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    79
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    80
    SSLContext sslContext;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    81
    HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    82
    HttpsServer httpsTestServer;       // HTTPS/1.1
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    83
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    84
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    85
    String httpURI;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    86
    String httpsURI;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    87
    String http2URI;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    88
    String https2URI;
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    89
    static final long start = System.nanoTime();
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    90
    public static String now() {
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    91
        long now = System.nanoTime() - start;
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    92
        long secs = now / 1000_000_000;
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    93
        long mill = (now % 1000_000_000) / 1000_000;
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    94
        long nan = now % 1000_000;
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    95
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
    96
    }
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    97
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    98
    @DataProvider(name = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
    99
    public Object[][] variants() {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   100
        return new Object[][]{
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   101
                { httpURI   },
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   102
                { httpsURI  },
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   103
                { http2URI  },
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   104
                { https2URI },
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   105
        };
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   106
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   107
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   108
    static final Class<NullPointerException> NPE = NullPointerException.class;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   109
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   110
    @Test
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   111
    public void testNull() {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   112
        System.out.printf(now() + "testNull() starting%n");
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   113
        assertThrows(NPE, () -> BodyHandler.fromSubscriber(null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   114
        assertThrows(NPE, () -> BodyHandler.fromSubscriber(null, Function.identity()));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   115
        assertThrows(NPE, () -> BodyHandler.fromSubscriber(new ListSubscriber(), null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   116
        assertThrows(NPE, () -> BodyHandler.fromSubscriber(null, null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   117
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   118
        assertThrows(NPE, () -> BodySubscriber.fromSubscriber(null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   119
        assertThrows(NPE, () -> BodySubscriber.fromSubscriber(null, Function.identity()));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   120
        assertThrows(NPE, () -> BodySubscriber.fromSubscriber(new ListSubscriber(), null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   121
        assertThrows(NPE, () -> BodySubscriber.fromSubscriber(null, null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   122
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   123
        Subscriber subscriber = BodySubscriber.fromSubscriber(new ListSubscriber());
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   124
        assertThrows(NPE, () -> subscriber.onSubscribe(null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   125
        assertThrows(NPE, () -> subscriber.onNext(null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   126
        assertThrows(NPE, () -> subscriber.onError(null));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   127
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   128
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   129
    // List<ByteBuffer>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   130
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   131
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   132
    void testListWithFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   133
        System.out.printf(now() + "testListWithFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   134
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   135
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   136
                .POST(fromString("May the luck of the Irish be with you!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   137
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   138
        ListSubscriber subscriber = new ListSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   139
        HttpResponse<String> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   140
                BodyHandler.fromSubscriber(subscriber, Supplier::get)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   141
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   142
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   143
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   144
        assertEquals(text, "May the luck of the Irish be with you!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   145
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   146
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   147
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   148
    void testListWithoutFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   149
        System.out.printf(now() + "testListWithoutFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   150
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   151
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   152
                .POST(fromString("May the luck of the Irish be with you!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   153
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   154
        ListSubscriber subscriber = new ListSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   155
        HttpResponse<Void> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   156
                BodyHandler.fromSubscriber(subscriber)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   157
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   158
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   159
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   160
        assertEquals(text, "May the luck of the Irish be with you!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   161
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   162
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   163
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   164
    void testListWithFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   165
        System.out.printf(now() + "testListWithFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   166
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   167
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   168
                .POST(fromString("May the luck of the Irish be with you!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   169
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   170
        ListSubscriber subscriber = new ListSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   171
        HttpResponse<String> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   172
                BodyHandler.fromSubscriber(subscriber, Supplier::get));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   173
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   174
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   175
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   176
        assertEquals(text, "May the luck of the Irish be with you!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   177
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   178
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   179
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   180
    void testListWithoutFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   181
        System.out.printf(now() + "testListWithoutFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   182
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   183
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   184
                .POST(fromString("May the luck of the Irish be with you!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   185
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   186
        ListSubscriber subscriber = new ListSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   187
        HttpResponse<Void> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   188
                BodyHandler.fromSubscriber(subscriber));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   189
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   190
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   191
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   192
        assertEquals(text, "May the luck of the Irish be with you!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   193
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   194
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   195
    // Collection<ByteBuffer>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   196
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   197
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   198
    void testCollectionWithFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   199
        System.out.printf(now() + "testCollectionWithFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   200
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   201
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   202
                .POST(fromString("What's the craic?")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   203
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   204
        CollectionSubscriber subscriber = new CollectionSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   205
        HttpResponse<String> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   206
                BodyHandler.fromSubscriber(subscriber, CollectionSubscriber::get)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   207
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   208
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   209
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   210
        assertEquals(text, "What's the craic?");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   211
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   212
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   213
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   214
    void testCollectionWithoutFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   215
        System.out.printf(now() + "testCollectionWithoutFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   216
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   217
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   218
                .POST(fromString("What's the craic?")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   219
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   220
        CollectionSubscriber subscriber = new CollectionSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   221
        HttpResponse<Void> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   222
                BodyHandler.fromSubscriber(subscriber)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   223
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   224
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   225
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   226
        assertEquals(text, "What's the craic?");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   227
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   228
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   229
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   230
    void testCollectionWithFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   231
        System.out.printf(now() + "testCollectionWithFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   232
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   233
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   234
                .POST(fromString("What's the craic?")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   235
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   236
        CollectionSubscriber subscriber = new CollectionSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   237
        HttpResponse<String> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   238
                BodyHandler.fromSubscriber(subscriber, CollectionSubscriber::get));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   239
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   240
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   241
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   242
        assertEquals(text, "What's the craic?");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   243
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   244
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   245
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   246
    void testCollectionWithoutFinisheBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   247
        System.out.printf(now() + "testCollectionWithoutFinisheBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   248
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   249
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   250
                .POST(fromString("What's the craic?")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   251
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   252
        CollectionSubscriber subscriber = new CollectionSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   253
        HttpResponse<Void> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   254
                BodyHandler.fromSubscriber(subscriber));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   255
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   256
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   257
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   258
        assertEquals(text, "What's the craic?");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   259
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   260
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   261
    // Iterable<ByteBuffer>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   262
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   263
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   264
    void testIterableWithFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   265
        System.out.printf(now() + "testIterableWithFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   266
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   267
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   268
                .POST(fromString("We're sucking diesel now!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   269
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   270
        IterableSubscriber subscriber = new IterableSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   271
        HttpResponse<String> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   272
                BodyHandler.fromSubscriber(subscriber, Supplier::get)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   273
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   274
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   275
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   276
        assertEquals(text, "We're sucking diesel now!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   277
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   278
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   279
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   280
    void testIterableWithoutFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   281
        System.out.printf(now() + "testIterableWithoutFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   282
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   283
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   284
                .POST(fromString("We're sucking diesel now!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   285
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   286
        IterableSubscriber subscriber = new IterableSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   287
        HttpResponse<Void> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   288
                BodyHandler.fromSubscriber(subscriber)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   289
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   290
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   291
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   292
        assertEquals(text, "We're sucking diesel now!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   293
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   294
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   295
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   296
    void testIterableWithFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   297
        System.out.printf(now() + "testIterableWithFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   298
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   299
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   300
                .POST(fromString("We're sucking diesel now!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   301
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   302
        IterableSubscriber subscriber = new IterableSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   303
        HttpResponse<String> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   304
                BodyHandler.fromSubscriber(subscriber, Supplier::get));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   305
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   306
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   307
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   308
        assertEquals(text, "We're sucking diesel now!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   309
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   310
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   311
    @Test(dataProvider = "uris")
56164
4db4bec0e5bb http-client-branch: trivial test whitespace cleanup
chegar
parents: 56092
diff changeset
   312
    void testIterableWithoutFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   313
        System.out.printf(now() + "testIterableWithoutFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   314
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   315
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   316
                .POST(fromString("We're sucking diesel now!")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   317
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   318
        IterableSubscriber subscriber = new IterableSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   319
        HttpResponse<Void> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   320
                BodyHandler.fromSubscriber(subscriber));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   321
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   322
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   323
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   324
        assertEquals(text, "We're sucking diesel now!");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   325
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   326
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   327
    // Subscriber<Object>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   328
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   329
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   330
    void testObjectWithFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   331
        System.out.printf(now() + "testObjectWithFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   332
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   333
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   334
                .POST(fromString("May the wind always be at your back.")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   335
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   336
        ObjectSubscriber subscriber = new ObjectSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   337
        HttpResponse<String> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   338
                BodyHandler.fromSubscriber(subscriber, ObjectSubscriber::get)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   339
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   340
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   341
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   342
        assertTrue(text.length() != 0);  // what else can be asserted!
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   343
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   344
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   345
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   346
    void testObjectWithoutFinisher(String url) {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   347
        System.out.printf(now() + "testObjectWithoutFinisher(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   348
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   349
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   350
                .POST(fromString("May the wind always be at your back.")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   351
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   352
        ObjectSubscriber subscriber = new ObjectSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   353
        HttpResponse<Void> response = client.sendAsync(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   354
                BodyHandler.fromSubscriber(subscriber)).join();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   355
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   356
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   357
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   358
        assertTrue(text.length() != 0);  // what else can be asserted!
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   359
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   360
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   361
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   362
    void testObjectWithFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   363
        System.out.printf(now() + "testObjectWithFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   364
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   365
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   366
                .POST(fromString("May the wind always be at your back.")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   367
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   368
        ObjectSubscriber subscriber = new ObjectSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   369
        HttpResponse<String> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   370
                BodyHandler.fromSubscriber(subscriber, ObjectSubscriber::get));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   371
        String text = response.body();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   372
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   373
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   374
        assertTrue(text.length() != 0);  // what else can be asserted!
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   375
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   376
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   377
    @Test(dataProvider = "uris")
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   378
    void testObjectWithoutFinisherBlocking(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   379
        System.out.printf(now() + "testObjectWithoutFinisherBlocking(%s) starting%n", url);
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   380
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   381
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   382
                .POST(fromString("May the wind always be at your back.")).build();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   383
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   384
        ObjectSubscriber subscriber = new ObjectSubscriber();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   385
        HttpResponse<Void> response = client.send(request,
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   386
                BodyHandler.fromSubscriber(subscriber));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   387
        String text = subscriber.get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   388
        System.out.println(text);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   389
        assertEquals(response.statusCode(), 200);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   390
        assertTrue(text.length() != 0);  // what else can be asserted!
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   391
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   392
56050
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   393
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   394
    // -- mapping using convenience handlers
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   395
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   396
    @Test(dataProvider = "uris")
56164
4db4bec0e5bb http-client-branch: trivial test whitespace cleanup
chegar
parents: 56092
diff changeset
   397
    void mappingFromByteArray(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   398
        System.out.printf(now() + "mappingFromByteArray(%s) starting%n", url);
56050
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   399
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   400
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   401
                .POST(fromString("We're sucking diesel now!")).build();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   402
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   403
        client.sendAsync(request, BodyHandler.fromSubscriber(asByteArray(),
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   404
                    bas -> new String(bas.getBody().toCompletableFuture().join(), UTF_8)))
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   405
                .thenApply(FlowAdapterSubscriberTest::assert200ResponseCode)
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   406
                .thenApply(HttpResponse::body)
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   407
                .thenAccept(body -> assertEquals(body, "We're sucking diesel now!"))
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   408
                .join();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   409
    }
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   410
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   411
    @Test(dataProvider = "uris")
56164
4db4bec0e5bb http-client-branch: trivial test whitespace cleanup
chegar
parents: 56092
diff changeset
   412
    void mappingFromInputStream(String url) throws Exception {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56164
diff changeset
   413
        System.out.printf(now() + "mappingFromInputStream(%s) starting%n", url);
56050
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   414
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   415
        HttpRequest request = HttpRequest.newBuilder(URI.create(url))
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   416
                .POST(fromString("May the wind always be at your back.")).build();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   417
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   418
        client.sendAsync(request, BodyHandler.fromSubscriber(asInputStream(),
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   419
                    ins -> {
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   420
                        InputStream is = ins.getBody().toCompletableFuture().join();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   421
                        return new String(uncheckedReadAllBytes(is), UTF_8); } ))
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   422
                .thenApply(FlowAdapterSubscriberTest::assert200ResponseCode)
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   423
                .thenApply(HttpResponse::body)
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   424
                .thenAccept(body -> assertEquals(body, "May the wind always be at your back."))
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   425
                .join();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   426
    }
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   427
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   428
    /** An abstract Subscriber that converts all received data into a String. */
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   429
    static abstract class AbstractSubscriber implements Supplier<String> {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   430
        protected volatile Flow.Subscription subscription;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   431
        protected volatile ByteArrayOutputStream baos = new ByteArrayOutputStream();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   432
        protected volatile String text;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   433
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   434
        public void onSubscribe(Flow.Subscription subscription) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   435
            this.subscription = subscription;
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   436
            subscription.request(Long.MAX_VALUE);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   437
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   438
        public void onError(Throwable throwable) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   439
            throw new RuntimeException(throwable);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   440
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   441
        public void onComplete() {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   442
            text = new String(baos.toByteArray(), UTF_8);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   443
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   444
        @Override public String get() { return text; }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   445
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   446
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   447
    static class ListSubscriber extends AbstractSubscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   448
        implements Flow.Subscriber<List<ByteBuffer>>, Supplier<String>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   449
    {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   450
        @Override public void onNext(List<ByteBuffer> item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   451
            for (ByteBuffer bb : item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   452
                byte[] ba = new byte[bb.remaining()];
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   453
                bb.get(ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   454
                uncheckedWrite(baos, ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   455
            }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   456
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   457
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   458
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   459
    static class CollectionSubscriber extends AbstractSubscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   460
        implements Flow.Subscriber<Collection<ByteBuffer>>, Supplier<String>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   461
    {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   462
        @Override public void onNext(Collection<ByteBuffer> item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   463
            for (ByteBuffer bb : item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   464
                byte[] ba = new byte[bb.remaining()];
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   465
                bb.get(ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   466
                uncheckedWrite(baos, ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   467
            }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   468
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   469
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   470
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   471
    static class IterableSubscriber extends AbstractSubscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   472
        implements Flow.Subscriber<Iterable<ByteBuffer>>, Supplier<String>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   473
    {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   474
        @Override public void onNext(Iterable<ByteBuffer> item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   475
            for (ByteBuffer bb : item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   476
                byte[] ba = new byte[bb.remaining()];
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   477
                bb.get(ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   478
                uncheckedWrite(baos, ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   479
            }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   480
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   481
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   482
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   483
    static class ObjectSubscriber extends AbstractSubscriber
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   484
        implements Flow.Subscriber<Object>, Supplier<String>
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   485
    {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   486
        @Override public void onNext(Object item) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   487
            // What can anyone do with Object, cast or toString it ?
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   488
            uncheckedWrite(baos, item.toString().getBytes(UTF_8));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   489
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   490
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   491
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   492
    static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   493
        try {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   494
            baos.write(ba);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   495
        } catch (IOException e) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   496
            throw new UncheckedIOException(e);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   497
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   498
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   499
56050
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   500
    static byte[] uncheckedReadAllBytes(InputStream is) {
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   501
        try {
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   502
            return is.readAllBytes();
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   503
        } catch (IOException e) {
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   504
            throw new UncheckedIOException(e);
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   505
        }
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   506
    }
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   507
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   508
    static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) {
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   509
        assertEquals(response.statusCode(), 200);
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   510
        return response;
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   511
    }
e4877b1aa02f http-client-branch: expand FlowAdapterSubscriberTest with mapping using convenience handlers
chegar
parents: 48408
diff changeset
   512
48408
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   513
    @BeforeTest
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   514
    public void setup() throws Exception {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   515
        sslContext = new SimpleSSLContext().get();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   516
        if (sslContext == null)
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   517
            throw new AssertionError("Unexpected null sslContext");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   518
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   519
        InetSocketAddress sa = new InetSocketAddress("localhost", 0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   520
        httpTestServer = HttpServer.create(sa, 0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   521
        httpTestServer.createContext("/http1/echo", new Http1EchoHandler());
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   522
        httpURI = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/echo";
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   523
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   524
        httpsTestServer = HttpsServer.create(sa, 0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   525
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   526
        httpsTestServer.createContext("/https1/echo", new Http1EchoHandler());
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   527
        httpsURI = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/echo";
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   528
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   529
        http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   530
        http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   531
        int port = http2TestServer.getAddress().getPort();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   532
        http2URI = "http://127.0.0.1:" + port + "/http2/echo";
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   533
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   534
        https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   535
        https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   536
        port = https2TestServer.getAddress().getPort();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   537
        https2URI = "https://127.0.0.1:" + port + "/https2/echo";
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   538
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   539
        httpTestServer.start();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   540
        httpsTestServer.start();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   541
        http2TestServer.start();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   542
        https2TestServer.start();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   543
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   544
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   545
    @AfterTest
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   546
    public void teardown() throws Exception {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   547
        httpTestServer.stop(0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   548
        httpsTestServer.stop(0);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   549
        http2TestServer.stop();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   550
        https2TestServer.stop();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   551
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   552
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   553
    static class Http1EchoHandler implements HttpHandler {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   554
        @Override
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   555
        public void handle(HttpExchange t) throws IOException {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   556
            try (InputStream is = t.getRequestBody();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   557
                 OutputStream os = t.getResponseBody()) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   558
                byte[] bytes = is.readAllBytes();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   559
                t.sendResponseHeaders(200, bytes.length);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   560
                os.write(bytes);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   561
            }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   562
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   563
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   564
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   565
    static class Http2EchoHandler implements Http2Handler {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   566
        @Override
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   567
        public void handle(Http2TestExchange t) throws IOException {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   568
            try (InputStream is = t.getRequestBody();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   569
                 OutputStream os = t.getResponseBody()) {
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   570
                byte[] bytes = is.readAllBytes();
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   571
                t.sendResponseHeaders(200, bytes.length);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   572
                os.write(bytes);
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   573
            }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   574
        }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   575
    }
4f830b447edf 8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
diff changeset
   576
}