test/jdk/java/net/httpclient/websocket/WebSocketExtendedTest.java
author chegar
Thu, 26 Apr 2018 14:42:36 +0100
branchhttp-client-branch
changeset 56486 b664a887e277
parent 56451 9585061fdb04
child 56507 2294c51eae30
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
 * @bug 8159053
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
 * @run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 *      -Djdk.internal.httpclient.websocket.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
 *      -Djdk.internal.httpclient.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
 *      -Djdk.httpclient.websocket.writeBufferSize=1024
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
 *      -Djdk.httpclient.websocket.intermediateBufferSize=2048 WebSocketExtendedTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.net.http.WebSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.nio.charset.StandardCharsets;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.util.ArrayList;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import java.util.Random;
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
    46
import static java.net.http.HttpClient.Builder.NO_PROXY;
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
    47
import static java.net.http.HttpClient.newBuilder;
49765
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.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
 * This battery of tests exercises sending data (Text/Binary) messages with
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
 * possible fragmentation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
public class WebSocketExtendedTest {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
// * run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
// *      -Djdk.httpclient.websocket.writeBufferSize=16
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
// *      -Djdk.httpclient.sendBufferSize=32 WebSocketTextTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
    private final static Random random;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
    static {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
        long seed = System.currentTimeMillis();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
        System.out.println("seed=" + seed);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
        random = new Random(seed);
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
    // FIXME ensure subsequent (sendText/Binary, false) only CONTINUATIONs
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
    @Test(dataProvider = "binary")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    public void binary(ByteBuffer expected) throws IOException, InterruptedException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
            server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
    74
            WebSocket ws = newBuilder().proxy(NO_PROXY).build()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
                    .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
                    .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
            ws.sendBinary(expected.duplicate(), true).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
            ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
            ByteBuffer data = server.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
            List<Frame> frames = readFrames(data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
            assertEquals(frames.size(), 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
            Frame f = frames.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
            assertTrue(f.last);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
            assertEquals(f.opcode, Frame.Opcode.BINARY);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
            assertEquals(f.data, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    private static List<Frame> readFrames(ByteBuffer src) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        List<Frame> frames = new ArrayList<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
        Frame.Consumer consumer = new Frame.Consumer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
            ByteBuffer data;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
            Frame.Opcode opcode;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
            Frame.Masker masker = new Frame.Masker();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
            boolean last;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
            public void fin(boolean value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
                last = value;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
            public void rsv1(boolean value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
                if (value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
                    throw new AssertionError();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
            public void rsv2(boolean value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                if (value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
                    throw new AssertionError();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
            public void rsv3(boolean value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
                if (value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
                    throw new AssertionError();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
            public void opcode(Frame.Opcode value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
                opcode = value;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
            public void mask(boolean value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
                if (!value) { // Frames from the client MUST be masked
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
                    throw new AssertionError();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
            public void payloadLen(long value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                data = ByteBuffer.allocate((int) value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            public void maskingKey(int value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                masker.mask(value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
            public void payloadData(ByteBuffer data) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
                masker.transferMasking(data, this.data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
            public void endFrame() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
                frames.add(new Frame(opcode, this.data.flip(), last));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
        Frame.Reader r = new Frame.Reader();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        while (src.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
            r.readFrame(src, consumer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
        return frames;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
    @Test(dataProvider = "pingPong")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
    public void ping(ByteBuffer expected) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
            server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
   169
            WebSocket ws = newBuilder().proxy(NO_PROXY).build()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
                    .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                    .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
            ws.sendPing(expected.duplicate()).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
            ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
            ByteBuffer data = server.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
            List<Frame> frames = readFrames(data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
            assertEquals(frames.size(), 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
            Frame f = frames.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
            assertEquals(f.opcode, Frame.Opcode.PING);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
            ByteBuffer actual = ByteBuffer.allocate(expected.remaining());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
            actual.put(f.data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
            actual.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            assertEquals(actual, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
    @Test(dataProvider = "pingPong")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
    public void pong(ByteBuffer expected) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
            server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
   191
            WebSocket ws = newBuilder().proxy(NO_PROXY).build()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
                    .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
                    .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
            ws.sendPong(expected.duplicate()).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
            ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
            ByteBuffer data = server.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
            List<Frame> frames = readFrames(data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
            assertEquals(frames.size(), 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
            Frame f = frames.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
            assertEquals(f.opcode, Frame.Opcode.PONG);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
            ByteBuffer actual = ByteBuffer.allocate(expected.remaining());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
            actual.put(f.data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
            actual.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
            assertEquals(actual, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
    @Test(dataProvider = "close")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
    public void close(int statusCode, String reason) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
            server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
   213
            WebSocket ws = newBuilder().proxy(NO_PROXY).build()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
                    .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
                    .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
            ws.sendClose(statusCode, reason).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
            ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
            ByteBuffer data = server.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
            List<Frame> frames = readFrames(data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
            assertEquals(frames.size(), 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
            Frame f = frames.get(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
            assertEquals(f.opcode, Frame.Opcode.CLOSE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
            ByteBuffer actual = ByteBuffer.allocate(Frame.MAX_CONTROL_FRAME_PAYLOAD_SIZE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
            actual.put(f.data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
            actual.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
            assertEquals(actual.getChar(), statusCode);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
            assertEquals(StandardCharsets.UTF_8.decode(actual).toString(), reason);
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
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
    @Test(dataProvider = "text")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
    public void text(String expected) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
        try (DummyWebSocketServer server = new DummyWebSocketServer()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
            server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
   236
            WebSocket ws = newBuilder().proxy(NO_PROXY).build()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
                    .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
                    .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
                    .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
            ws.sendText(expected, true).join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
            ws.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
            ByteBuffer data = server.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
            List<Frame> frames = readFrames(data);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
            int maxBytes = (int) StandardCharsets.UTF_8.newEncoder().maxBytesPerChar() * expected.length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
            ByteBuffer actual = ByteBuffer.allocate(maxBytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
            frames.stream().forEachOrdered(f -> actual.put(f.data));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
            actual.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
            assertEquals(StandardCharsets.UTF_8.decode(actual).toString(), expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
    @DataProvider(name = "pingPong")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
    public Object[][] pingPongSizes() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
        return new Object[][]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
                {bytes(  0)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
                {bytes(  1)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
                {bytes( 63)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
                {bytes(125)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
    @DataProvider(name = "close")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
    public Object[][] closeArguments() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
        return new Object[][]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                {WebSocket.NORMAL_CLOSURE, utf8String( 0)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
                {WebSocket.NORMAL_CLOSURE, utf8String( 1)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
                // 123 / 3 = max reason bytes / max bytes per char
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
                {WebSocket.NORMAL_CLOSURE, utf8String(41)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
    private static String utf8String(int n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
        char[] abc = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
                // -- English Alphabet (26 characters, 1 byte per char) --
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
                0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                0x0059, 0x005A,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                // -- Russian Alphabet (33 characters, 2 bytes per char) --
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
                0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0401, 0x0416,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
                0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                0x041F, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
                0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
                0x042F,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
                // -- Hiragana base characters (46 characters, 3 bytes per char) --
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
                0x3042, 0x3044, 0x3046, 0x3048, 0x304A, 0x304B, 0x304D, 0x304F,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
                0x3051, 0x3053, 0x3055, 0x3057, 0x3059, 0x305B, 0x305D, 0x305F,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
                0x3061, 0x3064, 0x3066, 0x3068, 0x306A, 0x306B, 0x306C, 0x306D,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                0x306E, 0x306F, 0x3072, 0x3075, 0x3078, 0x307B, 0x307E, 0x307F,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                0x3080, 0x3081, 0x3082, 0x3084, 0x3086, 0x3088, 0x3089, 0x308A,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
                0x308B, 0x308C, 0x308D, 0x308F, 0x3092, 0x3093,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        assert new String(abc).getBytes(StandardCharsets.UTF_8).length > abc.length;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        StringBuilder str = new StringBuilder(n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        random.ints(0, abc.length).limit(n).forEach(i -> str.append(abc[i]));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
        return str.toString();
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
    @DataProvider(name = "text")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
    public Object[][] texts() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
        return new Object[][]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
                {utf8String(   0)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
                {utf8String(1024)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
    @DataProvider(name = "binary")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
    public Object[][] binary() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
        return new Object[][]{
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
                {bytes(   0)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
                {bytes(1024)},
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
    private static ByteBuffer bytes(int n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
        byte[] array = new byte[n];
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
        random.nextBytes(array);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
        return ByteBuffer.wrap(array);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
}