test/jdk/java/net/httpclient/http2/BadHeadersTest.java
author prappo
Mon, 12 Feb 2018 18:45:17 +0000
branchhttp-client-branch
changeset 56104 3420c1bdd254
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: (HttpClient) JDK-fixed 8196962
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     1
/*
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     4
 *
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     8
 *
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    13
 * accompanied this code).
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    14
 *
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    18
 *
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    21
 * questions.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    22
 */
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    23
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    24
/*
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    25
 * @test
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    26
 * @modules java.base/sun.net.www.http
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    27
 *          java.net.http/jdk.internal.net.http.common
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    28
 *          java.net.http/jdk.internal.net.http.frame
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.hpack
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    30
 * @library /lib/testlibrary server
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    31
 * @build Http2TestServer
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    32
 * @build jdk.testlibrary.SimpleSSLContext
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    33
 * @run testng/othervm BadHeadersTest
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    34
 */
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    35
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    36
import jdk.internal.net.http.common.HttpHeadersImpl;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    37
import jdk.internal.net.http.common.Pair;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    38
import jdk.internal.net.http.frame.ContinuationFrame;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    39
import jdk.internal.net.http.frame.HeaderFrame;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    40
import jdk.internal.net.http.frame.HeadersFrame;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    41
import jdk.internal.net.http.frame.Http2Frame;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    42
import jdk.testlibrary.SimpleSSLContext;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    43
import org.testng.annotations.AfterTest;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    44
import org.testng.annotations.BeforeTest;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    45
import org.testng.annotations.DataProvider;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    46
import org.testng.annotations.Test;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    47
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    48
import javax.net.ssl.SSLContext;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    49
import javax.net.ssl.SSLSession;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    50
import java.io.IOException;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    51
import java.io.InputStream;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    52
import java.io.OutputStream;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    53
import java.net.URI;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    54
import java.net.http.HttpClient;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    55
import java.net.http.HttpRequest;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    56
import java.nio.ByteBuffer;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    57
import java.util.ArrayList;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    58
import java.util.List;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    59
import java.util.concurrent.CompletionException;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    60
import java.util.concurrent.atomic.AtomicInteger;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    61
import java.util.function.BiFunction;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    62
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    63
import static java.net.http.HttpRequest.BodyPublisher.fromString;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    64
import static java.net.http.HttpResponse.BodyHandler.asString;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    65
import static jdk.internal.net.http.common.Pair.pair;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    66
import static org.testng.Assert.assertThrows;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    67
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    68
// Code copied from ContinuationFrameTest
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    69
public class BadHeadersTest {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    70
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    71
    private static final List<Pair<String, String>> BAD_HEADERS = List.of(
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    72
            pair(":hello", "GET"),                    // Unknown pseudo-header
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    73
            pair("hell o", "value"),                  // Space in the name
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    74
            pair("hello", "line1\r\n  line2\r\n"),    // Multiline value
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    75
            pair("hello", "DE" + ((char) 0x7F) + "L") // Bad byte in value
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    76
    );
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    77
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    78
    SSLContext sslContext;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    79
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    80
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    81
    String http2URI;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    82
    String https2URI;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    83
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    84
    /**
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    85
     * A function that returns a list of 1) a HEADERS frame ( with an empty
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    86
     * payload ), and 2) a CONTINUATION frame with the actual headers.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    87
     */
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    88
    static BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> oneContinuation =
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    89
            (Integer streamid, List<ByteBuffer> encodedHeaders) -> {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    90
                List<ByteBuffer> empty =  List.of(ByteBuffer.wrap(new byte[0]));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    91
                HeadersFrame hf = new HeadersFrame(streamid, 0, empty);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    92
                ContinuationFrame cf = new ContinuationFrame(streamid,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    93
                                                             HeaderFrame.END_HEADERS,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    94
                                                             encodedHeaders);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    95
                return List.of(hf, cf);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    96
            };
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    97
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    98
    /**
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
    99
     * A function that returns a list of a HEADERS frame followed by a number of
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   100
     * CONTINUATION frames. Each frame contains just a single byte of payload.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   101
     */
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   102
    static BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> byteAtATime =
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   103
            (Integer streamid, List<ByteBuffer> encodedHeaders) -> {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   104
                assert encodedHeaders.get(0).hasRemaining();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   105
                List<Http2Frame> frames = new ArrayList<>();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   106
                ByteBuffer hb = ByteBuffer.wrap(new byte[] {encodedHeaders.get(0).get()});
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   107
                HeadersFrame hf = new HeadersFrame(streamid, 0, hb);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   108
                frames.add(hf);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   109
                for (ByteBuffer bb : encodedHeaders) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   110
                    while (bb.hasRemaining()) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   111
                        List<ByteBuffer> data = List.of(ByteBuffer.wrap(new byte[] {bb.get()}));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   112
                        ContinuationFrame cf = new ContinuationFrame(streamid, 0, data);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   113
                        frames.add(cf);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   114
                    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   115
                }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   116
                frames.get(frames.size() - 1).setFlag(HeaderFrame.END_HEADERS);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   117
                return frames;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   118
            };
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   119
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   120
    @DataProvider(name = "variants")
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   121
    public Object[][] variants() {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   122
        return new Object[][] {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   123
                { http2URI,  false, oneContinuation },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   124
                { https2URI, false, oneContinuation },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   125
                { http2URI,  true,  oneContinuation },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   126
                { https2URI, true,  oneContinuation },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   127
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   128
                { http2URI,  false, byteAtATime },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   129
                { https2URI, false, byteAtATime },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   130
                { http2URI,  true,  byteAtATime },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   131
                { https2URI, true,  byteAtATime },
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   132
        };
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   133
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   134
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   135
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   136
    @Test(dataProvider = "variants")
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   137
    void test(String uri,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   138
              boolean sameClient,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   139
              BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> headerFramesSupplier)
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   140
            throws Exception
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   141
    {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   142
        CFTHttp2TestExchange.setHeaderFrameSupplier(headerFramesSupplier);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   143
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   144
        HttpClient client = null;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   145
        for (int i=0; i< BAD_HEADERS.size(); i++) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   146
            if (!sameClient || client == null)
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   147
                client = HttpClient.newBuilder().sslContext(sslContext).build();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   148
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   149
            HttpRequest request = HttpRequest.newBuilder(URI.create(uri))
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   150
                    .POST(fromString("Hello there!"))
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   151
                    .build();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   152
            final HttpClient cc = client;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   153
            if (i % 2 == 0) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   154
                assertThrows(IOException.class, () -> cc.send(request, asString()));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   155
            } else {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   156
                Throwable t = null;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   157
                try {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   158
                    cc.sendAsync(request, asString()).join();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   159
                } catch (Throwable t0) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   160
                    t = t0;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   161
                }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   162
                if (t == null) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   163
                    throw new AssertionError("An exception was expected");
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   164
                }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   165
                if (t instanceof CompletionException) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   166
                    Throwable c = t.getCause();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   167
                    if (!(c instanceof IOException)) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   168
                        throw new AssertionError("Unexpected exception", c);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   169
                    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   170
                } else if (!(t instanceof IOException)) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   171
                    throw new AssertionError("Unexpected exception", t);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   172
                }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   173
            }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   174
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   175
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   176
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   177
    @BeforeTest
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   178
    public void setup() throws Exception {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   179
        sslContext = new SimpleSSLContext().get();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   180
        if (sslContext == null)
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   181
            throw new AssertionError("Unexpected null sslContext");
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   182
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   183
        http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   184
        http2TestServer.addHandler(new Http2EchoHandler(), "/http2/echo");
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   185
        int port = http2TestServer.getAddress().getPort();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   186
        http2URI = "http://127.0.0.1:" + port + "/http2/echo";
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   187
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   188
        https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   189
        https2TestServer.addHandler(new Http2EchoHandler(), "/https2/echo");
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   190
        port = https2TestServer.getAddress().getPort();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   191
        https2URI = "https://127.0.0.1:" + port + "/https2/echo";
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   192
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   193
        // Override the default exchange supplier with a custom one to enable
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   194
        // particular test scenarios
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   195
        http2TestServer.setExchangeSupplier(CFTHttp2TestExchange::new);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   196
        https2TestServer.setExchangeSupplier(CFTHttp2TestExchange::new);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   197
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   198
        http2TestServer.start();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   199
        https2TestServer.start();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   200
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   201
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   202
    @AfterTest
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   203
    public void teardown() throws Exception {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   204
        http2TestServer.stop();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   205
        https2TestServer.stop();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   206
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   207
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   208
    static class Http2EchoHandler implements Http2Handler {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   209
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   210
        private final AtomicInteger requestNo = new AtomicInteger();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   211
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   212
        @Override
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   213
        public void handle(Http2TestExchange t) throws IOException {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   214
            try (InputStream is = t.getRequestBody();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   215
                 OutputStream os = t.getResponseBody()) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   216
                byte[] bytes = is.readAllBytes();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   217
                int i = requestNo.incrementAndGet();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   218
                Pair<String, String> p = BAD_HEADERS.get(i % BAD_HEADERS.size());
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   219
                t.getResponseHeaders().addHeader(p.first, p.second);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   220
                t.sendResponseHeaders(200, bytes.length);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   221
                os.write(bytes);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   222
            }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   223
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   224
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   225
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   226
    // A custom Http2TestExchangeImpl that overrides sendResponseHeaders to
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   227
    // allow headers to be sent with a number of CONTINUATION frames.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   228
    static class CFTHttp2TestExchange extends Http2TestExchangeImpl {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   229
        static volatile BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> headerFrameSupplier;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   230
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   231
        static void setHeaderFrameSupplier(BiFunction<Integer,List<ByteBuffer>,List<Http2Frame>> hfs) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   232
            headerFrameSupplier = hfs;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   233
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   234
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   235
        CFTHttp2TestExchange(int streamid, String method, HttpHeadersImpl reqheaders,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   236
                             HttpHeadersImpl rspheaders, URI uri, InputStream is,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   237
                             SSLSession sslSession, BodyOutputStream os,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   238
                             Http2TestServerConnection conn, boolean pushAllowed) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   239
            super(streamid, method, reqheaders, rspheaders, uri, is, sslSession,
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   240
                  os, conn, pushAllowed);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   241
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   242
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   243
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   244
        @Override
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   245
        public void sendResponseHeaders(int rCode, long responseLength) throws IOException {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   246
            this.responseLength = responseLength;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   247
            if (responseLength > 0 || responseLength < 0) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   248
                long clen = responseLength > 0 ? responseLength : 0;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   249
                rspheaders.setHeader("Content-length", Long.toString(clen));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   250
            }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   251
            rspheaders.setHeader(":status", Integer.toString(rCode));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   252
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   253
            List<ByteBuffer> encodeHeaders = conn.encodeHeaders(rspheaders);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   254
            List<Http2Frame> headerFrames = headerFrameSupplier.apply(streamid, encodeHeaders);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   255
            assert headerFrames.size() > 0;  // there must always be at least 1
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   256
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   257
            if (responseLength < 0) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   258
                headerFrames.get(headerFrames.size() -1).setFlag(HeadersFrame.END_STREAM);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   259
                os.closeInternal();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   260
            }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   261
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   262
            for (Http2Frame f : headerFrames)
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   263
                conn.outputQ.put(f);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   264
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   265
            os.goodToGo();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   266
            System.err.println("Sent response headers " + rCode);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   267
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   268
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents:
diff changeset
   269
}