test/jdk/java/net/httpclient/InvalidSubscriptionRequest.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @bug 8201186
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @summary Tests an asynchronous BodySubscriber that completes
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *          immediately with a Publisher<List<ByteBuffer>> whose
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 *          subscriber issues bad requests
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    30
 * @library /test/lib http2/server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    31
 * @build jdk.test.lib.net.SimpleSSLContext ReferenceTracker
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
 * @run testng/othervm InvalidSubscriptionRequest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import com.sun.net.httpserver.HttpsServer;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    42
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.net.http.HttpResponse.BodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
import java.net.http.HttpResponse.BodySubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import java.net.http.HttpResponse.BodySubscribers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import java.util.concurrent.CompletionException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import java.util.concurrent.ExecutionException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
import java.util.concurrent.Executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
import java.util.concurrent.Executors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
import java.util.concurrent.Flow;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
import java.util.concurrent.Flow.Publisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
import java.util.function.Supplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
public class InvalidSubscriptionRequest implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    HttpTestServer httpTestServer;    // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
    HttpTestServer httpsTestServer;   // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    HttpTestServer http2TestServer;   // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
    HttpTestServer https2TestServer;  // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    String httpURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
    String httpURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    String httpsURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
    String httpsURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    String http2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    String http2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
    String https2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
    String https2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
    static final int ITERATION_COUNT = 3;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
    // a shared executor helps reduce the amount of threads created by the test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
    static final Executor executor = Executors.newCachedThreadPool();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
    interface BHS extends Supplier<BodyHandler<Publisher<List<ByteBuffer>>>> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        static BHS of(BHS impl, String name) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
            return new BHSImpl(impl, name);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
    static final class BHSImpl implements BHS {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
        final BHS supplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
        final String name;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
        BHSImpl(BHS impl, String name) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
            this.supplier = impl;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
            this.name = name;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
        public String toString() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
            return name;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
        public BodyHandler<Publisher<List<ByteBuffer>>> get() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
            return supplier.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
    static final Supplier<BodyHandler<Publisher<List<ByteBuffer>>>> OF_PUBLISHER_API =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
            BHS.of(BodyHandlers::ofPublisher, "BodyHandlers::ofPublisher");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
    @DataProvider(name = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
    public Object[][] variants() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
        return new Object[][]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
                { httpURI_fixed,    false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
                { httpURI_chunk,    false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
                { httpsURI_fixed,   false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
                { httpsURI_chunk,   false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
                { http2URI_fixed,   false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
                { http2URI_chunk,   false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
                { https2URI_fixed,  false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
                { https2URI_chunk,  false, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
                { httpURI_fixed,    true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                { httpURI_chunk,    true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                { httpsURI_fixed,   true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                { httpsURI_chunk,   true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
                { http2URI_fixed,   true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
                { http2URI_chunk,   true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
                { https2URI_fixed,  true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                { https2URI_chunk,  true, OF_PUBLISHER_API },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
    final ReferenceTracker TRACKER = ReferenceTracker.INSTANCE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
    HttpClient newHttpClient() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        return TRACKER.track(HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
                         .proxy(HttpClient.Builder.NO_PROXY)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
                         .executor(executor)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
                         .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
                         .build());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
    public void testNoBody(String uri, boolean sameClient, BHS handlers) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
                client = newHttpClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
            BodyHandler<Publisher<List<ByteBuffer>>> handler = handlers.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
            HttpResponse<Publisher<List<ByteBuffer>>> response = client.send(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
            // We can reuse our BodySubscribers implementations to subscribe to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
            // Publisher<List<ByteBuffer>>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
            BodySubscriber<String> ofString = new BadBodySubscriber<>(BodySubscribers.ofString(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
            // get the Publisher<List<ByteBuffer>> and
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
            // subscribe to it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
            response.body().subscribe(ofString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
            // Get the final result and compare it with the expected body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
                String body = ofString.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
                assertEquals(body, "");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
                if (uri.endsWith("/chunk")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
                        && response.version() == HttpClient.Version.HTTP_1_1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
                    // with /fixed and 0 length
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                    // there's no need for any call to request()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
                    throw new RuntimeException("Expected IAE not thrown");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
            } catch (Exception x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
                Throwable cause = x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
                if (x instanceof CompletionException || x instanceof ExecutionException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
                    cause = x.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
                if (cause instanceof IllegalArgumentException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
                    System.out.println("Got expected exception: " + cause);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
                } else throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
    public void testNoBodyAsync(String uri, boolean sameClient, BHS handlers) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
                client = newHttpClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
            BodyHandler<Publisher<List<ByteBuffer>>> handler = handlers.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
            // We can reuse our BodySubscribers implementations to subscribe to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
            // Publisher<List<ByteBuffer>>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
            BodySubscriber<String> ofString =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
                    new BadBodySubscriber<>(BodySubscribers.ofString(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
            CompletableFuture<HttpResponse<Publisher<List<ByteBuffer>>>> response =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
                    client.sendAsync(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
            CompletableFuture<String> result = response.thenCompose(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
                            (responsePublisher) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
                                // get the Publisher<List<ByteBuffer>> and
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                                // subscribe to it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
                                responsePublisher.body().subscribe(ofString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
                                return ofString.getBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
                            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
                // Get the final result and compare it with the expected body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
                assertEquals(result.get(), "");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
                if (uri.endsWith("/chunk")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
                        && response.get().version() == HttpClient.Version.HTTP_1_1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
                    // with /fixed and 0 length
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
                    // there's no need for any call to request()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
                    throw new RuntimeException("Expected IAE not thrown");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
            } catch (Exception x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
                Throwable cause = x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
                if (x instanceof CompletionException || x instanceof ExecutionException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
                    cause = x.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
                if (cause instanceof IllegalArgumentException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
                    System.out.println("Got expected exception: " + cause);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                } else throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
    public void testAsString(String uri, boolean sameClient, BHS handlers) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
                client = newHttpClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri+"/withBody"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
            BodyHandler<Publisher<List<ByteBuffer>>> handler = handlers.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
            HttpResponse<Publisher<List<ByteBuffer>>> response = client.send(req, handler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
            // We can reuse our BodySubscribers implementations to subscribe to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
            // Publisher<List<ByteBuffer>>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
            BodySubscriber<String> ofString = new BadBodySubscriber<>(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
                    BodySubscribers.ofString(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
            // get the Publisher<List<ByteBuffer>> and
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
            // subscribe to it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
            response.body().subscribe(ofString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
            // Get the final result and compare it with the expected body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
                String body = ofString.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
                assertEquals(body, WITH_BODY);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
                throw new RuntimeException("Expected IAE not thrown");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
            } catch (Exception x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
                Throwable cause = x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
                if (x instanceof CompletionException || x instanceof ExecutionException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                    cause = x.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
                if (cause instanceof IllegalArgumentException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
                    System.out.println("Got expected exception: " + cause);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
                } else throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
    public void testAsStringAsync(String uri, boolean sameClient, BHS handlers) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
        HttpClient client = null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
        for (int i=0; i< ITERATION_COUNT; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
            if (!sameClient || client == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                client = newHttpClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri+"/withBody"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                    .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
            BodyHandler<Publisher<List<ByteBuffer>>> handler = handlers.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
            // We can reuse our BodySubscribers implementations to subscribe to the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
            // Publisher<List<ByteBuffer>>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
            BodySubscriber<String> ofString =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
                    new BadBodySubscriber<>(BodySubscribers.ofString(UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
            CompletableFuture<String> result = client.sendAsync(req, handler)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                    .thenCompose((responsePublisher) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                        // get the Publisher<List<ByteBuffer>> and
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
                        // subscribe to it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
                        responsePublisher.body().subscribe(ofString);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
                        return ofString.getBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
                    });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
            // Get the final result and compare it with the expected body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
                String body = result.get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
                assertEquals(body, WITH_BODY);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
                throw new RuntimeException("Expected IAE not thrown");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
            } catch (Exception x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
                Throwable cause = x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
                if (x instanceof CompletionException || x instanceof ExecutionException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                    cause = x.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
                if (cause instanceof IllegalArgumentException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
                    System.out.println("Got expected exception: " + cause);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
                } else throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
    static final class BadSubscription implements Flow.Subscription {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
        Flow.Subscription subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
        Executor executor;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
        BadSubscription(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
            this.subscription = subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
        public void request(long n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
            if (executor == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
                subscription.request(-n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
                executor.execute(() -> subscription.request(-n));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
        public void cancel() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
            subscription.cancel();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
    static final class BadBodySubscriber<T> implements BodySubscriber<T> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
        final BodySubscriber<T> subscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
        BadBodySubscriber(BodySubscriber<T> subscriber) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
            this.subscriber = subscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
        public CompletionStage<T> getBody() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
            return subscriber.getBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
        public void onSubscribe(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
            System.out.println("Subscription is: " + subscription);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
            subscriber.onSubscribe(new BadSubscription(subscription));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
        public void onNext(List<ByteBuffer> item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
            subscriber.onNext(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
        public void onError(Throwable throwable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
            subscriber.onError(throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
        public void onComplete() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
            subscriber.onComplete();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
    static String serverAuthority(HttpServer server) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
        return InetAddress.getLoopbackAddress().getHostName() + ":"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
                + server.getAddress().getPort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
        // HTTP/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
        HttpTestHandler h1_fixedLengthHandler = new HTTP_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
        HttpTestHandler h1_chunkHandler = new HTTP_VariableLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
        httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
        httpTestServer.addHandler( h1_fixedLengthHandler, "/http1/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
        httpTestServer.addHandler(h1_chunkHandler,"/http1/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
        httpURI_fixed = "http://" + httpTestServer.serverAuthority() + "/http1/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
        httpURI_chunk = "http://" + httpTestServer.serverAuthority() + "/http1/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
        HttpsServer httpsServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
        httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
        httpsTestServer = HttpTestServer.of(httpsServer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
        httpsTestServer.addHandler(h1_fixedLengthHandler, "/https1/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
        httpsTestServer.addHandler(h1_chunkHandler, "/https1/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
        httpsURI_fixed = "https://" + httpsTestServer.serverAuthority() + "/https1/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
        httpsURI_chunk = "https://" + httpsTestServer.serverAuthority() + "/https1/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
        // HTTP/2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
        HttpTestHandler h2_fixedLengthHandler = new HTTP_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
        HttpTestHandler h2_chunkedHandler = new HTTP_VariableLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
        http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   407
        https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   411
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   412
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   417
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   418
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   419
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   420
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   421
        AssertionError fail = TRACKER.check(500);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   422
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   423
            httpTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   424
            httpsTestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   425
            http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   426
            https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   427
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   428
            if (fail != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   429
                throw fail;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   430
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   431
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   432
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   433
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   434
    static final String WITH_BODY = "Lorem ipsum dolor sit amet, consectetur" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   435
            " adipiscing elit, sed do eiusmod tempor incididunt ut labore et" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   436
            " dolore magna aliqua. Ut enim ad minim veniam, quis nostrud" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   437
            " exercitation ullamco laboris nisi ut aliquip ex ea" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   438
            " commodo consequat. Duis aute irure dolor in reprehenderit in " +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   439
            "voluptate velit esse cillum dolore eu fugiat nulla pariatur." +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   440
            " Excepteur sint occaecat cupidatat non proident, sunt in culpa qui" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   441
            " officia deserunt mollit anim id est laborum.";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   442
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   443
    static class HTTP_FixedLengthHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   444
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   445
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   446
            out.println("HTTP_FixedLengthHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   447
            try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   448
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   449
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   450
            if (t.getRequestURI().getPath().endsWith("/withBody")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   451
                byte[] bytes = WITH_BODY.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   452
                t.sendResponseHeaders(200, bytes.length);  // body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   453
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   454
                    os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   455
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   456
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   457
                t.sendResponseHeaders(200, 0);  //no body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   458
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   459
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   460
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   461
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   462
    static class HTTP_VariableLengthHandler implements HttpTestHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   463
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   464
        public void handle(HttpTestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   465
            out.println("HTTP_VariableLengthHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   466
            try (InputStream is = t.getRequestBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   467
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   468
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   469
            t.sendResponseHeaders(200, -1);  //chunked or variable
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   470
            if (t.getRequestURI().getPath().endsWith("/withBody")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   471
                byte[] bytes = WITH_BODY.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   472
                try (OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   473
                    int chunkLen = bytes.length/10;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   474
                    if (chunkLen == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   475
                        os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   476
                    } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   477
                        int count = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   478
                        for (int i=0; i<10; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   479
                            os.write(bytes, count, chunkLen);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   480
                            os.flush();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   481
                            count += chunkLen;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   482
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   483
                        os.write(bytes, count, bytes.length % chunkLen);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   484
                        count += bytes.length % chunkLen;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   485
                        assert count == bytes.length;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   486
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   487
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   488
            } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   489
                t.getResponseBody().close();   // no body
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   490
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   491
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   492
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   493
}