test/jdk/java/net/httpclient/HttpServerAdapters.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56128 249a863b0aca
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
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
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    71
    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
    72
        try {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    73
            baos.write(ba);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    74
        } catch (IOException e) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    75
            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
    76
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    77
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    78
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    79
    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
    80
        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
    81
        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
    82
        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
    83
        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
    84
        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
    85
                    + 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
    86
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    87
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    88
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    89
     * 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
    90
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    91
    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
    92
        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
    93
        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
    94
        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
    95
        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
    96
        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
    97
        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
    98
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
    99
        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
   100
            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
   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(HttpHeadersImpl 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 Http2TestHeaders(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
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   106
        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
   107
            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
   108
            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
   109
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   110
            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
   111
                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
   112
                    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
   113
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   114
                return Optional.empty();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   115
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   116
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   117
            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
   118
                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
   119
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   120
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   121
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   122
            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
   123
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   124
            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
   125
                return headers.entrySet();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   126
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   127
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   128
            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
   129
                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
   130
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   131
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   132
            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
   133
                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
   134
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   135
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   136
        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
   137
            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
   138
            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
   139
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   140
            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
   141
                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
   142
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   143
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   144
            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
   145
                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
   146
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   147
            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
   148
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   149
            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
   150
                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
   151
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   152
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   153
            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
   154
                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
   155
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   156
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   157
            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
   158
                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
   159
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   160
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   161
    }
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
     * 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
   165
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   166
    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
   167
        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
   168
        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
   169
        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
   170
        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
   171
        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
   172
        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
   173
        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
   174
        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
   175
        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
   176
        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
   177
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   178
        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
   179
            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
   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(Http2TestExchange 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 Http2TestExchangeImpl(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
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   185
        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
   186
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   187
        // implementations...
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   188
        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
   189
            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
   190
            Http1TestExchange(HttpExchange exch) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   191
                this.exchange = exch;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   192
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   193
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   194
            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
   195
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   196
            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
   197
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   198
            public InputStream getRequestBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   199
                return exchange.getRequestBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   200
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   201
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   202
            public OutputStream getResponseBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   203
                return exchange.getResponseBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   204
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   205
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   206
            public HttpTestHeaders getRequestHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   207
                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
   208
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   209
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   210
            public HttpTestHeaders getResponseHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   211
                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
   212
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   213
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   214
            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
   215
                if (contentLength == 0) contentLength = -1;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   216
                if (contentLength < 0) contentLength = 0;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   217
                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
   218
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   219
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   220
            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
   221
                chain.doFilter(exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   222
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   223
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   224
            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
   225
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   226
            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
   227
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   228
            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
   229
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   230
            public String toString() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   231
                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
   232
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   233
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   234
        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
   235
            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
   236
            Http2TestExchangeImpl(Http2TestExchange exch) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   237
                this.exchange = exch;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   238
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   239
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   240
            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
   241
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   242
            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
   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 InputStream getRequestBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   245
                return exchange.getRequestBody();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   246
            }
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 OutputStream getResponseBody() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   249
                return exchange.getResponseBody();
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 HttpTestHeaders getRequestHeaders() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   253
                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
   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 getResponseHeaders() {
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.getResponseHeaders());
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 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
   261
                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
   262
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   263
            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
   264
                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
   265
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   266
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   267
            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
   268
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   269
            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
   270
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   271
            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
   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 String toString() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   274
                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
   275
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   276
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   277
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   278
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   279
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   280
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
     * 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
   283
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   284
    public interface HttpTestHandler {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   285
        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
   286
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   287
        default HttpHandler toHttpHandler() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   288
            return (t) -> handle(HttpTestExchange.of(t));
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
        default Http2Handler toHttp2Handler() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   291
            return (t) -> handle(HttpTestExchange.of(t));
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
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   294
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   295
    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
   296
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   297
        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
   298
            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
   299
                 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
   300
                byte[] bytes = is.readAllBytes();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   301
                printBytes(System.out,"Bytes: ", bytes);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   302
                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
   303
                    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
   304
                            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
   305
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   306
                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
   307
                os.write(bytes);
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
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   311
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   312
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   313
     * 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
   314
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   315
    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
   316
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   317
        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
   318
        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
   319
            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
   320
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   321
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   322
        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
   323
            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
   324
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   325
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   326
        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
   327
            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
   328
            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
   329
                this.chain = chain;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   330
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   331
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   332
            public 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
   333
                exchange.doFilter(chain);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   334
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   335
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   336
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   337
        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
   338
            ListIterator<HttpTestFilter> iter;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   339
            HttpTestHandler handler;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   340
            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
   341
                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
   342
                this.handler = handler;
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
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   345
            public 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
   346
                if (iter.hasNext()) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   347
                    iter.next().doFilter(exchange, this);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   348
                } else {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   349
                    handler.handle(exchange);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   350
                }
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
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   354
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   355
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   356
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   357
     * 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
   358
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   359
    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
   360
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   361
        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
   362
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   363
        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
   364
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   365
        public Filter toFilter() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   366
            return new Filter() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   367
                @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   368
                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
   369
                    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
   370
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   371
                @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   372
                public String description() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   373
                    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
   374
                }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   375
            };
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   376
        }
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
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   379
    /**
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   380
     * 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
   381
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   382
    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
   383
        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
   384
        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
   385
        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
   386
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   387
        // 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
   388
        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
   389
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   390
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
     * 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
   393
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   394
    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
   395
        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
   396
        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
   397
        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
   398
        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
   399
        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
   400
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   401
        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
   402
            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
   403
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   404
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   405
        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
   406
            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
   407
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   408
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   409
        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
   410
            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
   411
            Http1TestServer(HttpServer server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   412
                this.impl = server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   413
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   414
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   415
            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
   416
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   417
            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
   418
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   419
            public HttpTestContext addHandler(HttpTestHandler handler, String path) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   420
                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
   421
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   422
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   423
            public InetSocketAddress getAddress() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   424
                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
   425
                        impl.getAddress().getPort());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   426
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   427
            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
   428
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   429
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   430
        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
   431
            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
   432
            Http1TestContext(HttpContext ctxt) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   433
                this.context = ctxt;
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
            @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
   436
                return context.getPath();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   437
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   438
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   439
            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
   440
                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
   441
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   442
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   443
            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
   444
                context.setAuthenticator(authenticator);
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
            @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
   447
        }
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
        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
   450
            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
   451
            Http2TestServerImpl(Http2TestServer server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   452
                this.impl = server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   453
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   454
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   455
            public void start() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   456
                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
   457
                impl.start();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   458
            }
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 void stop() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   461
                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
   462
                impl.stop();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   463
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   464
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   465
            public HttpTestContext addHandler(HttpTestHandler handler, String path) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   466
                System.out.println("Http2TestServerImpl::addHandler " + handler + ", " + path);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   467
                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
   468
                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
   469
                return context;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   470
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   471
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   472
            public InetSocketAddress getAddress() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   473
                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
   474
                        impl.getAddress().getPort());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   475
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   476
            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
   477
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   478
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   479
        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
   480
                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
   481
            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
   482
            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
   483
            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
   484
            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
   485
                this.handler = hdl;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   486
                this.path = path;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   487
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   488
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   489
            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
   490
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   491
            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
   492
                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
   493
                filters.add(filter);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   494
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   495
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   496
            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
   497
                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
   498
                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
   499
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   500
            @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   501
            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
   502
                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
   503
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   504
            @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
   505
        }
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
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents:
diff changeset
   508
}