test/jdk/java/net/httpclient/HttpServerAdapters.java
author dfuchs
Thu, 01 Mar 2018 13:36:41 +0000
branchhttp-client-branch
changeset 56218 fd7bd32963ef
parent 56209 43d5ad612710
child 56262 d818a6a8295a
permissions -rw-r--r--
http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     1
/*
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     4
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     8
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    14
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    18
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    21
 * questions.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    22
 */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    23
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    24
import com.sun.net.httpserver.Filter;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    25
import com.sun.net.httpserver.Headers;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    26
import com.sun.net.httpserver.HttpContext;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    27
import com.sun.net.httpserver.HttpExchange;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    28
import com.sun.net.httpserver.HttpHandler;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    29
import com.sun.net.httpserver.HttpServer;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56070
diff changeset
    30
import java.net.http.HttpClient.Version;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    31
import jdk.internal.net.http.common.HttpHeadersImpl;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    33
import java.io.IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    34
import java.io.InputStream;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    35
import java.io.OutputStream;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    36
import java.io.PrintStream;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    37
import java.io.UncheckedIOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    38
import java.math.BigInteger;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    39
import java.net.InetSocketAddress;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    40
import java.net.URI;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    41
import java.util.List;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    42
import java.util.ListIterator;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    43
import java.util.Map;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    44
import java.util.Optional;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    45
import java.util.Set;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    46
import java.util.concurrent.CopyOnWriteArrayList;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    47
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    48
/**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    49
 * Defines an adaptation layers so that a test server handlers and filters
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    50
 * can be implemented independently of the underlying server version.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    51
 * <p>
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    52
 * For instance:
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    53
 * <pre>{@code
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    54
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    55
 *  URI http1URI, http2URI;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    56
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    57
 *  InetSocketAddress sa = new InetSocketAddress("localhost", 0);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    58
 *  HttpTestServer server1 = HttpTestServer.of(HttpServer.create(sa, 0));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    59
 *  HttpTestContext context = server.addHandler(new HttpTestEchoHandler(), "/http1/echo");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    60
 *  http2URI = "http://127.0.0.1:" + server1.getAddress().getPort() + "/http1/echo";
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    61
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    62
 *  Http2TestServer http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    63
 *  HttpTestServer server2 = HttpTestServer.of(http2TestServer);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    64
 *  server2.addHandler(new HttpTestEchoHandler(), "/http2/echo");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    65
 *  http1URI = "http://127.0.0.1:" + server2.getAddress().getPort() + "/http2/echo";
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    66
 *
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    67
 *  }</pre>
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    68
 */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    69
