test/jdk/java/net/httpclient/ImmutableFlowItems.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 52121 934969c63223
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @summary Tests response body subscribers's onNext's Lists are unmodifiable,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 *          and that the buffers are read-only
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    28
 * @library /test/lib http2/server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    29
 * @build jdk.test.lib.net.SimpleSSLContext
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 * @modules java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 * @run testng/othervm ImmutableFlowItems
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.io.InputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.io.OutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import java.util.concurrent.Flow;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import com.sun.net.httpserver.HttpExchange;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import com.sun.net.httpserver.HttpHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import com.sun.net.httpserver.HttpServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import com.sun.net.httpserver.HttpsServer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
import java.net.http.HttpHeaders;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
import java.net.http.HttpResponse.BodyHandler;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
import java.net.http.HttpResponse.BodySubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
import java.net.http.HttpResponse.BodySubscribers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
import javax.net.ssl.SSLContext;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    60
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
import org.testng.annotations.BeforeTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
import static org.testng.Assert.*;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
public class ImmutableFlowItems {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    SSLContext sslContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
    HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    HttpsServer httpsTestServer;       // HTTPS/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
    String httpURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
    String httpURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
    String httpsURI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
    String httpsURI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
    String http2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    String http2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
    String https2URI_fixed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    String https2URI_chunk;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    @DataProvider(name = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
    public Object[][] variants() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
        return new Object[][]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
                { httpURI_fixed   },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
                { httpURI_chunk   },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
                { httpsURI_fixed  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
                { httpsURI_chunk  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
                { http2URI_fixed  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
                { http2URI_chunk  },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
                { https2URI_fixed },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                { https2URI_chunk },
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
    static final String BODY = "You'll never plough a field by turning it over in your mind.";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    HttpClient newHttpClient() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
        return HttpClient.newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
                .sslContext(sslContext)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
    @Test(dataProvider = "variants")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    public void testAsString(String uri) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
        HttpClient client = newHttpClient();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
        HttpRequest req = HttpRequest.newBuilder(URI.create(uri))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
                .build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
        BodyHandler<String> handler = new CRSBodyHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        client.sendAsync(req, handler)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
                .thenApply(HttpResponse::body)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
                .thenAccept(body -> assertEquals(body, BODY))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
    static class CRSBodyHandler implements BodyHandler<String> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
        public BodySubscriber<String> apply(HttpResponse.ResponseInfo rinfo) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
            assertEquals(rinfo.statusCode(), 200);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            return new CRSBodySubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
    static class CRSBodySubscriber implements BodySubscriber<String> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        private final BodySubscriber<String> ofString = BodySubscribers.ofString(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        public void onSubscribe(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
            ofString.onSubscribe(subscription);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
        public void onNext(List<ByteBuffer> item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
            assertUnmodifiableList(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
            long c = item.stream().filter(ByteBuffer::isReadOnly).count();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
            assertEquals(c, item.size(), "Unexpected writable buffer in: " +item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
            ofString.onNext(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
        public void onError(Throwable throwable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
            ofString.onError(throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
        public void onComplete() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
            ofString.onComplete();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
        public CompletionStage<String> getBody() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
            return ofString.getBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
    static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
    static void assertUnmodifiableList(List<ByteBuffer> list) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
        assertNotNull(list);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        ByteBuffer b = list.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        assertNotNull(b);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        assertThrows(UOE, () -> list.add(ByteBuffer.wrap(new byte[0])));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        assertThrows(UOE, () -> list.remove(b));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
    static String serverAuthority(HttpServer server) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
        return InetAddress.getLoopbackAddress().getHostName() + ":"
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
                + server.getAddress().getPort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
    @BeforeTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
    public void setup() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
        sslContext = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
        if (sslContext == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
            throw new AssertionError("Unexpected null sslContext");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        // HTTP/1.1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
        HttpHandler h1_fixedLengthHandler = new HTTP1_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        HttpHandler h1_chunkHandler = new HTTP1_ChunkedHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
        httpTestServer = HttpServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
        httpTestServer.createContext("/http1/fixed", h1_fixedLengthHandler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
        httpTestServer.createContext("/http1/chunk", h1_chunkHandler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
        httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
        httpsTestServer = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        httpsTestServer.createContext("/https1/fixed", h1_fixedLengthHandler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        httpsTestServer.createContext("/https1/chunk", h1_chunkHandler);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        // HTTP/2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        Http2Handler h2_fixedLengthHandler = new HTTP2_FixedLengthHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
        Http2Handler h2_chunkedHandler = new HTTP2_VariableHandler();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
        http2TestServer = new Http2TestServer("localhost", false, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        http2TestServer.addHandler(h2_fixedLengthHandler, "/http2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        http2TestServer.addHandler(h2_chunkedHandler, "/http2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
        http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
        http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   209
        https2TestServer = new Http2TestServer("localhost", true, sslContext);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
        https2TestServer.addHandler(h2_fixedLengthHandler, "/https2/fixed");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
        https2TestServer.addHandler(h2_chunkedHandler, "/https2/chunk");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
        https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/fixed";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/chunk";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
        httpTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
        httpsTestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
        http2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
        https2TestServer.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
    public void teardown() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        httpTestServer.stop(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
        httpsTestServer.stop(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
        http2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
        https2TestServer.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
    static class HTTP1_FixedLengthHandler implements HttpHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
        public void handle(HttpExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
            out.println("HTTP1_FixedLengthHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
            try (InputStream is = t.getRequestBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
                 OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
                byte[] bytes = BODY.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
                t.sendResponseHeaders(200, bytes.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
                os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
    static class HTTP1_ChunkedHandler implements HttpHandler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        public void handle(HttpExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
            out.println("HTTP1_ChunkedHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
            try (InputStream is = t.getRequestBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
                 OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
                byte[] bytes = BODY.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
                t.sendResponseHeaders(200, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
                os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
    static class HTTP2_FixedLengthHandler implements Http2Handler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        public void handle(Http2TestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
            out.println("HTTP2_FixedLengthHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
            try (InputStream is = t.getRequestBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
                 OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
                byte[] bytes = BODY.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
                t.sendResponseHeaders(200, bytes.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
    static class HTTP2_VariableHandler implements Http2Handler {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
        public void handle(Http2TestExchange t) throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
            out.println("HTTP2_VariableHandler received request to " + t.getRequestURI());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
            try (InputStream is = t.getRequestBody();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
                 OutputStream os = t.getResponseBody()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                is.readAllBytes();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                byte[] bytes = BODY.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                t.sendResponseHeaders(200, 0); // variable
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                os.write(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
}