test/jdk/java/net/httpclient/websocket/WSHandshakeExceptionTest.java
changeset 49765 ee6f7a61f3a5
child 49944 4690a2871b44
child 56451 9585061fdb04
equal deleted inserted replaced
49707:f7fd051519ac 49765:ee6f7a61f3a5
       
     1 /*
       
     2  * Copyright (c) 2017, 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 Basic test for WebSocketHandshakeException
       
    27  * @library /lib/testlibrary
       
    28  * @build jdk.testlibrary.SimpleSSLContext
       
    29  * @modules java.net.http
       
    30  *          jdk.httpserver
       
    31  * @run testng/othervm -Djdk.internal.httpclient.debug=true WSHandshakeExceptionTest
       
    32  */
       
    33 
       
    34 import com.sun.net.httpserver.HttpServer;
       
    35 import com.sun.net.httpserver.HttpsConfigurator;
       
    36 import com.sun.net.httpserver.HttpsServer;
       
    37 
       
    38 import java.net.InetAddress;
       
    39 import java.net.http.HttpClient;
       
    40 import java.net.http.WebSocket;
       
    41 import java.net.http.WebSocketHandshakeException;
       
    42 import jdk.testlibrary.SimpleSSLContext;
       
    43 import org.testng.annotations.AfterTest;
       
    44 import org.testng.annotations.BeforeTest;
       
    45 import org.testng.annotations.DataProvider;
       
    46 import org.testng.annotations.Test;
       
    47 import javax.net.ssl.SSLContext;
       
    48 import java.net.InetSocketAddress;
       
    49 import java.net.URI;
       
    50 import java.util.concurrent.CompletionException;
       
    51 import java.util.concurrent.ExecutionException;
       
    52 import java.util.concurrent.ExecutorService;
       
    53 import java.util.concurrent.Executors;
       
    54 import static org.testng.Assert.assertEquals;
       
    55 import static org.testng.Assert.assertNotNull;
       
    56 import static org.testng.Assert.fail;
       
    57 
       
    58 public class WSHandshakeExceptionTest {
       
    59 
       
    60     SSLContext sslContext;
       
    61     HttpServer httpTestServer;         // HTTP/1.1    [ 2 servers ]
       
    62     HttpsServer httpsTestServer;       // HTTPS/1.1
       
    63     String httpURI;
       
    64     String httpsURI;
       
    65 
       
    66     static final int ITERATION_COUNT = 10;
       
    67     // a shared executor helps reduce the amount of threads created by the test
       
    68     static final ExecutorService executor = Executors.newCachedThreadPool();
       
    69 
       
    70     @DataProvider(name = "variants")
       
    71     public Object[][] variants() {
       
    72         return new Object[][]{
       
    73                 { httpURI,    false },
       
    74                 { httpsURI,   false },
       
    75                 { httpURI,    true },
       
    76                 { httpsURI,   true },
       
    77         };
       
    78     }
       
    79 
       
    80     HttpClient newHttpClient() {
       
    81         return HttpClient.newBuilder()
       
    82                          .executor(executor)
       
    83                          .sslContext(sslContext)
       
    84                          .build();
       
    85     }
       
    86 
       
    87     @Test(dataProvider = "variants")
       
    88     public void test(String uri, boolean sameClient) {
       
    89         HttpClient client = null;
       
    90         for (int i = 0; i < ITERATION_COUNT; i++) {
       
    91             System.out.printf("iteration %s%n", i);
       
    92             if (!sameClient || client == null)
       
    93                 client = newHttpClient();
       
    94 
       
    95             try {
       
    96                 client.newWebSocketBuilder()
       
    97                       .buildAsync(URI.create(uri), new WebSocket.Listener() { })
       
    98                       .join();
       
    99                 fail("Expected to throw");
       
   100             } catch (CompletionException ce) {
       
   101                 Throwable t = getCompletionCause(ce);
       
   102                 if (!(t instanceof WebSocketHandshakeException)) {
       
   103                     throw new AssertionError("Unexpected exception", t);
       
   104                 }
       
   105                 WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
       
   106                 assertNotNull(wse.getResponse());
       
   107                 assertEquals(wse.getResponse().statusCode(), 404);
       
   108             }
       
   109         }
       
   110     }
       
   111 
       
   112     @BeforeTest
       
   113     public void setup() throws Exception {
       
   114         sslContext = new SimpleSSLContext().get();
       
   115         if (sslContext == null)
       
   116             throw new AssertionError("Unexpected null sslContext");
       
   117 
       
   118         // HTTP/1.1
       
   119         InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
       
   120         httpTestServer = HttpServer.create(sa, 0);
       
   121         httpURI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/";
       
   122 
       
   123         httpsTestServer = HttpsServer.create(sa, 0);
       
   124         httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
       
   125         httpsURI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/";
       
   126 
       
   127         httpTestServer.start();
       
   128         httpsTestServer.start();
       
   129     }
       
   130 
       
   131     @AfterTest
       
   132     public void teardown() {
       
   133         httpTestServer.stop(0);
       
   134         httpsTestServer.stop(0);
       
   135         executor.shutdownNow();
       
   136     }
       
   137 
       
   138     private static Throwable getCompletionCause(Throwable x) {
       
   139         if (!(x instanceof CompletionException)
       
   140                 && !(x instanceof ExecutionException)) return x;
       
   141         final Throwable cause = x.getCause();
       
   142         if (cause == null) {
       
   143             throw new InternalError("Unexpected null cause", x);
       
   144         }
       
   145         return cause;
       
   146     }
       
   147 }