test/jdk/java/net/httpclient/MappingResponseSubscriber.java
author chegar
Sat, 03 Mar 2018 09:54:31 +0000
branchhttp-client-branch
changeset 56233 1753108d07b9
parent 56167 96fa4f49a9ff
child 56265 ec34ae013fbe
permissions -rw-r--r--
http-client-branch: setReuseAddress(false) for all test ServerSockets
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     1
/*
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     4
 *
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     8
 *
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    13
 * accompanied this code).
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    14
 *
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    18
 *
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    21
 * questions.
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    22
 */
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    23
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    24
/*
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    25
 * @test
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    26
 * @summary Tests mapped response subscriber
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    27
 * @library /lib/testlibrary http2/server
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    28
 * @build jdk.testlibrary.SimpleSSLContext
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    29
 * @modules java.base/sun.net.www.http
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.common
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.frame
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.hpack
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    33
 * @run testng/othervm
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    34
 *       -Djdk.internal.httpclient.debug=true
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    35
 *      MappingResponseSubscriber
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    36
 */
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    37
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    38
import java.io.IOException;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    39
import java.io.InputStream;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    40
import java.io.OutputStream;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    41
import java.net.InetSocketAddress;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    42
import java.net.URI;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    43
import java.nio.ByteBuffer;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    44
import java.util.List;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    45
import java.util.concurrent.CompletionStage;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    46
import java.util.concurrent.Executor;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    47
import java.util.concurrent.Executors;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    48
import java.util.concurrent.Flow;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    49
import com.sun.net.httpserver.HttpExchange;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    50
import com.sun.net.httpserver.HttpHandler;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    51
import com.sun.net.httpserver.HttpServer;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    52
import com.sun.net.httpserver.HttpsConfigurator;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    53
import com.sun.net.httpserver.HttpsServer;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    54
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    55
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    56
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    57
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    58
import java.net.http.HttpResponse.BodyHandler;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
    59
import java.net.http.HttpResponse.BodyHandlers;
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
    60
import java.net.http.HttpResponse.BodySubscribers;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    61
import  java.net.http.HttpResponse.BodySubscriber;
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
    62
import java.util.function.Function;
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    63
import javax.net.ssl.SSLContext;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    64
import jdk.testlibrary.SimpleSSLContext;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    65
import org.testng.annotations.AfterTest;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    66
import org.testng.annotations.BeforeTest;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    67
import org.testng.annotations.DataProvider;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    68
import org.testng.annotations.Test;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    69
import static java.lang.System.out;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    70
import static java.nio.charset.StandardCharsets.UTF_8;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    71
import static org.testng.Assert.assertEquals;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    72
import static org.testng.Assert.assertTrue;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    73
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
    74
