test/jdk/java/net/httpclient/websocket/WSHandshakeExceptionTest.java
author prappo
Fri, 08 Dec 2017 12:35:49 +0300
branchhttp-client-branch
changeset 55978 65bd0b92abec
child 56089 42208b2f224e
permissions -rw-r--r--
http-client-branch: (WebSocket) tests cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55978
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     1
/*
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
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
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    29
 * @modules jdk.incubator.httpclient
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    30
 *          jdk.httpserver
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    31
 * @run testng/othervm WSHandshakeExceptionTest
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;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    36
import jdk.incubator.http.HttpClient;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    37
import jdk.incubator.http.WebSocket;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    38
import jdk.incubator.http.WebSocketHandshakeException;
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;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    48
import java.util.concurrent.ExecutorService;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    49
import java.util.concurrent.Executors;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    50
import static org.testng.Assert.assertEquals;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    51
import static org.testng.Assert.assertNotNull;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    52
import static org.testng.Assert.fail;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    53
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    54
public class WSHandshakeExceptionTest {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    55
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    56
    SSLContext sslContext;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    57
    HttpServer httpTestServer;         // HTTP/1.1    [ 2 servers ]
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    58
    HttpsServer httpsTestServer;       // HTTPS/1.1
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    59
    String httpURI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    60
    String httpsURI;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    61
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    62
    static final int ITERATION_COUNT = 10;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    63
    // a shared executor helps reduce the amount of threads created by the test
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    64
    static final ExecutorService executor = Executors.newCachedThreadPool();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    65
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    66
    @DataProvider(name = "variants")
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    67
    public Object[][] variants() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    68
        return new Object[][]{
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    69
                { httpURI,    false },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    70
                { httpsURI,   false },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    71
                { httpURI,    true },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    72
                { httpsURI,   true },
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    73
        };
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
    HttpClient newHttpClient() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    77
        return HttpClient.newBuilder()
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    78
                         .executor(executor)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    79
                         .sslContext(sslContext)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    80
                         .build();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    81
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    82
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    83
    @Test(dataProvider = "variants")
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    84
    public void test(String uri, boolean sameClient) {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    85
        HttpClient client = null;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    86
        for (int i = 0; i < ITERATION_COUNT; i++) {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    87
            if (!sameClient || client == null)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    88
                client = newHttpClient();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    89
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    90
            try {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    91
                client.newWebSocketBuilder()
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    92
                      .buildAsync(URI.create(uri), new WebSocket.Listener() { })
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    93
                      .join();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    94
                fail("Expected to throw");
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    95
            } catch (CompletionException ce) {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    96
                Throwable t = ce.getCause();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    97
                assertEquals(t.getClass(), WebSocketHandshakeException.class);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    98
                WebSocketHandshakeException wse = (WebSocketHandshakeException) t;
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
    99
                assertNotNull(wse.getResponse());
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   100
                assertEquals(wse.getResponse().statusCode(), 404);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   101
            }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   102
        }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   103
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   104
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   105
    @BeforeTest
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   106
    public void setup() throws Exception {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   107
        sslContext = new SimpleSSLContext().get();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   108
        if (sslContext == null)
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   109
            throw new AssertionError("Unexpected null sslContext");
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   110
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   111
        // HTTP/1.1
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   112
        InetSocketAddress sa = new InetSocketAddress("localhost", 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   113
        httpTestServer = HttpServer.create(sa, 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   114
        httpURI = "ws://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/";
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   115
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   116
        httpsTestServer = HttpsServer.create(sa, 0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   117
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   118
        httpsURI = "wss://127.0.0.1:" + httpsTestServer.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
        httpTestServer.start();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   121
        httpsTestServer.start();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   122
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   123
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   124
    @AfterTest
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   125
    public void teardown() {
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   126
        httpTestServer.stop(0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   127
        httpsTestServer.stop(0);
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   128
        executor.shutdownNow();
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   129
    }
65bd0b92abec http-client-branch: (WebSocket) tests cleanup
prappo
parents:
diff changeset
   130
}