jdk/test/java/net/httpclient/BasicWebSocketAPITest.java
author prappo
Mon, 09 May 2016 23:33:09 +0100
changeset 37874 02589df0999a
child 38864 bf2b41533aed
permissions -rw-r--r--
8087113: Websocket API and implementation Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     4
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
import org.testng.annotations.Test;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
import java.io.IOException;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.net.InetSocketAddress;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.net.URI;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.net.http.HttpClient;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.net.http.WebSocket;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.net.http.WebSocket.CloseCode;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
import java.nio.ByteBuffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import java.nio.channels.SocketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.util.concurrent.CompletableFuture;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.util.concurrent.TimeUnit;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.util.function.Consumer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.stream.Stream;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
 * @test
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
 * @bug 8087113
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
 * @build TestKit
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
 * @run testng/othervm BasicWebSocketAPITest
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
public class BasicWebSocketAPITest {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
    @Test
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
    public void webSocket() throws Exception {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
                        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
                                "(?i).*\\bnegative\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
                                () -> ws.request(-1))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
                TestKit.assertNotThrows(() -> ws.request(0))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
                TestKit.assertNotThrows(() -> ws.request(1))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
                TestKit.assertNotThrows(() -> ws.request(Long.MAX_VALUE))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
                TestKit.assertNotThrows(ws::isClosed)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
                TestKit.assertNotThrows(ws::getSubprotocol)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
                (ws) -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
                    try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
                        ws.abort();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
                    } catch (IOException ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
                    // No matter what happens during the first abort invocation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
                    // other invocations must return normally
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
                    TestKit.assertNotThrows(ws::abort);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
                    TestKit.assertNotThrows(ws::abort);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
                                () -> ws.sendBinary((byte[]) null, true))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
                                () -> ws.sendBinary((ByteBuffer) null, true))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
                                () -> ws.sendPing(null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
                                () -> ws.sendPong(null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
                                () -> ws.sendText((CharSequence) null, true))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
                                () -> ws.sendText((CharSequence) null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
                                () -> ws.sendText((Stream<? extends CharSequence>) null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
                                "code",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
                                () -> ws.sendClose(null, ""))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
                        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
                                () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, ""))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
                                "reason",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
                                () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
                                "code|reason",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
                                () -> ws.sendClose(null, null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
    @Test
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
    public void builder() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
        URI ws = URI.create("ws://localhost:9001");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
        // FIXME: check all 24 cases:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
        // {null, ws, wss, incorrect} x {null, HttpClient.getDefault(), custom} x {null, listener}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
        //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
        // if (any null) or (any incorrect)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
        //     NPE or IAE is thrown
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
        // else
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
        //     builder is created
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
                "uri",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
                () -> WebSocket.newBuilder(null, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
                "listener",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
                () -> WebSocket.newBuilder(ws, null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
        URI uri = URI.create("ftp://localhost:9001");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   167
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   168
                "(?i).*\\buri\\b\\s+\\bscheme\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
                () -> WebSocket.newBuilder(uri, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
                () -> WebSocket.newBuilder(ws, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
        URI uri1 = URI.create("wss://localhost:9001");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
                () -> WebSocket.newBuilder(uri1, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
        URI uri2 = URI.create("wss://localhost:9001#a");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
                "(?i).*\\bfragment\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
                () -> WebSocket.newBuilder(uri2, HttpClient.getDefault(), defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
                "uri",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
                () -> WebSocket.newBuilder(null, HttpClient.getDefault(), defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
                "client",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
                () -> WebSocket.newBuilder(ws, null, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
                "listener",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
                () -> WebSocket.newBuilder(ws, HttpClient.getDefault(), null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
        // FIXME: check timeout works
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
        // (i.e. it directly influences the time WebSocket waits for connection + opening handshake)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
                () -> WebSocket.newBuilder(ws, defaultListener()).connectTimeout(1, TimeUnit.SECONDS)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
        WebSocket.Builder builder = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
                "(?i).*\\bnegative\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
                () -> builder.connectTimeout(-1, TimeUnit.SECONDS)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
        WebSocket.Builder builder1 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   206
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   207
                "unit",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
                () -> builder1.connectTimeout(1, null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
        // FIXME: check these headers are actually received by the server
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
                () -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
                () -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b").header("a", "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
        // FIXME: check all 18 cases:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
        // {null, websocket(7), custom} x {null, custom}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
        WebSocket.Builder builder2 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
                "name",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
                () -> builder2.header(null, "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
        WebSocket.Builder builder3 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   225
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   226
                "value",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
                () -> builder3.header("a", null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
        WebSocket.Builder builder4 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
                () -> builder4.header("Sec-WebSocket-Accept", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
        WebSocket.Builder builder5 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
                () -> builder5.header("Sec-WebSocket-Extensions", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
        WebSocket.Builder builder6 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
                () -> builder6.header("Sec-WebSocket-Key", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
        WebSocket.Builder builder7 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
                () -> builder7.header("Sec-WebSocket-Protocol", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
        WebSocket.Builder builder8 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
                () -> builder8.header("Sec-WebSocket-Version", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
        WebSocket.Builder builder9 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
                () -> builder9.header("Connection", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
        WebSocket.Builder builder10 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
                () -> builder10.header("Upgrade", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
        // FIXME: check 3 cases (1 arg):
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
        // {null, incorrect, custom}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
        // FIXME: check 12 cases (2 args):
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
        // {null, incorrect, custom} x {(String) null, (String[]) null, incorrect, custom}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
        // FIXME: check 27 cases (3 args) (the interesting part in null inside var-arg):
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
        // {null, incorrect, custom}^3
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
        // FIXME: check the server receives them in the order listed
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
                "mostPreferred",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols(null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
                "lesserPreferred",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
                "lesserPreferred\\[0\\]",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   274
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null, "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   275
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   277
                "lesserPreferred\\[1\\]",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   278
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   279
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   280
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   281
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   282
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   283
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   284
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", "c")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   285
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   286
        WebSocket.Builder builder11 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   287
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   288
                () -> builder11.subprotocols("")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   289
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   290
        WebSocket.Builder builder12 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   291
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   292
                () -> builder12.subprotocols("a", "a")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   293
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   294
        WebSocket.Builder builder13 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   295
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   296
                () -> builder13.subprotocols("a" + ((char) 0x7f))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   297
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   298
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   299
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   300
    private static WebSocket.Listener defaultListener() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   301
        return new WebSocket.Listener() { };
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   302
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   303
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   304
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   305
    // Automatically closes everything after the check has been performed
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   306
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   307
    private static void checkAndClose(Consumer<? super WebSocket> c) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   308
        HandshakePhase HandshakePhase
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   309
                = new HandshakePhase(new InetSocketAddress("127.0.0.1", 0));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   310
        URI serverURI = HandshakePhase.getURI();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   311
        CompletableFuture<SocketChannel> cfc = HandshakePhase.afterHandshake();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   312
        WebSocket.Builder b = WebSocket.newBuilder(serverURI, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   313
        CompletableFuture<WebSocket> cfw = b.buildAsync();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   314
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   315
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   316
            WebSocket ws;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   317
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   318
                ws = cfw.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   319
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   320
                throw new RuntimeException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   321
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   322
            c.accept(ws);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   323
        } finally {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   324
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   325
                SocketChannel now = cfc.getNow(null);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   326
                if (now != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   327
                    now.close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   328
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   329
            } catch (Throwable ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   330
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   331
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   332
}