public interface HttpServerAdapters {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    70
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
    71
    static final boolean PRINTSTACK =
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
    72
            Boolean.getBoolean("jdk.internal.httpclient.debug");
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
    73
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    74
    static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    75
        try {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    76
            baos.write(ba);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    77
        } catch (IOException e) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    78
            throw new UncheckedIOException(e);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    79
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    80
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    81
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    82
    static void printBytes(PrintStream out, String prefix, byte[] bytes) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    83
        int padding = 4 + 4 - (bytes.length % 4);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    84
        padding = padding > 4 ? padding - 4 : 4;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    85
        byte[] bigbytes = new byte[bytes.length + padding];
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    86
        System.arraycopy(bytes, 0, bigbytes, padding, bytes.length);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    87
        out.println(prefix + bytes.length + " "
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    88
                    + new BigInteger(bigbytes).toString(16));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    89
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    90
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    91
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    92
     * A version agnostic adapter class for HTTP Headers.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    93
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    94
    public static abstract class HttpTestHeaders {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    95
        public abstract Optional<String> firstValue(String name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    96
        public abstract void addHeader(String name, String value);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    97
        public abstract Set<String> keySet();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    98
        public abstract Set<Map.Entry<String, List<String>>> entrySet();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    99
        public abstract List<String> get(String name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   100
        public abstract boolean containsKey(String name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   101
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   102
        public static HttpTestHeaders of(Headers headers) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   103
            return new Http1TestHeaders(headers);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   104
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   105
        public static HttpTestHeaders of(HttpHeadersImpl headers) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   106
            return new Http2TestHeaders(headers);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   107
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   108
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   109
        private final static class Http1TestHeaders extends HttpTestHeaders {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   110
            private final Headers headers;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   111
            Http1TestHeaders(Headers h) { this.headers = h; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   112
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   113
            public Optional<String> firstValue(String name) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   114
                if (headers.containsKey(name)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   115
                    return Optional.ofNullable(headers.getFirst(name));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   116
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   117
                return Optional.empty();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   118
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   119
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   120
            public void addHeader(String name, String value) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   121
                headers.add(name, value);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   122
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   123
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   124
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   125
            public Set<String> keySet() { return headers.keySet(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   126
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   127
            public Set<Map.Entry<String, List<String>>> entrySet() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   128
                return headers.entrySet();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   129
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   130
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   131
            public List<String> get(String name) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   132
                return headers.get(name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   133
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   134
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   135
            public boolean containsKey(String name) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   136
                return headers.containsKey(name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   137
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   138
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   139
        private final static class Http2TestHeaders extends HttpTestHeaders {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   140
            private final HttpHeadersImpl headers;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   141
            Http2TestHeaders(HttpHeadersImpl h) { this.headers = h; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   142
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   143
            public Optional<String> firstValue(String name) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   144
                return headers.firstValue(name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   145
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   146
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   147
            public void addHeader(String name, String value) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   148
                headers.addHeader(name, value);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   149
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   150
            public Set<String> keySet() { return headers.map().keySet(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   151
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   152
            public Set<Map.Entry<String, List<String>>> entrySet() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   153
                return headers.map().entrySet();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   154
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   155
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   156
            public List<String> get(String name) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   157
                return headers.allValues(name);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   158
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   159
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   160
            public boolean containsKey(String name) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   161
                return headers.firstValue(name).isPresent();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   162
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   163
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   164
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   165
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   166
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   167
     * A version agnostic adapter class for HTTP Server Exchange.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   168
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   169
    public static abstract class HttpTestExchange {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   170
        public abstract Version getServerVersion();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   171
        public abstract Version getExchangeVersion();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   172
        public abstract InputStream   getRequestBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   173
        public abstract OutputStream  getResponseBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   174
        public abstract HttpTestHeaders getRequestHeaders();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   175
        public abstract HttpTestHeaders getResponseHeaders();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   176
        public abstract void sendResponseHeaders(int code, int contentLength) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   177
        public abstract URI getRequestURI();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   178
        public abstract String getRequestMethod();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   179
        public abstract void close();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   180
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   181
        public static HttpTestExchange of(HttpExchange exchange) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   182
            return new Http1TestExchange(exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   183
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   184
        public static HttpTestExchange of(Http2TestExchange exchange) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   185
            return new Http2TestExchangeImpl(exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   186
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   187
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   188
        abstract void doFilter(Filter.Chain chain) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   189
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   190
        // implementations...
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   191
        private static final class Http1TestExchange extends HttpTestExchange {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   192
            private final HttpExchange exchange;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   193
            Http1TestExchange(HttpExchange exch) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   194
                this.exchange = exch;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   195
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   196
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   197
            public Version getServerVersion() { return Version.HTTP_1_1; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   198
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   199
            public Version getExchangeVersion() { return Version.HTTP_1_1; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   200
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   201
            public InputStream getRequestBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   202
                return exchange.getRequestBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   203
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   204
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   205
            public OutputStream getResponseBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   206
                return exchange.getResponseBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   207
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   208
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   209
            public HttpTestHeaders getRequestHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   210
                return HttpTestHeaders.of(exchange.getRequestHeaders());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   211
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   212
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   213
            public HttpTestHeaders getResponseHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   214
                return HttpTestHeaders.of(exchange.getResponseHeaders());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   215
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   216
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   217
            public void sendResponseHeaders(int code, int contentLength) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   218
                if (contentLength == 0) contentLength = -1;
56135
e359a2716711 http-client-branch: Fixed variable lenght logic in HttpServerAdapters.java
dfuchs
parents: 56133
diff changeset
   219
                else if (contentLength < 0) contentLength = 0;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   220
                exchange.sendResponseHeaders(code, contentLength);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   221
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   222
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   223
            void doFilter(Filter.Chain chain) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   224
                chain.doFilter(exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   225
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   226
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   227
            public void close() { exchange.close(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   228
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   229
            public URI getRequestURI() { return exchange.getRequestURI(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   230
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   231
            public String getRequestMethod() { return exchange.getRequestMethod(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   232
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   233
            public String toString() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   234
                return this.getClass().getSimpleName() + ": " + exchange.toString();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   235
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   236
        }
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   237
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   238
        private static final class Http2TestExchangeImpl extends HttpTestExchange {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   239
            private final Http2TestExchange exchange;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   240
            Http2TestExchangeImpl(Http2TestExchange exch) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   241
                this.exchange = exch;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   242
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   243
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   244
            public Version getServerVersion() { return Version.HTTP_2; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   245
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   246
            public Version getExchangeVersion() { return Version.HTTP_2; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   247
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   248
            public InputStream getRequestBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   249
                return exchange.getRequestBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   250
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   251
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   252
            public OutputStream getResponseBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   253
                return exchange.getResponseBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   254
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   255
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   256
            public HttpTestHeaders getRequestHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   257
                return HttpTestHeaders.of(exchange.getRequestHeaders());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   258
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   259
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   260
            public HttpTestHeaders getResponseHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   261
                return HttpTestHeaders.of(exchange.getResponseHeaders());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   262
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   263
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   264
            public void sendResponseHeaders(int code, int contentLength) throws IOException {
56133
be9720a556c7 http-client-branch: Add a test for Handlers/Subscribers as described in JDK-8165220
dfuchs
parents: 56128
diff changeset
   265
                if (contentLength == 0) contentLength = -1;
56135
e359a2716711 http-client-branch: Fixed variable lenght logic in HttpServerAdapters.java
dfuchs
parents: 56133
diff changeset
   266
                else if (contentLength < 0) contentLength = 0;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   267
                exchange.sendResponseHeaders(code, contentLength);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   268
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   269
            void doFilter(Filter.Chain filter) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   270
                throw new IOException("cannot use HTTP/1.1 filter with HTTP/2 server");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   271
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   272
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   273
            public void close() { exchange.close();}
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   274
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   275
            public URI getRequestURI() { return exchange.getRequestURI(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   276
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   277
            public String getRequestMethod() { return exchange.getRequestMethod(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   278
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   279
            public String toString() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   280
                return this.getClass().getSimpleName() + ": " + exchange.toString();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   281
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   282
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   283
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   284
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   285
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   286
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   287
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   288
     * A version agnostic adapter class for HTTP Server Handlers.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   289
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   290
    public interface HttpTestHandler {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   291
        void handle(HttpTestExchange t) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   292
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   293
        default HttpHandler toHttpHandler() {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   294
            return (t) -> doHandle(HttpTestExchange.of(t));
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   295
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   296
        default Http2Handler toHttp2Handler() {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   297
            return (t) -> doHandle(HttpTestExchange.of(t));
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   298
        }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   299
        private void doHandle(HttpTestExchange t) throws IOException {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   300
            try {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   301
                handle(t);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   302
            } catch (Throwable x) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   303
                System.out.println("WARNING: exception caught in HttpTestHandler::handle " + x);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   304
                System.err.println("WARNING: exception caught in HttpTestHandler::handle " + x);
56218
fd7bd32963ef http-client-branch: extend ThrowingPublishers test to throw in the second call of Subsription::request
dfuchs
parents: 56209
diff changeset
   305
                if (PRINTSTACK && !expectException(t)) x.printStackTrace(System.out);
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   306
                throw x;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   307
            }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   308
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   309
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   310
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   311
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   312
    public static class HttpTestEchoHandler implements HttpTestHandler {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   313
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   314
        public void handle(HttpTestExchange t) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   315
            try (InputStream is = t.getRequestBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   316
                 OutputStream os = t.getResponseBody()) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   317
                byte[] bytes = is.readAllBytes();
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56092
diff changeset
   318
                printBytes(System.out,"Echo server got "
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56092
diff changeset
   319
                        + t.getExchangeVersion() + " bytes: ", bytes);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   320
                if (t.getRequestHeaders().firstValue("Content-type").isPresent()) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   321
                    t.getResponseHeaders().addHeader("Content-type",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   322
                            t.getRequestHeaders().firstValue("Content-type").get());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   323
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   324
                t.sendResponseHeaders(200, bytes.length);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   325
                os.write(bytes);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   326
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   327
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   328
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   329
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   330
    public static boolean expectException(HttpTestExchange e) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   331
        HttpTestHeaders h = e.getRequestHeaders();
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   332
        Optional<String> expectException = h.firstValue("X-expect-exception");
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   333
        if (expectException.isPresent()) {
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   334
            return expectException.get().equalsIgnoreCase("true");
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   335
        }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   336
        return false;
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   337
    }
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   338
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   339
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   340
     * A version agnostic adapter class for HTTP Server Filter Chains.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   341
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   342
    public abstract class HttpChain {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   343
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   344
        public abstract void doFilter(HttpTestExchange exchange) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   345
        public static HttpChain of(Filter.Chain chain) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   346
            return new Http1Chain(chain);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   347
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   348
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   349
        public static HttpChain of(List<HttpTestFilter> filters, HttpTestHandler handler) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   350
            return new Http2Chain(filters, handler);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   351
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   352
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   353
        private static class Http1Chain extends HttpChain {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   354
            final Filter.Chain chain;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   355
            Http1Chain(Filter.Chain chain) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   356
                this.chain = chain;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   357
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   358
            @Override
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   359
            public void doFilter(HttpTestExchange exchange) throws IOException {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   360
                try {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   361
                    exchange.doFilter(chain);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   362
                } catch (Throwable t) {
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   363
                    System.out.println("WARNING: exception caught in Http1Chain::doFilter " + t);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   364
                    System.err.println("WARNING: exception caught in Http1Chain::doFilter " + t);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   365
                    if (PRINTSTACK && !expectException(exchange)) t.printStackTrace();
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   366
                    throw t;
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   367
                }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   368
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   369
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   370
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   371
        private static class Http2Chain extends HttpChain {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   372
            ListIterator<HttpTestFilter> iter;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   373
            HttpTestHandler handler;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   374
            Http2Chain(List<HttpTestFilter> filters, HttpTestHandler handler) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   375
                this.iter = filters.listIterator();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   376
                this.handler = handler;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   377
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   378
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   379
            public void doFilter(HttpTestExchange exchange) throws IOException {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   380
                try {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   381
                    if (iter.hasNext()) {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   382
                        iter.next().doFilter(exchange, this);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   383
                    } else {
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   384
                        handler.handle(exchange);
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   385
                    }
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   386
                } catch (Throwable t) {
56209
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   387
                    System.out.println("WARNING: exception caught in Http2Chain::doFilter " + t);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   388
                    System.err.println("WARNING: exception caught in Http2Chain::doFilter " + t);
43d5ad612710 http-client-branch: more ThrowingPublishers tests and fixes
dfuchs
parents: 56165
diff changeset
   389
                    if (PRINTSTACK && !expectException(exchange)) t.printStackTrace();
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   390
                    throw t;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   391
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   392
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   393
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   394
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   395
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   396
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   397
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   398
     * A version agnostic adapter class for HTTP Server Filters.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   399
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   400
    public abstract class HttpTestFilter {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   401
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   402
        public abstract String description();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   403
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   404
        public abstract void doFilter(HttpTestExchange exchange, HttpChain chain) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   405
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   406
        public Filter toFilter() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   407
            return new Filter() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   408
                @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   409
                public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   410
                    HttpTestFilter.this.doFilter(HttpTestExchange.of(exchange), HttpChain.of(chain));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   411
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   412
                @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   413
                public String description() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   414
                    return HttpTestFilter.this.description();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   415
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   416
            };
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   417
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   418
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   419
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   420
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   421
     * A version agnostic adapter class for HTTP Server Context.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   422
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   423
    public static abstract class HttpTestContext {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   424
        public abstract String getPath();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   425
        public abstract void addFilter(HttpTestFilter filter);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   426
        public abstract Version getVersion();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   427
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   428
        // will throw UOE if the server is HTTP/2
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   429
        public abstract void setAuthenticator(com.sun.net.httpserver.Authenticator authenticator);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   430
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   431
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   432
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   433
     * A version agnostic adapter class for HTTP Servers.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   434
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   435
    public static abstract class HttpTestServer {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   436
        public abstract void start();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   437
        public abstract void stop();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   438
        public abstract HttpTestContext addHandler(HttpTestHandler handler, String root);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   439
        public abstract InetSocketAddress getAddress();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   440
        public abstract Version getVersion();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   441
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   442
        public static HttpTestServer of(HttpServer server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   443
            return new Http1TestServer(server);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   444
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   445
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   446
        public static HttpTestServer of(Http2TestServer server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   447
            return new Http2TestServerImpl(server);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   448
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   449
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   450
        private static class Http1TestServer extends  HttpTestServer {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   451
            private final HttpServer impl;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   452
            Http1TestServer(HttpServer server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   453
                this.impl = server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   454
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   455
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   456
            public void start() { impl.start(); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   457
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   458
            public void stop() { impl.stop(0); }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   459
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   460
            public HttpTestContext addHandler(HttpTestHandler handler, String path) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   461
                System.out.println("Http1TestServer[" + getAddress()
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   462
                        + "]::addHandler " + handler + ", " + path);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   463
                return new Http1TestContext(impl.createContext(path, handler.toHttpHandler()));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   464
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   465
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   466
            public InetSocketAddress getAddress() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   467
                return new InetSocketAddress("127.0.0.1",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   468
                        impl.getAddress().getPort());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   469
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   470
            public Version getVersion() { return Version.HTTP_1_1; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   471
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   472
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   473
        private static class Http1TestContext extends HttpTestContext {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   474
            private final HttpContext context;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   475
            Http1TestContext(HttpContext ctxt) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   476
                this.context = ctxt;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   477
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   478
            @Override public String getPath() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   479
                return context.getPath();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   480
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   481
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   482
            public void addFilter(HttpTestFilter filter) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   483
                context.getFilters().add(filter.toFilter());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   484
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   485
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   486
            public void setAuthenticator(com.sun.net.httpserver.Authenticator authenticator) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   487
                context.setAuthenticator(authenticator);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   488
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   489
            @Override public Version getVersion() { return Version.HTTP_1_1; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   490
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   491
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   492
        private static class Http2TestServerImpl extends  HttpTestServer {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   493
            private final Http2TestServer impl;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   494
            Http2TestServerImpl(Http2TestServer server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   495
                this.impl = server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   496
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   497
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   498
            public void start() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   499
                System.out.println("Http2TestServerImpl: start");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   500
                impl.start();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   501
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   502
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   503
            public void stop() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   504
                System.out.println("Http2TestServerImpl: stop");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   505
                impl.stop();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   506
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   507
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   508
            public HttpTestContext addHandler(HttpTestHandler handler, String path) {
56165
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   509
                System.out.println("Http2TestServerImpl[" + getAddress()
8a6065d830b9 http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents: 56135
diff changeset
   510
                                   + "]::addHandler " + handler + ", " + path);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   511
                Http2TestContext context = new Http2TestContext(handler, path);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   512
                impl.addHandler(context.toHttp2Handler(), path);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   513
                return context;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   514
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   515
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   516
            public InetSocketAddress getAddress() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   517
                return new InetSocketAddress("127.0.0.1",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   518
                        impl.getAddress().getPort());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   519
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   520
            public Version getVersion() { return Version.HTTP_2; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   521
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   522
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   523
        private static class Http2TestContext
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   524
                extends HttpTestContext implements HttpTestHandler {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   525
            private final HttpTestHandler handler;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   526
            private final String path;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   527
            private final List<HttpTestFilter> filters = new CopyOnWriteArrayList<>();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   528
            Http2TestContext(HttpTestHandler hdl, String path) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   529
                this.handler = hdl;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   530
                this.path = path;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   531
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   532
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   533
            public String getPath() { return path; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   534
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   535
            public void addFilter(HttpTestFilter filter) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   536
                System.out.println("Http2TestContext::addFilter " + filter.description());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   537
                filters.add(filter);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   538
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   539
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   540
            public void handle(HttpTestExchange exchange) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   541
                System.out.println("Http2TestContext::handle " + exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   542
                HttpChain.of(filters, handler).doFilter(exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   543
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   544
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   545
            public void setAuthenticator(com.sun.net.httpserver.Authenticator authenticator) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   546
                throw new UnsupportedOperationException("Can't set HTTP/1.1 authenticator on HTTP/2 context");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   547
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   548
            @Override public Version getVersion() { return Version.HTTP_2; }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   549
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   550
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   551
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   552
}