test/jdk/java/net/httpclient/ConcurrentResponses.java
author chegar
Tue, 23 Jan 2018 12:23:55 +0000
changeset 48703 3eae36c6baa5
child 49765 ee6f7a61f3a5
child 56071 3353cb42b1b4
permissions -rw-r--r--
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data Reviewed-by: dfuchs, michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48703
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     1
/*
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     4
 *
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     8
 *
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    13
 * accompanied this code).
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    14
 *
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    18
 *
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    21
 * questions.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    22
 */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    23
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    24
/*
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    25
 * @test
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    26
 * @bug 8195823
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    27
 * @summary Buffers given to response body subscribers should not contain
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    28
 *          unprocessed HTTP data
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    29
 * @modules java.base/sun.net.www.http
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    30
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.common
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    31
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    32
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    33
 *          java.logging
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    34
 *          jdk.httpserver
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    35
 * @library /lib/testlibrary http2/server
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    36
 * @build Http2TestServer
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    37
 * @build jdk.testlibrary.SimpleSSLContext
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    38
 * @run testng/othervm -Djdk.internal.httpclient.debug=true ConcurrentResponses
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    39
 */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    40
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    41
import java.io.IOException;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    42
import java.io.InputStream;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    43
import java.io.OutputStream;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    44
import java.net.InetSocketAddress;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    45
import java.net.URI;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    46
import java.nio.ByteBuffer;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    47
import java.util.HashMap;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    48
import java.util.List;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    49
import java.util.Map;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    50
import java.util.concurrent.CompletableFuture;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    51
import java.util.concurrent.CompletionStage;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    52
import java.util.concurrent.Flow;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    53
import java.util.stream.IntStream;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    54
import javax.net.ssl.SSLContext;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    55
import com.sun.net.httpserver.HttpExchange;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    56
import com.sun.net.httpserver.HttpHandler;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    57
import com.sun.net.httpserver.HttpServer;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    58
import com.sun.net.httpserver.HttpsConfigurator;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    59
import com.sun.net.httpserver.HttpsServer;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    60
import jdk.incubator.http.HttpClient;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    61
import jdk.incubator.http.HttpRequest;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    62
import jdk.incubator.http.HttpResponse;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    63
import jdk.incubator.http.HttpResponse.BodyHandler;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    64
import jdk.incubator.http.HttpResponse.BodySubscriber;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    65
import jdk.testlibrary.SimpleSSLContext;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    66
import org.testng.annotations.AfterTest;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    67
import org.testng.annotations.BeforeTest;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    68
import org.testng.annotations.DataProvider;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    69
import org.testng.annotations.Test;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    70
import static java.nio.charset.StandardCharsets.UTF_8;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    71
import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    72
import static jdk.incubator.http.HttpResponse.BodyHandler.discard;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    73
import static org.testng.Assert.assertEquals;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    74
import static org.testng.Assert.assertFalse;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    75
import static org.testng.Assert.fail;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    76
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    77
public class ConcurrentResponses {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    78
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    79
    SSLContext sslContext;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    80
    HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    81
    HttpsServer httpsTestServer;       // HTTPS/1.1
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    82
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    83
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    84
    String httpFixedURI, httpsFixedURI, httpChunkedURI, httpsChunkedURI;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    85
    String http2FixedURI, https2FixedURI, http2VariableURI, https2VariableURI;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    86
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    87
    static final int CONCURRENT_REQUESTS = 13;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    88
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    89
    static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    90
    static final int ALPHABET_LENGTH = ALPHABET.length();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    91
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    92
    static final String stringOfLength(int requiredLength) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    93
        StringBuilder sb = new StringBuilder(requiredLength);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    94
        IntStream.range(0, requiredLength)
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    95
                 .mapToObj(i -> ALPHABET.charAt(i % ALPHABET_LENGTH))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    96
                 .forEach(c -> sb.append(c));
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    97
        return sb.toString();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    98
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
    99
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   100
    /** An array of different Strings, to be used as bodies. */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   101
    static final String[] BODIES = bodies();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   102
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   103
    static String[] bodies() {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   104
        String[] bodies = new String[CONCURRENT_REQUESTS];
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   105
        for (int i=0;i<CONCURRENT_REQUESTS; i++) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   106
            // slightly, but still, different bodies
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   107
            bodies[i] = "Request-" + i + "-body-" + stringOfLength((1024) + i);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   108
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   109
        return bodies;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   110
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   111
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   112
    /**
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   113
     * Asserts the given response's status code is 200.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   114
     * Returns a CF that completes with the given response.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   115
     */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   116
    static final <T> CompletionStage<HttpResponse<T>>
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   117
    assert200ResponseCode(HttpResponse<T> response) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   118
        assertEquals(response.statusCode(), 200);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   119
        return CompletableFuture.completedFuture(response);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   120
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   121
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   122
    /**
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   123
     * Asserts that the given response's body is equal to the given body.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   124
     * Returns a CF that completes with the given response.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   125
     */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   126
    static final <T> CompletionStage<HttpResponse<T>>
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   127
    assertbody(HttpResponse<T> response, T body) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   128
        assertEquals(response.body(), body);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   129
        return CompletableFuture.completedFuture(response);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   130
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   131
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   132
    @DataProvider(name = "uris")
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   133
    public Object[][] variants() {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   134
        return new Object[][]{
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   135
                { httpFixedURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   136
                { httpsFixedURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   137
                { httpChunkedURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   138
                { httpsChunkedURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   139
                { http2FixedURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   140
                { https2FixedURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   141
                { http2VariableURI },
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   142
                { https2VariableURI }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   143
        };
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   144
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   145
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   146
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   147
    // The asString implementation accumulates data, below a certain threshold
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   148
    // into the byte buffers it is given.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   149
    @Test(dataProvider = "uris")
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   150
    void testAsString(String uri) throws Exception {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   151
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   152
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   153
        Map<HttpRequest, String> requests = new HashMap<>();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   154
        for (int i=0;i<CONCURRENT_REQUESTS; i++) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   155
            HttpRequest request = HttpRequest.newBuilder(URI.create(uri + "?" + i))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   156
                                             .build();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   157
            requests.put(request, BODIES[i]);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   158
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   159
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   160
        // initial connection to seed the cache so next parallel connections reuse it
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   161
        client.sendAsync(HttpRequest.newBuilder(URI.create(uri)).build(), discard(null)).join();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   162
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   163
        // will reuse connection cached from the previous request ( when HTTP/2 )
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   164
        CompletableFuture.allOf(requests.keySet().parallelStream()
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   165
                .map(request -> client.sendAsync(request, asString()))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   166
                .map(cf -> cf.thenCompose(ConcurrentResponses::assert200ResponseCode))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   167
                .map(cf -> cf.thenCompose(response -> assertbody(response, requests.get(response.request()))))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   168
                .toArray(CompletableFuture<?>[]::new))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   169
                .join();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   170
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   171
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   172
    // The custom subscriber aggressively attacks any area, between the limit
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   173
    // and the capacity, in the byte buffers it is given, by writing 'X' into it.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   174
    @Test(dataProvider = "uris")
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   175
    void testWithCustomSubscriber(String uri) throws Exception {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   176
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   177
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   178
        Map<HttpRequest, String> requests = new HashMap<>();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   179
        for (int i=0;i<CONCURRENT_REQUESTS; i++) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   180
            HttpRequest request = HttpRequest.newBuilder(URI.create(uri + "?" + i))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   181
                    .build();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   182
            requests.put(request, BODIES[i]);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   183
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   184
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   185
        // initial connection to seed the cache so next parallel connections reuse it
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   186
        client.sendAsync(HttpRequest.newBuilder(URI.create(uri)).build(), discard(null)).join();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   187
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   188
        // will reuse connection cached from the previous request ( when HTTP/2 )
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   189
        CompletableFuture.allOf(requests.keySet().parallelStream()
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   190
                .map(request -> client.sendAsync(request, CustomSubscriber.handler))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   191
                .map(cf -> cf.thenCompose(ConcurrentResponses::assert200ResponseCode))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   192
                .map(cf -> cf.thenCompose(response -> assertbody(response, requests.get(response.request()))))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   193
                .toArray(CompletableFuture<?>[]::new))
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   194
                .join();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   195
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   196
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   197
    /**
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   198
     * A subscriber that wraps asString, but mucks with any data between limit
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   199
     * and capacity, if the client mistakenly passes it any that is should not.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   200
     */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   201
    static class CustomSubscriber implements BodySubscriber<String> {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   202
        static final BodyHandler<String> handler = (r,h) -> new CustomSubscriber();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   203
        private final BodySubscriber<String> asString = BodySubscriber.asString(UTF_8);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   204
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   205
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   206
        public CompletionStage<String> getBody() {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   207
            return asString.getBody();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   208
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   209
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   210
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   211
        public void onSubscribe(Flow.Subscription subscription) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   212
            asString.onSubscribe(subscription);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   213
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   214
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   215
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   216
        public void onNext(List<ByteBuffer> buffers) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   217
            // Muck any data beyond the give limit, since there shouldn't
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   218
            // be any of interest to the HTTP Client.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   219
            for (ByteBuffer buffer : buffers) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   220
                if (buffer.limit() != buffer.capacity()) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   221
                    final int limit = buffer.limit();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   222
                    final int position = buffer.position();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   223
                    buffer.position(buffer.limit());
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   224
                    buffer.limit(buffer.capacity());
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   225
                    while (buffer.hasRemaining())
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   226
                        buffer.put((byte)'X');
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   227
                    buffer.position(position); // restore original position
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   228
                    buffer.limit(limit);       // restore original limit
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   229
                }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   230
            }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   231
            asString.onNext(buffers);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   232
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   233
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   234
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   235
        public void onError(Throwable throwable) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   236
            asString.onError(throwable);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   237
            throwable.printStackTrace();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   238
            fail("UNEXPECTED:" + throwable);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   239
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   240
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   241
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   242
        public void onComplete() {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   243
            asString.onComplete();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   244
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   245
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   246
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   247
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   248
    @BeforeTest
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   249
    public void setup() throws Exception {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   250
        sslContext = new SimpleSSLContext().get();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   251
        if (sslContext == null)
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   252
            throw new AssertionError("Unexpected null sslContext");
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   253
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   254
        InetSocketAddress sa = new InetSocketAddress("localhost", 0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   255
        httpTestServer = HttpServer.create(sa, 0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   256
        httpTestServer.createContext("/http1/fixed", new Http1FixedHandler());
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   257
        httpFixedURI = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/fixed";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   258
        httpTestServer.createContext("/http1/chunked", new Http1ChunkedHandler());
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   259
        httpChunkedURI = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/chunked";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   260
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   261
        httpsTestServer = HttpsServer.create(sa, 0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   262
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   263
        httpsTestServer.createContext("/https1/fixed", new Http1FixedHandler());
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   264
        httpsFixedURI = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/fixed";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   265
        httpsTestServer.createContext("/https1/chunked", new Http1ChunkedHandler());
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   266
        httpsChunkedURI = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/chunked";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   267
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   268
        http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   269
        http2TestServer.addHandler(new Http2FixedHandler(), "/http2/fixed");
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   270
        http2FixedURI = "http://127.0.0.1:" + http2TestServer.getAddress().getPort() + "/http2/fixed";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   271
        http2TestServer.addHandler(new Http2VariableHandler(), "/http2/variable");
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   272
        http2VariableURI = "http://127.0.0.1:" + http2TestServer.getAddress().getPort() + "/http2/variable";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   273
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   274
        https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   275
        https2TestServer.addHandler(new Http2FixedHandler(), "/https2/fixed");
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   276
        https2FixedURI = "https://127.0.0.1:" + https2TestServer.getAddress().getPort() + "/https2/fixed";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   277
        https2TestServer.addHandler(new Http2VariableHandler(), "/https2/variable");
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   278
        https2VariableURI = "https://127.0.0.1:" + https2TestServer.getAddress().getPort() + "/https2/variable";
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   279
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   280
        httpTestServer.start();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   281
        httpsTestServer.start();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   282
        http2TestServer.start();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   283
        https2TestServer.start();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   284
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   285
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   286
    @AfterTest
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   287
    public void teardown() throws Exception {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   288
        httpTestServer.stop(0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   289
        httpsTestServer.stop(0);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   290
        http2TestServer.stop();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   291
        https2TestServer.stop();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   292
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   293
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   294
    interface SendResponseHeadersFunction {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   295
        void apply(int responseCode, long responseLength) throws IOException;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   296
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   297
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   298
    // A handler implementation that replies with 200 OK. If the exchange's uri
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   299
    // has a query, then it must be an integer, which is used as an index to
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   300
    // select the particular response body, e.g. /http2/x?5 -> BODIES[5]
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   301
    static void serverHandlerImpl(InputStream inputStream,
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   302
                                  OutputStream outputStream,
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   303
                                  URI uri,
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   304
                                  SendResponseHeadersFunction sendResponseHeadersFunction)
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   305
        throws IOException
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   306
    {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   307
        try (InputStream is = inputStream;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   308
             OutputStream os = outputStream) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   309
            is.readAllBytes();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   310
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   311
            String magicQuery = uri.getQuery();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   312
            if (magicQuery != null) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   313
                int bodyIndex = Integer.valueOf(magicQuery);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   314
                String body = BODIES[bodyIndex];
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   315
                byte[] bytes = body.getBytes(UTF_8);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   316
                sendResponseHeadersFunction.apply(200, bytes.length);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   317
                int offset = 0;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   318
                // Deliberately attempt to reply with several relatively
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   319
                // small data frames ( each write corresponds to its own
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   320
                // data frame ). Additionally, yield, to encourage other
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   321
                // handlers to execute, therefore increasing the likelihood
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   322
                // of multiple different-stream related frames in the
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   323
                // client's read buffer.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   324
                while (offset < bytes.length) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   325
                    int length = Math.min(bytes.length - offset, 64);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   326
                    os.write(bytes, offset, length);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   327
                    os.flush();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   328
                    offset += length;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   329
                    Thread.yield();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   330
                }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   331
            } else {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   332
                sendResponseHeadersFunction.apply(200, 1);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   333
                os.write('A');
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   334
            }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   335
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   336
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   337
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   338
    static class Http1FixedHandler implements HttpHandler {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   339
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   340
        public void handle(HttpExchange t) throws IOException {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   341
            serverHandlerImpl(t.getRequestBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   342
                              t.getResponseBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   343
                              t.getRequestURI(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   344
                              (rcode, length) -> t.sendResponseHeaders(rcode, length));
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   345
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   346
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   347
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   348
    static class Http1ChunkedHandler implements HttpHandler {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   349
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   350
        public void handle(HttpExchange t) throws IOException {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   351
            serverHandlerImpl(t.getRequestBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   352
                              t.getResponseBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   353
                              t.getRequestURI(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   354
                              (rcode, ignored) -> t.sendResponseHeaders(rcode, 0 /*chunked*/));
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   355
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   356
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   357
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   358
    static class Http2FixedHandler implements Http2Handler {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   359
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   360
        public void handle(Http2TestExchange t) throws IOException {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   361
            serverHandlerImpl(t.getRequestBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   362
                              t.getResponseBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   363
                              t.getRequestURI(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   364
                              (rcode, length) -> t.sendResponseHeaders(rcode, length));
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   365
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   366
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   367
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   368
    static class Http2VariableHandler implements Http2Handler {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   369
        @Override
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   370
        public void handle(Http2TestExchange t) throws IOException {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   371
            serverHandlerImpl(t.getRequestBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   372
                              t.getResponseBody(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   373
                              t.getRequestURI(),
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   374
                              (rcode, ignored) -> t.sendResponseHeaders(rcode, 0 /* no Content-Length */));
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   375
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   376
    }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
diff changeset
   377
}