49765
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @build DummyWebSocketServer
|
|
27 |
* @run testng/othervm
|
|
28 |
* -Djdk.internal.httpclient.websocket.debug=true
|
|
29 |
* SendTest
|
|
30 |
*/
|
|
31 |
|
|
32 |
import org.testng.annotations.AfterTest;
|
|
33 |
import org.testng.annotations.Test;
|
|
34 |
|
|
35 |
import java.io.IOException;
|
|
36 |
import java.net.http.WebSocket;
|
|
37 |
|
|
38 |
import static java.net.http.HttpClient.newHttpClient;
|
|
39 |
import static java.net.http.WebSocket.NORMAL_CLOSURE;
|
|
40 |
import static org.testng.Assert.assertEquals;
|
|
41 |
import static org.testng.Assert.assertThrows;
|
|
42 |
import static org.testng.Assert.assertTrue;
|
|
43 |
|
|
44 |
public class SendTest {
|
|
45 |
|
|
46 |
private static final Class<NullPointerException> NPE = NullPointerException.class;
|
|
47 |
|
|
48 |
private DummyWebSocketServer server;
|
|
49 |
private WebSocket webSocket;
|
|
50 |
|
|
51 |
@AfterTest
|
|
52 |
public void cleanup() {
|
|
53 |
server.close();
|
|
54 |
webSocket.abort();
|
|
55 |
}
|
|
56 |
|
|
57 |
@Test
|
|
58 |
public void sendMethodsThrowNPE() throws IOException {
|
|
59 |
server = new DummyWebSocketServer();
|
|
60 |
server.open();
|
|
61 |
webSocket = newHttpClient().newWebSocketBuilder()
|
|
62 |
.buildAsync(server.getURI(), new WebSocket.Listener() { })
|
|
63 |
.join();
|
|
64 |
|
|
65 |
assertThrows(NPE, () -> webSocket.sendText(null, false));
|
|
66 |
assertThrows(NPE, () -> webSocket.sendText(null, true));
|
|
67 |
assertThrows(NPE, () -> webSocket.sendBinary(null, false));
|
|
68 |
assertThrows(NPE, () -> webSocket.sendBinary(null, true));
|
|
69 |
assertThrows(NPE, () -> webSocket.sendPing(null));
|
|
70 |
assertThrows(NPE, () -> webSocket.sendPong(null));
|
|
71 |
assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
|
|
72 |
|
|
73 |
webSocket.abort();
|
|
74 |
|
|
75 |
assertThrows(NPE, () -> webSocket.sendText(null, false));
|
|
76 |
assertThrows(NPE, () -> webSocket.sendText(null, true));
|
|
77 |
assertThrows(NPE, () -> webSocket.sendBinary(null, false));
|
|
78 |
assertThrows(NPE, () -> webSocket.sendBinary(null, true));
|
|
79 |
assertThrows(NPE, () -> webSocket.sendPing(null));
|
|
80 |
assertThrows(NPE, () -> webSocket.sendPong(null));
|
|
81 |
assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null));
|
|
82 |
}
|
|
83 |
|
|
84 |
// TODO: request in onClose/onError
|
|
85 |
// TODO: throw exception in onClose/onError
|
|
86 |
// TODO: exception is thrown from request()
|
|
87 |
|
|
88 |
@Test
|
|
89 |
public void sendCloseCompleted() throws IOException {
|
|
90 |
server = new DummyWebSocketServer();
|
|
91 |
server.open();
|
|
92 |
webSocket = newHttpClient().newWebSocketBuilder()
|
|
93 |
.buildAsync(server.getURI(), new WebSocket.Listener() { })
|
|
94 |
.join();
|
|
95 |
webSocket.sendClose(NORMAL_CLOSURE, "").join();
|
|
96 |
assertTrue(webSocket.isOutputClosed());
|
|
97 |
assertEquals(webSocket.getSubprotocol(), "");
|
|
98 |
webSocket.request(1); // No exceptions must be thrown
|
|
99 |
}
|
|
100 |
}
|