1 /* 2 * Copyright (c) 2018, 2019, 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.Test; 33 34 import java.io.IOException; 35 import java.net.http.WebSocket; 36 37 import static java.net.http.HttpClient.Builder.NO_PROXY; 38 import static java.net.http.HttpClient.newBuilder; 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 @Test sendMethodsThrowNPE()49 public void sendMethodsThrowNPE() throws IOException { 50 try (var server = new DummyWebSocketServer()) { 51 server.open(); 52 var webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() 53 .buildAsync(server.getURI(), new WebSocket.Listener() { }) 54 .join(); 55 try { 56 assertThrows(NPE, () -> webSocket.sendText(null, false)); 57 assertThrows(NPE, () -> webSocket.sendText(null, true)); 58 assertThrows(NPE, () -> webSocket.sendBinary(null, false)); 59 assertThrows(NPE, () -> webSocket.sendBinary(null, true)); 60 assertThrows(NPE, () -> webSocket.sendPing(null)); 61 assertThrows(NPE, () -> webSocket.sendPong(null)); 62 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null)); 63 64 webSocket.abort(); 65 66 assertThrows(NPE, () -> webSocket.sendText(null, false)); 67 assertThrows(NPE, () -> webSocket.sendText(null, true)); 68 assertThrows(NPE, () -> webSocket.sendBinary(null, false)); 69 assertThrows(NPE, () -> webSocket.sendBinary(null, true)); 70 assertThrows(NPE, () -> webSocket.sendPing(null)); 71 assertThrows(NPE, () -> webSocket.sendPong(null)); 72 assertThrows(NPE, () -> webSocket.sendClose(NORMAL_CLOSURE, null)); 73 } finally { 74 webSocket.abort(); 75 } 76 } 77 } 78 79 // TODO: request in onClose/onError 80 // TODO: throw exception in onClose/onError 81 // TODO: exception is thrown from request() 82 83 @Test sendCloseCompleted()84 public void sendCloseCompleted() throws IOException { 85 try (var server = new DummyWebSocketServer()) { 86 server.open(); 87 var webSocket = newBuilder().proxy(NO_PROXY).build().newWebSocketBuilder() 88 .buildAsync(server.getURI(), new WebSocket.Listener() { }) 89 .join(); 90 try { 91 webSocket.sendClose(NORMAL_CLOSURE, "").join(); 92 assertTrue(webSocket.isOutputClosed()); 93 assertEquals(webSocket.getSubprotocol(), ""); 94 webSocket.request(1); // No exceptions must be thrown 95 } finally { 96 webSocket.abort(); 97 } 98 } 99 } 100 } 101 102