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