test/jdk/java/net/httpclient/websocket/WebSocketTextTest.java
author prappo
Wed, 14 Mar 2018 13:03:11 +0000
branchhttp-client-branch
changeset 56303 a82058c084ef
parent 56263 4933a477d628
child 56304 065641767a75
permissions -rw-r--r--
http-client-branch: (WebSocket) swapping automatic pong replies
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     1
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     4
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     8
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    13
 * accompanied this code).
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    14
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    18
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    21
 * questions.
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    22
 */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    23
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    24
import org.testng.annotations.DataProvider;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    25
import org.testng.annotations.Test;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    26
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    27
import java.io.IOException;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    28
import java.net.http.WebSocket;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    29
import java.nio.ByteBuffer;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    30
import java.nio.charset.StandardCharsets;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    31
import java.util.ArrayList;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    32
import java.util.List;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    33
import java.util.Random;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    34
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    35
import static java.net.http.HttpClient.newHttpClient;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    36
import static org.testng.Assert.assertEquals;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    37
import static org.testng.Assert.assertTrue;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    38
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    39
/*
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    40
 * @test
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    41
 * @bug 8159053
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    42
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    43
 *
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    44
 * @run testng/othervm
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    45
 *      -Djdk.httpclient.websocket.writeBufferSize=1024
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    46
 *      -Djdk.httpclient.websocket.intermediateBufferSize=2048 WebSocketTextTest
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    47
 */
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    48
public class WebSocketTextTest {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    49
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    50
    private final static Random random;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    51
    static {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    52
        long seed = System.currentTimeMillis();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    53
        System.out.println("seed=" + seed);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    54
        random = new Random(seed);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    55
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    56
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    57
// * @run testng/othervm
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    58
// *      -Djdk.httpclient.websocket.writeBufferSize=16
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    59
// *      -Djdk.httpclient.sendBufferSize=32 WebSocketTextTest
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    60
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    61
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    62
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    63
    // FIXME ensure subsequent (sendText/Binary, false) only CONTINUATIONs
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    64
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    65
    @Test(dataProvider = "binary")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    66
    public void binary(ByteBuffer expected) throws IOException, InterruptedException {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    67
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    68
            server.open();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    69
            WebSocket ws = newHttpClient()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    70
                    .newWebSocketBuilder()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    71
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    72
                    .join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    73
            ws.sendBinary(expected.duplicate(), true).join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    74
            ws.abort();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    75
            ByteBuffer data = server.read();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    76
            List<Frame> frames = readFrames(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    77
            assertEquals(frames.size(), 1);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    78
            Frame f = frames.get(0);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    79
            assertTrue(f.last);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    80
            assertEquals(f.opcode, Frame.Opcode.BINARY);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    81
            assertEquals(f.data, expected);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    82
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    83
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    84
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    85
    private static List<Frame> readFrames(ByteBuffer src) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    86
        List<Frame> frames = new ArrayList<>();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    87
        Frame.Consumer consumer = new Frame.Consumer() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    88
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    89
            ByteBuffer data;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    90
            Frame.Opcode opcode;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    91
            Frame.Masker masker = new Frame.Masker();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    92
            boolean last;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    93
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    94
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    95
            public void fin(boolean value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    96
                last = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    97
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    98
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
    99
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   100
            public void rsv1(boolean value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   101
                if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   102
                    throw new AssertionError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   103
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   104
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   105
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   106
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   107
            public void rsv2(boolean value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   108
                if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   109
                    throw new AssertionError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   110
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   111
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   112
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   113
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   114
            public void rsv3(boolean value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   115
                if (value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   116
                    throw new AssertionError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   117
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   118
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   119
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   120
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   121
            public void opcode(Frame.Opcode value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   122
                opcode = value;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   123
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   124
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   125
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   126
            public void mask(boolean value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   127
                if (!value) { // Frames from the client MUST be masked
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   128
                    throw new AssertionError();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   129
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   130
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   131
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   132
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   133
            public void payloadLen(long value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   134
                data = ByteBuffer.allocate((int) value);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   135
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   136
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   137
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   138
            public void maskingKey(int value) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   139
                masker.mask(value);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   140
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   141
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   142
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   143
            public void payloadData(ByteBuffer data) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   144
                masker.transferMasking(data, this.data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   145
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   146
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   147
            @Override
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   148
            public void endFrame() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   149
                frames.add(new Frame(opcode, this.data.flip(), last));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   150
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   151
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   152
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   153
        Frame.Reader r = new Frame.Reader();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   154
        while (src.hasRemaining()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   155
            r.readFrame(src, consumer);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   156
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   157
        return frames;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   158
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   159
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   160
    @Test(dataProvider = "pingPong")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   161
    public void ping(ByteBuffer expected) throws Exception {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   162
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   163
            server.open();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   164
            WebSocket ws = newHttpClient()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   165
                    .newWebSocketBuilder()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   166
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   167
                    .join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   168
            ws.sendPing(expected.duplicate()).join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   169
            ws.abort();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   170
            ByteBuffer data = server.read();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   171
            List<Frame> frames = readFrames(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   172
            assertEquals(frames.size(), 1);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   173
            Frame f = frames.get(0);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   174
            assertEquals(f.opcode, Frame.Opcode.PING);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   175
            ByteBuffer actual = ByteBuffer.allocate(expected.remaining());
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   176
            actual.put(f.data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   177
            actual.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   178
            assertEquals(actual, expected);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   179
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   180
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   181
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   182
    @Test(dataProvider = "pingPong")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   183
    public void pong(ByteBuffer expected) throws Exception {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   184
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   185
            server.open();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   186
            WebSocket ws = newHttpClient()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   187
                    .newWebSocketBuilder()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   188
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   189
                    .join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   190
            ws.sendPong(expected.duplicate()).join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   191
            ws.abort();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   192
            ByteBuffer data = server.read();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   193
            List<Frame> frames = readFrames(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   194
            assertEquals(frames.size(), 1);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   195
            Frame f = frames.get(0);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   196
            assertEquals(f.opcode, Frame.Opcode.PONG);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   197
            ByteBuffer actual = ByteBuffer.allocate(expected.remaining());
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   198
            actual.put(f.data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   199
            actual.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   200
            assertEquals(actual, expected);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   201
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   202
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   203
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   204
    @Test(dataProvider = "close")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   205
    public void close(int statusCode, String reason) throws Exception {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   206
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   207
            server.open();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   208
            WebSocket ws = newHttpClient()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   209
                    .newWebSocketBuilder()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   210
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   211
                    .join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   212
            ws.sendClose(statusCode, reason).join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   213
            ws.abort();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   214
            ByteBuffer data = server.read();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   215
            List<Frame> frames = readFrames(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   216
            assertEquals(frames.size(), 1);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   217
            Frame f = frames.get(0);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   218
            assertEquals(f.opcode, Frame.Opcode.CLOSE);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   219
            ByteBuffer actual = ByteBuffer.allocate(Frame.MAX_CONTROL_FRAME_PAYLOAD_SIZE);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   220
            actual.put(f.data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   221
            actual.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   222
            assertEquals(actual.getChar(), statusCode);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   223
            assertEquals(StandardCharsets.UTF_8.decode(actual).toString(), reason);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   224
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   225
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   226
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   227
    @Test(dataProvider = "text")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   228
    public void text(String expected) throws Exception {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   229
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   230
            server.open();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   231
            WebSocket ws = newHttpClient()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   232
                    .newWebSocketBuilder()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   233
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   234
                    .join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   235
            ws.sendText(expected, true).join();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   236
            ws.abort();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   237
            ByteBuffer data = server.read();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   238
            List<Frame> frames = readFrames(data);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   239
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   240
            int maxBytes = (int) StandardCharsets.UTF_8.newEncoder().maxBytesPerChar() * expected.length();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   241
            ByteBuffer actual = ByteBuffer.allocate(maxBytes);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   242
            frames.stream().forEachOrdered(f -> actual.put(f.data));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   243
            actual.flip();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   244
            assertEquals(StandardCharsets.UTF_8.decode(actual).toString(), expected);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   245
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   246
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   247
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   248
    @DataProvider(name = "pingPong")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   249
    public Object[][] pingPongSizes() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   250
        return new Object[][]{
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   251
                {bytes(  0)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   252
                {bytes(  1)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   253
                {bytes( 63)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   254
                {bytes(125)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   255
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   256
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   257
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   258
    @DataProvider(name = "close")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   259
    public Object[][] closeArguments() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   260
        return new Object[][]{
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   261
                {WebSocket.NORMAL_CLOSURE, utf8String( 0)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   262
                {WebSocket.NORMAL_CLOSURE, utf8String( 1)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   263
                // 123 / 3 = max reason bytes / max bytes per char
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   264
                {WebSocket.NORMAL_CLOSURE, utf8String(41)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   265
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   266
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   267
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   268
    private static String utf8String(int n) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   269
        char[] abc = {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   270
                // -- English Alphabet (26 characters, 1 byte per char) --
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   271
                0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   272
                0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   273
                0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   274
                0x0059, 0x005A,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   275
                // -- Russian Alphabet (33 characters, 2 bytes per char) --
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   276
                0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   277
                0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   278
                0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   279
                0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   280
                0x042F,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   281
                // -- Hiragana base characters (46 characters, 3 bytes per char) --
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   282
                0x3042, 0x3044, 0x3046, 0x3048, 0x304A, 0x304B, 0x304D, 0x304F,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   283
                0x3051, 0x3053, 0x3055, 0x3057, 0x3059, 0x305B, 0x305D, 0x305F,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   284
                0x3061, 0x3064, 0x3066, 0x3068, 0x306A, 0x306B, 0x306C, 0x306D,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   285
                0x306E, 0x306F, 0x3072, 0x3075, 0x3078, 0x307B, 0x307E, 0x307F,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   286
                0x3080, 0x3081, 0x3082, 0x3084, 0x3086, 0x3088, 0x3089, 0x308A,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   287
                0x308B, 0x308C, 0x308D, 0x308F, 0x3092, 0x3093,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   288
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   289
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   290
        assert new String(abc).getBytes(StandardCharsets.UTF_8).length > abc.length;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   291
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   292
        StringBuilder str = new StringBuilder(n);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   293
        random.ints(0, abc.length).limit(n).forEach(i -> str.append(abc[i]));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   294
        return str.toString();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   295
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   296
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   297
    @DataProvider(name = "text")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   298
    public Object[][] texts() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   299
        return new Object[][]{
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   300
                {utf8String(   0)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   301
                {utf8String(1024)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   302
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   303
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   304
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   305
    @DataProvider(name = "binary")
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   306
    public Object[][] binary() {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   307
        return new Object[][]{
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   308
                {bytes(   0)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   309
                {bytes(1024)},
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   310
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   311
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   312
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   313
    private static ByteBuffer bytes(int n) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   314
        byte[] array = new byte[n];
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   315
        random.nextBytes(array);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   316
        return ByteBuffer.wrap(array);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   317
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents:
diff changeset
   318
}