public class MappingResponseSubscriber {
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    75
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    76
    SSLContext sslContext;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    77
    HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    78
    HttpsServer httpsTestServer;       // HTTPS/1.1
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    79
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    80
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    81
    String httpURI_fixed;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    82
    String httpURI_chunk;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    83
    String httpsURI_fixed;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    84
    String httpsURI_chunk;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    85
    String http2URI_fixed;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    86
    String http2URI_chunk;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    87
    String https2URI_fixed;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    88
    String https2URI_chunk;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    89
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    90
    static final int ITERATION_COUNT = 10;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    91
    // a shared executor helps reduce the amount of threads created by the test
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    92
    static final Executor executor = Executors.newCachedThreadPool();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    93
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    94
    @DataProvider(name = "variants")
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    95
    public Object[][] variants() {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    96
        return new Object[][]{
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    97
                { httpURI_fixed,    false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    98
                { httpURI_chunk,    false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
    99
                { httpsURI_fixed,   false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   100
                { httpsURI_chunk,   false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   101
                { http2URI_fixed,   false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   102
                { http2URI_chunk,   false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   103
                { https2URI_fixed,  false,},
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   104
                { https2URI_chunk,  false },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   105
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   106
                { httpURI_fixed,    true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   107
                { httpURI_chunk,    true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   108
                { httpsURI_fixed,   true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   109
                { httpsURI_chunk,   true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   110
                { http2URI_fixed,   true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   111
                { http2URI_chunk,   true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   112
                { https2URI_fixed,  true,},
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   113
                { https2URI_chunk,  true },
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   114
        };
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   115
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   116
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   117
    HttpClient newHttpClient() {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   118
        return HttpClient.newBuilder()
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   119
                         .executor(executor)
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   120
                         .sslContext(sslContext)
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   121
                         .build();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   122
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   123
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   124
    @Test(dataProvider = "variants")
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   125
    public void testAsBytes(String uri, boolean sameClient) throws Exception {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   126
        HttpClient client = null;
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   127
        for (int i = 0; i < ITERATION_COUNT; i++) {
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   128
            if (!sameClient || client == null)
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   129
                client = newHttpClient();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   130
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   131
            HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   132
                                         .build();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   133
            BodyHandler<byte[]> handler = new CRSBodyHandler();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   134
            HttpResponse<byte[]> response = client.send(req, handler);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   135
            byte[] body = response.body();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   136
            assertEquals(body, bytes);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   137
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   138
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   139
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   140
    static class CRSBodyHandler implements BodyHandler<byte[]> {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   141
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   142
        public BodySubscriber<byte[]> apply(int statusCode, HttpHeaders responseHeaders) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   143
            assertEquals(statusCode, 200);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   144
            return BodySubscribers.mapping(
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   145
                new CRSBodySubscriber(), (s) -> s.getBytes(UTF_8)
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   146
            );
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   147
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   148
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   149
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   150
    static class CRSBodySubscriber implements BodySubscriber<String> {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   151
        private final BodySubscriber<String> ofString = BodySubscribers.ofString(UTF_8);
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   152
        volatile boolean onSubscribeCalled;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   153
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   154
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   155
        public void onSubscribe(Flow.Subscription subscription) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   156
            //out.println("onSubscribe ");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   157
            onSubscribeCalled = true;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   158
            ofString.onSubscribe(subscription);
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   159
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   160
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   161
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   162
        public void onNext(List<ByteBuffer> item) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   163
           // out.println("onNext " + item);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   164
            assertTrue(onSubscribeCalled);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   165
            ofString.onNext(item);
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   166
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   167
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   168
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   169
        public void onError(Throwable throwable) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   170
            //out.println("onError");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   171
            assertTrue(onSubscribeCalled);
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   172
            ofString.onError(throwable);
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   173
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   174
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   175
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   176
        public void onComplete() {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   177
            //out.println("onComplete");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   178
            assertTrue(onSubscribeCalled, "onComplete called before onSubscribe");
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   179
            ofString.onComplete();
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   180
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   181
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   182
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   183
        public CompletionStage<String> getBody() {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   184
            return ofString.getBody();
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   185
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   186
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   187
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   188
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   189
    @BeforeTest
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   190
    public void setup() throws Exception {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   191
        sslContext = new SimpleSSLContext().get();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   192
        if (sslContext == null)
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   193
            throw new AssertionError("Unexpected null sslContext");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   194
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   195
        // HTTP/1.1
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   196
        HttpHandler h1_fixedLengthHandler = new HTTP1_FixedLengthHandler();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   197
        HttpHandler h1_chunkHandler = new HTTP1_ChunkedHandler();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   198
        InetSocketAddress sa = new InetSocketAddress("localhost", 0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   199
        httpTestServer = HttpServer.create(sa, 0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   200
        httpTestServer.createContext("/http1/fixed", h1_fixedLengthHandler);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   201
        httpTestServer.createContext("/http1/chunk", h1_chunkHandler);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   202
        httpURI_fixed = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/fixed";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   203
        httpURI_chunk = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/chunk";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   204
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   205
        httpsTestServer = HttpsServer.create(sa, 0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   206
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   207
        httpsTestServer.createContext("/https1/fixed", h1_fixedLengthHandler);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   208
        httpsTestServer.createContext("/https1/chunk", h1_chunkHandler);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   209
        httpsURI_fixed = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/fixed";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   210
        httpsURI_chunk = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/chunk";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   211
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   212
        // HTTP/2
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   213
        Http2Handler h2_fixedLengthHandler = new HTTP2_FixedLengthHandler();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   214
        Http2Handler h2_chunkedHandler = new HTTP2_VariableHandler();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   215
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   216
        http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   217
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   218
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   219
        int port = http2TestServer.getAddress().getPort();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   220
        http2URI_fixed = "http://127.0.0.1:" + port + "/http2/fixed";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   221
        http2URI_chunk = "http://127.0.0.1:" + port + "/http2/chunk";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   222
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   223
        https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   224
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   225
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   226
        port = https2TestServer.getAddress().getPort();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   227
        https2URI_fixed = "https://127.0.0.1:" + port + "/https2/fixed";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   228
        https2URI_chunk = "https://127.0.0.1:" + port + "/https2/chunk";
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   229
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   230
        httpTestServer.start();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   231
        httpsTestServer.start();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   232
        http2TestServer.start();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   233
        https2TestServer.start();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   234
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   235
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   236
    @AfterTest
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   237
    public void teardown() throws Exception {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   238
        httpTestServer.stop(0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   239
        httpsTestServer.stop(0);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   240
        http2TestServer.stop();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   241
        https2TestServer.stop();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   242
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   243
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   244
    static byte[] bytes;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   245
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   246
    static {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   247
        bytes = new byte[128 * 1024];
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   248
        int b = 'A';
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   249
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   250
        for (int i=0; i< bytes.length; i++) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   251
            bytes[i] = (byte)b;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   252
            b = b == 'Z'? 'A' : b + 1;
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   253
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   254
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   255
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   256
    static class HTTP1_FixedLengthHandler implements HttpHandler {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   257
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   258
        public void handle(HttpExchange t) throws IOException {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   259
            out.println("HTTP1_FixedLengthHandler received request to " + t.getRequestURI());
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   260
            try (InputStream is = t.getRequestBody()) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   261
                is.readAllBytes();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   262
            }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   263
            t.sendResponseHeaders(200, bytes.length);  //no body
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   264
            OutputStream os = t.getResponseBody();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   265
            os.write(bytes);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   266
            os.close();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   267
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   268
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   269
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   270
    static class HTTP1_ChunkedHandler implements HttpHandler {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   271
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   272
        public void handle(HttpExchange t) throws IOException {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   273
            out.println("HTTP1_ChunkedHandler received request to " + t.getRequestURI());
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   274
            try (InputStream is = t.getRequestBody()) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   275
                is.readAllBytes();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   276
            }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   277
            t.sendResponseHeaders(200, 0); // chunked
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   278
            OutputStream os = t.getResponseBody();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   279
            os.write(bytes);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   280
            os.close();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   281
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   282
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   283
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   284
    static class HTTP2_FixedLengthHandler implements Http2Handler {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   285
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   286
        public void handle(Http2TestExchange t) throws IOException {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   287
            out.println("HTTP2_FixedLengthHandler received request to " + t.getRequestURI());
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   288
            try (InputStream is = t.getRequestBody()) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   289
                is.readAllBytes();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   290
            }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   291
            t.sendResponseHeaders(200, 0); // chunked
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   292
            OutputStream os = t.getResponseBody();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   293
            os.write(bytes);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   294
            os.close();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   295
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   296
    }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   297
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   298
    static class HTTP2_VariableHandler implements Http2Handler {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   299
        @Override
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   300
        public void handle(Http2TestExchange t) throws IOException {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   301
            out.println("HTTP2_VariableHandler received request to " + t.getRequestURI());
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   302
            try (InputStream is = t.getRequestBody()) {
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   303
                is.readAllBytes();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   304
            }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   305
            t.sendResponseHeaders(200, bytes.length);  //no body
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   306
            OutputStream os = t.getResponseBody();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   307
            os.write(bytes);
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   308
            os.close();
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   309
        }
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   310
    }
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   311
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   312
    // -- Compile only. Verifies generic signatures
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   313
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   314
    static final Function<CharSequence,Integer> f1 = subscriber -> 1;
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   315
    static final Function<CharSequence,Number> f2 = subscriber -> 2;
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   316
    static final Function<String,Integer> f3 = subscriber -> 3;
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   317
    static final Function<String,Number> f4 = subscriber -> 4;
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   318
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   319
    public void compileOnly() throws Exception {
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   320
        HttpClient client = null;
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   321
        HttpRequest req = null;
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   322
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   323
        HttpResponse<Integer> r1 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   324
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> 1));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   325
        HttpResponse<Number>  r2 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   326
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> 1));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   327
        HttpResponse<String>  r3 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   328
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> "s"));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   329
        HttpResponse<CharSequence> r4 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   330
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), s -> "s"));
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   331
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   332
        HttpResponse<Integer> x1 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   333
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f1));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   334
        HttpResponse<Number>  x2 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   335
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f1));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   336
        HttpResponse<Number>  x3 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   337
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f2));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   338
        HttpResponse<Integer> x4 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   339
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f3));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   340
        HttpResponse<Number>  x5 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   341
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f3));
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   342
        HttpResponse<Number>  x7 = client.send(req, (c,h) ->
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56159
diff changeset
   343
                BodySubscribers.mapping(BodySubscribers.ofString(UTF_8), f4));
56159
039ab5a71a5c http-client-branch: type paramater review of generic methods
chegar
parents: 56097
diff changeset
   344
    }
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents:
diff changeset
   345
}