test/jdk/java/net/httpclient/websocket/AutomaticPong.java
author chegar
Thu, 26 Apr 2018 14:42:36 +0100
branchhttp-client-branch
changeset 56486 b664a887e277
parent 56451 9585061fdb04
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
 *       AutomaticPong
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.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.io.IOException;
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.nio.charset.StandardCharsets;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import static java.net.http.HttpClient.newHttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import static org.testng.Assert.assertFalse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import static org.testng.Assert.fail;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
public class AutomaticPong {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
    private DummyWebSocketServer server;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
    private WebSocket webSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
    public void cleanup() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
        server.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
        webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
     * The sendClose method has been invoked and a Ping comes from the server.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
     * Naturally, the client cannot reply with a Pong (the output has been
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
     * closed). However, this MUST not be treated as an error.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
     * At this stage the server either has received or pretty soon will receive
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
     * the Close message sent by the sendClose. Thus, the server will know the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
     * client cannot send further messages and it's up to the server to decide
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
     * how to react on the corresponding Pong not being received.
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 sendCloseThenAutomaticPong() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
        int[] bytes = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
                0x89, 0x00,                                     // ping
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
                0x89, 0x06, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x3f, // ping hello?
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
                0x88, 0x00,                                     // close
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
        server = Support.serverWithCannedData(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
        server.open();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
        MockListener listener = new MockListener() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
            protected void onOpen0(WebSocket webSocket) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
                /* request nothing */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
        webSocket = newHttpClient()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
                .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
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
        webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok").join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        // now request all messages available
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
        webSocket.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        List<MockListener.Invocation> actual = listener.invocations();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
        ByteBuffer hello = ByteBuffer.wrap("hello?".getBytes(StandardCharsets.UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
        ByteBuffer empty = ByteBuffer.allocate(0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        List<MockListener.Invocation> expected = List.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                MockListener.Invocation.onOpen(webSocket),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
                MockListener.Invocation.onPing(webSocket, empty),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
                MockListener.Invocation.onPing(webSocket, hello),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
                MockListener.Invocation.onClose(webSocket, 1005, "")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        );
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
        assertEquals(actual, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
    }
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
     * The server sends a number of contiguous Ping messages. The client replies
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
     * to these messages automatically. According to RFC 6455 a WebSocket client
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
     * is free to reply only to the most recent Pings.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
     * What is checked here is that:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
     *     a) the order of Pong replies corresponds to the Pings received,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
     *     b) the last Pong corresponds to the last Ping
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
     *     c) there are no unrelated Pongs
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
    @Test(dataProvider = "nPings")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
    public void automaticPongs(int nPings) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
        // big enough to not bother with resize
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
        ByteBuffer buffer = ByteBuffer.allocate(65536);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        Frame.HeaderWriter w = new Frame.HeaderWriter();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        for (int i = 0; i < nPings; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
            w.fin(true)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
             .opcode(Frame.Opcode.PING)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
             .noMask()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
             .payloadLen(4)    // the length of the number of the Ping (int)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
             .write(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            buffer.putInt(i);  // the number of the Ping (int)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
        w.fin(true)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
         .opcode(Frame.Opcode.CLOSE)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
         .noMask()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
         .payloadLen(2)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        .write(buffer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        buffer.putChar((char) 1000);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        buffer.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        server = Support.serverWithCannedData(buffer.array());
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
        webSocket = newHttpClient()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                .newWebSocketBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                .buildAsync(server.getURI(), listener)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
        List<MockListener.Invocation> inv = listener.invocations();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
        assertEquals(inv.size(), nPings + 2); // n * onPing + onOpen + onClose
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
        ByteBuffer data = server.read();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
        Frame.Reader reader = new Frame.Reader();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
        Frame.Consumer consumer = new Frame.Consumer() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
            ByteBuffer number = ByteBuffer.allocate(4);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
            Frame.Masker masker = new Frame.Masker();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
            int i = -1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
            boolean closed;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
            public void fin(boolean value) { assertTrue(value); }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
            public void rsv1(boolean value) { assertFalse(value); }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
            public void rsv2(boolean value) { assertFalse(value); }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
            public void rsv3(boolean value) { assertFalse(value); }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
            public void opcode(Frame.Opcode value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
                if (value == Frame.Opcode.CLOSE) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
                    closed = true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
                    return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                assertEquals(value, Frame.Opcode.PONG);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
            public void mask(boolean value) { assertTrue(value); }
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
            public void payloadLen(long value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
                if (!closed)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                    assertEquals(value, 4);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
            public void maskingKey(int value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
                masker.mask(value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
            public void payloadData(ByteBuffer src) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
                masker.transferMasking(src, number);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
                if (closed) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
                    return;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
                number.flip();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
                int n = number.getInt();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
                System.out.printf("pong number=%s%n", n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
                number.clear();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
                // a Pong with the number less than the maximum of Pongs already
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
                // received MUST never be received
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
                if (i >= n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
                    fail(String.format("i=%s, n=%s", i, n));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
                i = n;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
            @Override
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
            public void endFrame() { }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
        while (data.hasRemaining()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
            reader.readFrame(data, consumer);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
    @DataProvider(name = "nPings")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
    public Object[][] nPings() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
        return new Object[][]{{1}, {2}, {4}, {8}, {9}, {256}};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
}