test/jdk/java/net/httpclient/websocket/WSHandshakeExceptionTest.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56256 0fe17c3f9b4f
child 56324 3edf200fff01
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
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;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56256
diff changeset
    36
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56256
diff changeset
    37
import java.net.InetAddress;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    38
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    39
import java.net.http.WebSocket;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 55978
diff changeset
    40
import java.net.http.WebSocketHandshakeException;
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    41
import jdk.testlibrary.SimpleSSLContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    42
import org.testng.annotations.AfterTest;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    43
import org.testng.annotations.BeforeTest;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    44
import org.testng.annotations.DataProvider;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    45
import org.testng.annotations.Test;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    46
import javax.net.ssl.SSLContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    47
import java.net.InetSocketAddress;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    48
import java.net.URI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    49
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
    50
import java.util.concurrent.ExecutionException;
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    51
import java.util.concurrent.ExecutorService;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    52
import java.util.concurrent.Executors;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    53
import static org.testng.Assert.assertEquals;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    54
import static org.testng.Assert.assertNotNull;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    55
import static org.testng.Assert.fail;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    56
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    57
public class WSHandshakeExceptionTest {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    58
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    59
    SSLContext sslContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    60
    HttpServer httpTestServer;         // HTTP/1.1    [ 2 servers ]
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    61
    HttpsServer httpsTestServer;       // HTTPS/1.1
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    62
    String httpURI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    63
    String httpsURI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    64
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    65
    static final int ITERATION_COUNT = 10;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    66
    // a shared executor helps reduce the amount of threads created by the test
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    67
    static final ExecutorService executor = Executors.newCachedThreadPool();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    68
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    69
    @DataProvider(name = "variants")
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    70
    public Object[][] variants() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    71
        return new Object[][]{
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    72
                { httpURI,    false },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    73
                { httpsURI,   false },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    74
                { httpURI,    true },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    75
                { httpsURI,   true },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    76
        };
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    77
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    78
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    79
    HttpClient newHttpClient() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    80
        return HttpClient.newBuilder()
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    81
                         .executor(executor)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    82
                         .sslContext(sslContext)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    83
                         .build();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    84
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    85
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    86
    @Test(dataProvider = "variants")
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    87
    public void test(String uri, boolean sameClient) {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    88
        HttpClient client = null;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    89
        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
    90
            System.out.printf("iteration %s%n", i);
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    91
            if (!sameClient || client == null)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    92
                client = newHttpClient();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    93
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    94
            try {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    95
                client.newWebSocketBuilder()
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    96
                      .buildAsync(URI.create(uri), new WebSocket.Listener() { })
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    97
                      .join();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    98
                fail("Expected to throw");
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    99
            } catch (CompletionException ce) {
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   100
                Throwable t = getCompletionCause(ce);
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   101
                if (!(t instanceof WebSocketHandshakeException)) {
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   102
                    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
   103
                }
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   104
                WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   105
                assertNotNull(wse.getResponse());
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   106
                assertEquals(wse.getResponse().statusCode(), 404);
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
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   110
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   111
    @BeforeTest
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   112
    public void setup() throws Exception {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   113
        sslContext = new SimpleSSLContext().get();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   114
        if (sslContext == null)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   115
            throw new AssertionError("Unexpected null sslContext");
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   116
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   117
        // HTTP/1.1
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56256
diff changeset
   118
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   119
        httpTestServer = HttpServer.create(sa, 0);
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56256
diff changeset
   120
        httpURI = "ws://localhost:" + httpTestServer.getAddress().getPort() + "/";
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   121
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   122
        httpsTestServer = HttpsServer.create(sa, 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   123
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56256
diff changeset
   124
        httpsURI = "wss://localhost:" + httpsTestServer.getAddress().getPort() + "/";
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   125
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   126
        httpTestServer.start();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   127
        httpsTestServer.start();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   128
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   129
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   130
    @AfterTest
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   131
    public void teardown() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   132
        httpTestServer.stop(0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   133
        httpsTestServer.stop(0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   134
        executor.shutdownNow();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   135
    }
56256
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   136
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   137
    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
   138
        if (!(x instanceof CompletionException)
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   139
                && !(x instanceof ExecutionException)) return x;
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   140
        final Throwable cause = x.getCause();
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   141
        if (cause == null) {
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   142
            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
   143
        }
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   144
        return cause;
0fe17c3f9b4f http-client-branch: (WebSocket) enhance logging for the test for WebSocket Opening Handshake
prappo
parents: 56089
diff changeset
   145
    }
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   146
}