test/jdk/java/net/httpclient/websocket/WSHandshakeExceptionTest.java
author prappo
Wed, 07 Mar 2018 13:00:11 +0000
branchhttp-client-branch
changeset 56256 0fe17c3f9b4f
parent 56089 42208b2f224e
child 56265 ec34ae013fbe
permissions -rw-r--r--
http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     1
/*
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     4
 *
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     8
 *
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    13
 * accompanied this code).
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    14
 *
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    18
 *
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    21
 * questions.
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    22
 */
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    23
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    24
/*
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    25
 * @test
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    26
 * @summary Basic test for WebSocketHandshakeException
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    27
 * @library /lib/testlibrary
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    28
 * @build jdk.testlibrary.SimpleSSLContext
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    29
 * @modules java.net.http
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    30
 *          jdk.httpserver
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
    31
 * @run testng/othervm -Djdk.internal.httpclient.debug=true WSHandshakeExceptionTest
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    32
 */
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    33
import com.sun.net.httpserver.HttpServer;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    34
import com.sun.net.httpserver.HttpsConfigurator;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    35
import com.sun.net.httpserver.HttpsServer;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    36
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    37
import java.net.http.WebSocket;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    38
import java.net.http.WebSocketHandshakeException;
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    39
import jdk.testlibrary.SimpleSSLContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    40
import org.testng.annotations.AfterTest;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    41
import org.testng.annotations.BeforeTest;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    42
import org.testng.annotations.DataProvider;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    43
import org.testng.annotations.Test;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    44
import javax.net.ssl.SSLContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    45
import java.net.InetSocketAddress;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    46
import java.net.URI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    47
import java.util.concurrent.CompletionException;
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
    48
import java.util.concurrent.ExecutionException;
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    49
import java.util.concurrent.ExecutorService;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    50
import java.util.concurrent.Executors;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    51
import static org.testng.Assert.assertEquals;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    52
import static org.testng.Assert.assertNotNull;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    53
import static org.testng.Assert.fail;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    54
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    55
public class WSHandshakeExceptionTest {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    56
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    57
    SSLContext sslContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    58
    HttpServer httpTestServer;         // HTTP/1.1    [ 2 servers ]
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    59
    HttpsServer httpsTestServer;       // HTTPS/1.1
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    60
    String httpURI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    61
    String httpsURI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    62
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    63
    static final int ITERATION_COUNT = 10;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    64
    // a shared executor helps reduce the amount of threads created by the test
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    65
    static final ExecutorService executor = Executors.newCachedThreadPool();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    66
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    67
    @DataProvider(name = "variants")
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    68
    public Object[][] variants() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    69
        return new Object[][]{
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    70
                { httpURI,    false },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    71
                { httpsURI,   false },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    72
                { httpURI,    true },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    73
                { httpsURI,   true },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    74
        };
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    75
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    76
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    77
    HttpClient newHttpClient() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    78
        return HttpClient.newBuilder()
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    79
                         .executor(executor)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    80
                         .sslContext(sslContext)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    81
                         .build();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    82
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    83
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    84
    @Test(dataProvider = "variants")
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    85
    public void test(String uri, boolean sameClient) {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    86
        HttpClient client = null;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    87
        for (int i = 0; i < ITERATION_COUNT; i++) {
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
    88
            System.out.printf("iteration %s%n", i);
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    89
            if (!sameClient || client == null)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    90
                client = newHttpClient();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    91
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    92
            try {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    93
                client.newWebSocketBuilder()
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    94
                      .buildAsync(URI.create(uri), new WebSocket.Listener() { })
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    95
                      .join();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    96
                fail("Expected to throw");
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    97
            } catch (CompletionException ce) {
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
    98
                Throwable t = getCompletionCause(ce);
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
    99
                if (!(t instanceof WebSocketHandshakeException)) {
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   100
                    throw new AssertionError("Unexpected exception", t);
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   101
                }
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   102
                WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   103
                assertNotNull(wse.getResponse());
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   104
                assertEquals(wse.getResponse().statusCode(), 404);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   105
            }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   106
        }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   107
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   108
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   109
    @BeforeTest
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   110
    public void setup() throws Exception {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   111
        sslContext = new SimpleSSLContext().get();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   112
        if (sslContext == null)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   113
            throw new AssertionError("Unexpected null sslContext");
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   114
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   115
        // HTTP/1.1
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   116
        InetSocketAddress sa = new InetSocketAddress("localhost", 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   117
        httpTestServer = HttpServer.create(sa, 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   118
        httpURI = "ws://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/";
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   119
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   120
        httpsTestServer = HttpsServer.create(sa, 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   121
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   122
        httpsURI = "wss://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/";
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   123
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   124
        httpTestServer.start();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   125
        httpsTestServer.start();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   126
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   127
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   128
    @AfterTest
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   129
    public void teardown() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   130
        httpTestServer.stop(0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   131
        httpsTestServer.stop(0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   132
        executor.shutdownNow();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   133
    }
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   134
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   135
    private static Throwable getCompletionCause(Throwable x) {
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   136
        if (!(x instanceof CompletionException)
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   137
                && !(x instanceof ExecutionException)) return x;
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   138
        final Throwable cause = x.getCause();
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   139
        if (cause == null) {
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   140
            throw new InternalError("Unexpected null cause", x);
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   141
        }
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   142
        return cause;
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   143
    }
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   144
}