jdk/test/java/net/httpclient/BasicWebSocketAPITest.java
author prappo
Wed, 08 Jun 2016 15:19:58 +0100
changeset 38864 bf2b41533aed
parent 37874 02589df0999a
child 39133 b5641ce64cf7
permissions -rw-r--r--
8156693: Improve usability of CompletableFuture use in WebSocket API Reviewed-by: rriggs
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;
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
    33
import java.nio.CharBuffer;
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import java.nio.channels.SocketChannel;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import java.util.concurrent.CompletableFuture;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import java.util.concurrent.TimeUnit;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
import java.util.function.Consumer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
import java.util.stream.Stream;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
 * @test
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
 * @bug 8087113
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
 * @build TestKit
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
 * @run testng/othervm BasicWebSocketAPITest
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
public class BasicWebSocketAPITest {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
    @Test
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
    public void webSocket() throws Exception {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
                        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
                                "(?i).*\\bnegative\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
                                () -> ws.request(-1))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
                TestKit.assertNotThrows(() -> ws.request(0))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
                TestKit.assertNotThrows(() -> ws.request(1))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
                TestKit.assertNotThrows(() -> ws.request(Long.MAX_VALUE))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
                TestKit.assertNotThrows(ws::isClosed)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
        checkAndClose((ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
                TestKit.assertNotThrows(ws::getSubprotocol)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
                (ws) -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
                    try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
                        ws.abort();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
                    } catch (IOException ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
                    // No matter what happens during the first abort invocation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
                    // other invocations must return normally
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
                    TestKit.assertNotThrows(ws::abort);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
                                () -> ws.sendBinary((byte[]) null, true))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
                                () -> ws.sendBinary((ByteBuffer) null, true))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
                (ws) ->
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
    96
                        TestKit.assertThrows(IllegalArgumentException.class,
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
    97
                                ".*message.*",
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
    98
                                () -> ws.sendPing(ByteBuffer.allocate(126)))
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
    99
        );
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   100
        checkAndClose(
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   101
                (ws) ->
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
                                () -> ws.sendPing(null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
                (ws) ->
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   108
                        TestKit.assertThrows(IllegalArgumentException.class,
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   109
                                ".*message.*",
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   110
                                () -> ws.sendPong(ByteBuffer.allocate(126)))
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   111
        );
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   112
        checkAndClose(
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   113
                (ws) ->
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
                                () -> ws.sendPong(null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
                                "message",
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   122
                                () -> ws.sendText(null, true))
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
                                () -> ws.sendText((CharSequence) null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
                                "message",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
                                () -> ws.sendText((Stream<? extends CharSequence>) null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
                (ws) ->
38864
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   138
                        TestKit.assertThrows(IllegalArgumentException.class,
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   139
                                "(?i).*reason.*",
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   140
                                () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, CharBuffer.allocate(124)))
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   141
        );
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   142
        checkAndClose(
bf2b41533aed 8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents: 37874
diff changeset
   143
                (ws) ->
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
                                "code",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
                                () -> ws.sendClose(null, ""))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   149
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   150
                        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   151
                                () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, ""))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   152
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   153
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   154
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   155
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   156
                                "reason",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   157
                                () -> ws.sendClose(CloseCode.NORMAL_CLOSURE, null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   158
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   159
        checkAndClose(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   160
                (ws) ->
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   161
                        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   162
                                "code|reason",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   163
                                () -> ws.sendClose(null, null))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   164
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   165
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   166
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   167
    @Test
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   168
    public void builder() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   169
        URI ws = URI.create("ws://localhost:9001");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   170
        // FIXME: check all 24 cases:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   171
        // {null, ws, wss, incorrect} x {null, HttpClient.getDefault(), custom} x {null, listener}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   172
        //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   173
        // if (any null) or (any incorrect)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   174
        //     NPE or IAE is thrown
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   175
        // else
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   176
        //     builder is created
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   177
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   178
                "uri",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   179
                () -> WebSocket.newBuilder(null, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   180
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   181
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   182
                "listener",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   183
                () -> WebSocket.newBuilder(ws, null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   184
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   185
        URI uri = URI.create("ftp://localhost:9001");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   186
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   187
                "(?i).*\\buri\\b\\s+\\bscheme\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   188
                () -> WebSocket.newBuilder(uri, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   189
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   190
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   191
                () -> WebSocket.newBuilder(ws, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   192
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   193
        URI uri1 = URI.create("wss://localhost:9001");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   194
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   195
                () -> WebSocket.newBuilder(uri1, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   196
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   197
        URI uri2 = URI.create("wss://localhost:9001#a");
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   198
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   199
                "(?i).*\\bfragment\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   200
                () -> WebSocket.newBuilder(uri2, HttpClient.getDefault(), defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   201
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   202
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   203
                "uri",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   204
                () -> WebSocket.newBuilder(null, HttpClient.getDefault(), defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   205
        );
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
                "client",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   208
                () -> WebSocket.newBuilder(ws, null, defaultListener())
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   209
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   210
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   211
                "listener",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   212
                () -> WebSocket.newBuilder(ws, HttpClient.getDefault(), null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   213
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   214
        // FIXME: check timeout works
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   215
        // (i.e. it directly influences the time WebSocket waits for connection + opening handshake)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   216
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   217
                () -> WebSocket.newBuilder(ws, defaultListener()).connectTimeout(1, TimeUnit.SECONDS)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   218
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   219
        WebSocket.Builder builder = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   220
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   221
                "(?i).*\\bnegative\\b.*",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   222
                () -> builder.connectTimeout(-1, TimeUnit.SECONDS)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   223
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   224
        WebSocket.Builder builder1 = 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
                "unit",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   227
                () -> builder1.connectTimeout(1, null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   228
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   229
        // FIXME: check these headers are actually received by the server
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   230
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   231
                () -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   232
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   233
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   234
                () -> WebSocket.newBuilder(ws, defaultListener()).header("a", "b").header("a", "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   235
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   236
        // FIXME: check all 18 cases:
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   237
        // {null, websocket(7), custom} x {null, custom}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   238
        WebSocket.Builder builder2 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   239
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   240
                "name",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   241
                () -> builder2.header(null, "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   242
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   243
        WebSocket.Builder builder3 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   244
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   245
                "value",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   246
                () -> builder3.header("a", null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   247
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   248
        WebSocket.Builder builder4 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   249
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   250
                () -> builder4.header("Sec-WebSocket-Accept", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   251
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   252
        WebSocket.Builder builder5 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   253
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   254
                () -> builder5.header("Sec-WebSocket-Extensions", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   255
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   256
        WebSocket.Builder builder6 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   257
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   258
                () -> builder6.header("Sec-WebSocket-Key", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   259
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   260
        WebSocket.Builder builder7 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   261
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   262
                () -> builder7.header("Sec-WebSocket-Protocol", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   263
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   264
        WebSocket.Builder builder8 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   265
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   266
                () -> builder8.header("Sec-WebSocket-Version", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   267
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   268
        WebSocket.Builder builder9 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   269
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   270
                () -> builder9.header("Connection", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   271
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   272
        WebSocket.Builder builder10 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   273
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   274
                () -> builder10.header("Upgrade", "")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   275
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   276
        // FIXME: check 3 cases (1 arg):
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   277
        // {null, incorrect, custom}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   278
        // FIXME: check 12 cases (2 args):
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   279
        // {null, incorrect, custom} x {(String) null, (String[]) null, incorrect, custom}
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   280
        // FIXME: check 27 cases (3 args) (the interesting part in null inside var-arg):
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   281
        // {null, incorrect, custom}^3
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   282
        // FIXME: check the server receives them in the order listed
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   283
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   284
                "mostPreferred",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   285
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols(null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   286
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   287
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   288
                "lesserPreferred",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   289
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   290
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   291
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   292
                "lesserPreferred\\[0\\]",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   293
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", null, "b")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   294
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   295
        TestKit.assertThrows(NullPointerException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   296
                "lesserPreferred\\[1\\]",
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   297
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", null)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   298
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   299
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   300
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   301
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   302
        TestKit.assertNotThrows(
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   303
                () -> WebSocket.newBuilder(ws, defaultListener()).subprotocols("a", "b", "c")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   304
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   305
        WebSocket.Builder builder11 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   306
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   307
                () -> builder11.subprotocols("")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   308
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   309
        WebSocket.Builder builder12 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   310
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   311
                () -> builder12.subprotocols("a", "a")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   312
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   313
        WebSocket.Builder builder13 = WebSocket.newBuilder(ws, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   314
        TestKit.assertThrows(IllegalArgumentException.class,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   315
                () -> builder13.subprotocols("a" + ((char) 0x7f))
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   316
        );
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   317
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   318
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   319
    private static WebSocket.Listener defaultListener() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   320
        return new WebSocket.Listener() { };
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   321
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   322
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   323
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   324
    // Automatically closes everything after the check has been performed
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   325
    //
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   326
    private static void checkAndClose(Consumer<? super WebSocket> c) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   327
        HandshakePhase HandshakePhase
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   328
                = new HandshakePhase(new InetSocketAddress("127.0.0.1", 0));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   329
        URI serverURI = HandshakePhase.getURI();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   330
        CompletableFuture<SocketChannel> cfc = HandshakePhase.afterHandshake();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   331
        WebSocket.Builder b = WebSocket.newBuilder(serverURI, defaultListener());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   332
        CompletableFuture<WebSocket> cfw = b.buildAsync();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   333
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   334
        try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   335
            WebSocket ws;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   336
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   337
                ws = cfw.get();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   338
            } catch (Exception e) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   339
                throw new RuntimeException(e);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   340
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   341
            c.accept(ws);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   342
        } finally {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   343
            try {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   344
                SocketChannel now = cfc.getNow(null);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   345
                if (now != null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   346
                    now.close();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   347
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   348
            } catch (Throwable ignored) { }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   349
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   350
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   351
}