test/jdk/java/net/httpclient/websocket/Abort.java
author chegar
Thu, 26 Apr 2018 14:42:36 +0100
branchhttp-client-branch
changeset 56486 b664a887e277
parent 49765 ee6f7a61f3a5
child 58289 3a79d4cccbcb
permissions -rw-r--r--
http-client-branch: set NO_PROXY in several WebSocket tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
 * @build DummyWebSocketServer
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 * @run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *      -Djdk.internal.httpclient.websocket.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 *       Abort
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.net.ProtocolException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.net.http.WebSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.util.concurrent.TimeUnit;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.util.concurrent.TimeoutException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import static java.net.http.HttpClient.newHttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import static java.net.http.WebSocket.NORMAL_CLOSURE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
import static org.testng.Assert.assertThrows;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
import static org.testng.Assert.fail;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
public class Abort {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
    private static final Class<NullPointerException> NPE = NullPointerException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
    private static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
    private static final Class<IOException> IOE = IOException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    private DummyWebSocketServer server;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
    private WebSocket webSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
    public void cleanup() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
        server.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
        webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
    public void onOpenThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
        // messages are available
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        // no more invocations after onOpen as WebSocket was aborted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
        assertEquals(inv, List.of(MockListener.Invocation.onOpen(webSocket)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
    public void onOpenThenOnTextThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
                0x81, 0x00, // opcode=text, fin=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
            protected CompletionStage<?> onText0(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
                                                 CharSequence message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
                                                 boolean last) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                return super.onText0(webSocket, message, last);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
        // no more invocations after onOpen, onBinary as WebSocket was aborted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
                MockListener.Invocation.onText(webSocket, "", true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
        assertEquals(inv, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
    public void onOpenThenOnBinaryThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
                0x82, 0x00, // opcode=binary, fin=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
            protected CompletionStage<?> onBinary0(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
                                                   ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
                                                   boolean last) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
                return super.onBinary0(webSocket, message, last);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
        // no more invocations after onOpen, onBinary as WebSocket was aborted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
                MockListener.Invocation.onBinary(webSocket, ByteBuffer.allocate(0), true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
        assertEquals(inv, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
    public void onOpenThenOnPingThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
                0x89, 0x00, // opcode=ping
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
            protected CompletionStage<?> onPing0(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
                                                 ByteBuffer message) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
                return super.onPing0(webSocket, message);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
        // no more invocations after onOpen, onPing as WebSocket was aborted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
                MockListener.Invocation.onPing(webSocket, ByteBuffer.allocate(0)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        assertEquals(inv, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
    public void onOpenThenOnPongThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
                0x8a, 0x00, // opcode=pong
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
            protected CompletionStage<?> onPong0(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
                                                 ByteBuffer message) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
                return super.onPong0(webSocket, message);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
        // no more invocations after onOpen, onPong as WebSocket was aborted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
                MockListener.Invocation.onPong(webSocket, ByteBuffer.allocate(0)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
        assertEquals(inv, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
    public void onOpenThenOnCloseThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                0x8a, 0x00, // opcode=pong
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
            protected CompletionStage<?> onClose0(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
                                                  int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
                                                  String reason) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
                return super.onClose0(webSocket, statusCode, reason);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        // no more invocations after onOpen, onClose
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
                MockListener.Invocation.onClose(webSocket, 1005, ""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
        assertEquals(inv, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
    public void onOpenThenOnErrorThenAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
        // A header of 128 bytes long Ping (which is a protocol error)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        int[] badPingHeader = new int[]{0x89, 0x7e, 0x00, 0x80};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
        int[] closeMessage = new int[]{0x88, 0x00};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
        int[] bytes = new int[badPingHeader.length + 128 + closeMessage.length];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        System.arraycopy(badPingHeader, 0, bytes, 0, badPingHeader.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
        System.arraycopy(closeMessage, 0, bytes, badPingHeader.length + 128, closeMessage.length);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                // unbounded request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
            protected void onError0(WebSocket webSocket, Throwable error) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
                webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
                super.onError0(webSocket, error);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        webSocket = newHttpClient().newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
        TimeUnit.SECONDS.sleep(5);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
        List<MockListener.Invocation> inv = listener.invocationsSoFar();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        // no more invocations after onOpen, onError
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
                MockListener.Invocation.onError(webSocket, ProtocolException.class));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        System.out.println("actual invocations:" + Arrays.toString(inv.toArray()));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
        assertEquals(inv, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
    public void immediateAbort() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
        CompletableFuture<Void> messageReceived = new CompletableFuture<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
        WebSocket.Listener listener = new WebSocket.Listener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
            public void onOpen(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
                /* no initial request */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
            public CompletionStage<?> onText(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                                             CharSequence message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
                                             boolean last) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
            public CompletionStage<?> onBinary(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
                                               ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
                                               boolean last) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
            public CompletionStage<?> onPing(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
                                             ByteBuffer message) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
            public CompletionStage<?> onPong(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                                             ByteBuffer message) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
            public CompletionStage<?> onClose(WebSocket webSocket,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
                                              int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
                                              String reason) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
                messageReceived.complete(null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
                return null;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
        int[] bytes = new int[]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
                0x82, 0x00, // opcode=binary, fin=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
                0x88, 0x00, // opcode=close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
        WebSocket ws = newHttpClient()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
                .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
        for (int i = 0; i < 3; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
            System.out.printf("iteration #%s%n", i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
            // after the first abort() each consecutive one must be a no-op,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
            // moreover, query methods should continue to return consistent
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
            // values
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
            for (int j = 0; j < 3; j++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
                System.out.printf("abort #%s%n", j);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
                ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
                assertTrue(ws.isInputClosed());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
                assertTrue(ws.isOutputClosed());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
                assertEquals(ws.getSubprotocol(), "");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
            // at this point valid requests MUST be a no-op:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
            for (int j = 0; j < 3; j++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
                System.out.printf("request #%s%n", j);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
                ws.request(1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
                ws.request(2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
                ws.request(8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   380
                ws.request(Integer.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   381
                ws.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   382
                // invalid requests MUST throw IAE:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   383
                assertThrows(IAE, () -> ws.request(Integer.MIN_VALUE));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   384
                assertThrows(IAE, () -> ws.request(Long.MIN_VALUE));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   385
                assertThrows(IAE, () -> ws.request(-1));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   386
                assertThrows(IAE, () -> ws.request(0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   387
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   388
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   389
        // even though there is a bunch of messages readily available on the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   390
        // wire we shouldn't have received any of them as we aborted before
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   391
        // the first request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   392
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   393
            messageReceived.get(5, TimeUnit.SECONDS);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   394
            fail();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   395
        } catch (TimeoutException expected) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   396
            System.out.println("Finished waiting");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   397
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   398
        for (int i = 0; i < 3; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   399
            System.out.printf("send #%s%n", i);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   400
            Support.assertFails(IOE, ws.sendText("text!", false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   401
            Support.assertFails(IOE, ws.sendText("text!", true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   402
            Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   403
            Support.assertFails(IOE, ws.sendBinary(ByteBuffer.allocate(16), true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   404
            Support.assertFails(IOE, ws.sendPing(ByteBuffer.allocate(16)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   405
            Support.assertFails(IOE, ws.sendPong(ByteBuffer.allocate(16)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   406
            Support.assertFails(IOE, ws.sendClose(NORMAL_CLOSURE, "a reason"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   407
            assertThrows(NPE, () -> ws.sendText(null, false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   408
            assertThrows(NPE, () -> ws.sendText(null, true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   409
            assertThrows(NPE, () -> ws.sendBinary(null, false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   410
            assertThrows(NPE, () -> ws.sendBinary(null, true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   411
            assertThrows(NPE, () -> ws.sendPing(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   412
            assertThrows(NPE, () -> ws.sendPong(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   413
            assertThrows(NPE, () -> ws.sendClose(NORMAL_CLOSURE, null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   414
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   415
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   416
}