test/jdk/java/net/httpclient/websocket/SendTest.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
 * @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
 *       SendTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.net.http.WebSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
    38
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
    39
import static java.net.http.HttpClient.newBuilder;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import static java.net.http.WebSocket.NORMAL_CLOSURE;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import static org.testng.Assert.assertThrows;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import static org.testng.Assert.assertTrue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
public class SendTest {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
    private static final Class<NullPointerException> NPE = NullPointerException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
    private DummyWebSocketServer server;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
    private WebSocket webSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
    public void cleanup() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
        server.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
        webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    public void sendMethodsThrowNPE() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
        server = new DummyWebSocketServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
        server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
    62
        webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
                .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
        assertThrows(NPE, () -> webSocket.sendText(null, false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
        assertThrows(NPE, () -> webSocket.sendText(null, true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
        assertThrows(NPE, () -> webSocket.sendBinary(null, false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
        assertThrows(NPE, () -> webSocket.sendBinary(null, true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
        assertThrows(NPE, () -> webSocket.sendPing(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
        assertThrows(NPE, () -> webSocket.sendPong(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
        assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
        webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
        assertThrows(NPE, () -> webSocket.sendText(null, false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
        assertThrows(NPE, () -> webSocket.sendText(null, true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
        assertThrows(NPE, () -> webSocket.sendBinary(null, false));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
        assertThrows(NPE, () -> webSocket.sendBinary(null, true));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
        assertThrows(NPE, () -> webSocket.sendPing(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
        assertThrows(NPE, () -> webSocket.sendPong(null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
        assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    // TODO: request in onClose/onError
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
    // TODO: throw exception in onClose/onError
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    // TODO: exception is thrown from request()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    @Test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
    public void sendCloseCompleted() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        server = new DummyWebSocketServer();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
        server.open();
56486
b664a887e277 http-client-branch: set NO_PROXY in several WebSocket tests
chegar
parents: 56451
diff changeset
    93
        webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder()
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
                .buildAsync(server.getURI(), new WebSocket.Listener() { })
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
                .join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
        webSocket.sendClose(NORMAL_CLOSURE, "").join();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
        assertTrue(webSocket.isOutputClosed());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
        assertEquals(webSocket.getSubprotocol(), "");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        webSocket.request(1); // No exceptions must be thrown
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
}