@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { // Have a server running on a different port to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(4321)); theServer.websocketHandler(ws -> { fail("Should not connect"); }).listen(ar -> { if (ar.succeeded()) { latch.countDown(); } else { fail("Failed to bind server"); } }); awaitLatch(latch); testSharedServersRoundRobin(); }
@Test public void testSetHandlersAfterListening() throws Exception { server.requestHandler(noOpHandler()); server.listen(onSuccess(s -> { assertIllegalStateException(() -> server.requestHandler(noOpHandler())); assertIllegalStateException(() -> server.websocketHandler(noOpHandler())); testComplete(); })); await(); }
@Test public void testSetHandlersAfterListening2() throws Exception { server.requestHandler(noOpHandler()); server.listen(onSuccess(v -> testComplete())); assertIllegalStateException(() -> server.requestHandler(noOpHandler())); assertIllegalStateException(() -> server.websocketHandler(noOpHandler())); await(); }
private void testServerWebsocketPingPongCheck(int maxFrameSize, Consumer<ServerWebSocket> check) { server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebsocketFrameSize(maxFrameSize)); server.websocketHandler(ws -> { ws.pongHandler(buff -> fail()); check.accept(ws); }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {}); })); await(); }
@Test public void testServerWebsocketIdleTimeout() { server.close(); server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST)); server.websocketHandler(ws -> {}).listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", ws -> { ws.closeHandler(v -> testComplete()); }); }); await(); }
@Test public void testNoRequestHandler() throws Exception { CountDownLatch latch = new CountDownLatch(1); vertx.createHttpServer() .websocketHandler(ws -> fail()) .listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> latch.countDown())); awaitLatch(latch); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { resp.endHandler(v -> { assertEquals(400, resp.statusCode()); testComplete(); }); })); await(); } }
@Test public void testReportProtocolViolationOnServer() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { AtomicReference<Throwable> failure = new AtomicReference<>(); ws.closeHandler(v -> { assertNotNull(failure.get()); testComplete(); }); ws.exceptionHandler(failure::set); }); server.listen(ar -> { assertTrue(ar.succeeded()); handshake(sock -> { // Let's write an invalid frame Buffer buff = Buffer.buffer(); buff.appendByte((byte)(0x8)).appendByte((byte)0); // Violates protocol with V13 (final control frame) sock.write(buff); }); }); await(); }
private SocketMessages testWriteTextMessages(List<String> messagesToSend, WebsocketVersion version) { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { for (String messageToSend : messagesToSend) { ws.writeTextMessage(messageToSend); } ws.close(); }); List<String> receivedMessages = new ArrayList<>(); List<Throwable> receivedExceptions = new ArrayList<>(); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { ws.textMessageHandler(receivedMessages::add); ws.exceptionHandler(receivedExceptions::add); ws.closeHandler(v -> testComplete()); }); }); await(); return new SocketMessages(receivedMessages, receivedExceptions); }
private void testInvalidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String subProtocol = "myprotocol"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols("invalid")).websocketHandler(ws -> { }); server.listen(onSuccess(ar -> { client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol). exceptionHandler(t -> { // Should fail testComplete(); }). handler(ws -> { }); })); await(); }
private void testWriteMessage(int size, WebsocketVersion version) { String path = "/some/path"; byte[] expected = TestUtils.randomByteArray(size); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { ws.writeBinaryMessage(Buffer.buffer(expected)); ws.close(); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { Buffer actual = Buffer.buffer(); ws.handler(actual::appendBuffer); ws.closeHandler(v -> { assertArrayEquals(expected, actual.getBytes(0, actual.length())); testComplete(); }); }); }); await(); }
@Test public void testClientWebsocketIdleTimeout() { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setIdleTimeout(1)); server.websocketHandler(ws -> {}).listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", ws -> { ws.closeHandler(v -> testComplete()); }); }); await(); }
@Test public void testServerWebsocketPingPong() { server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST)); server.websocketHandler(ws -> { ws.pongHandler(buff -> { assertEquals("ping", buff.toString()); testComplete(); }); ws.writePing(Buffer.buffer("ping")); }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {}); })); await(); }
@Test public void testClientWebsocketPingPong() { server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST)); server.websocketHandler(ws -> { }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.pongHandler( pong -> { assertEquals("ping", pong.toString()); testComplete(); }); ws.writePing(Buffer.buffer("ping")); }); })); await(); }
@Test public void testClientWebsocketSendPingExceeds125Bytes() { //Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background String pingBody = randomAlphaString(126); Integer maxFrameSize = 256; server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebsocketFrameSize(maxFrameSize)); server.websocketHandler(ws -> { }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.pongHandler(buffer -> fail()); ws.writeFrame(WebSocketFrame.pingFrame(Buffer.buffer(pingBody))); vertx.setTimer(2000, id -> testComplete()); }); })); await(); }
@Test public void testCloseStatusCodeFromServer() throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); vertx.setTimer(1000, (ar) -> socket.close()); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }); }); awaitLatch(latch); }
@Test public void testServerWebsocketSendPingExceeds125Bytes() { //Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background String pingBody = randomAlphaString(126); Integer maxFrameSize = 256; server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebsocketFrameSize(maxFrameSize)); server.websocketHandler(ws -> { ws.pongHandler(buff -> fail()); ws.writeFrame(WebSocketFrame.pingFrame(Buffer.buffer(pingBody))); vertx.setTimer(2000, id -> testComplete()); }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {}); })); await(); }
@Test public void testClientWebsocketSendPongExceeds125Bytes() { //Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background String pingBody = randomAlphaString(126); Integer maxFrameSize = 256; server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebsocketFrameSize(maxFrameSize)); server.websocketHandler(ws -> { ws.pongHandler(buff -> fail()); vertx.setTimer(2000, id -> testComplete()); }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.writeFrame(WebSocketFrame.pongFrame(Buffer.buffer(pingBody))); }); })); await(); }
@Test public void testServerWebsocketSendPongExceeds125Bytes() { //Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background String pingBody = randomAlphaString(126); Integer maxFrameSize = 256; server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebsocketFrameSize(maxFrameSize)); server.websocketHandler(ws -> { ws.writeFrame(WebSocketFrame.pongFrame(Buffer.buffer(pingBody))); }).listen(onSuccess(v -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.pongHandler(buff -> fail()); vertx.setTimer(2000, id -> testComplete()); }); })); await(); }
@Test public void testRequestEntityTooLarge() { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> fail()); server.listen(onSuccess(ar -> { client.get(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTPS_HOST, path, onSuccess(resp -> { assertEquals(413, resp.statusCode()); resp.request().connection().closeHandler(v -> { testComplete(); }); })).putHeader("Upgrade", "Websocket") .putHeader("Connection", "Upgrade") .end(TestUtils.randomBuffer(8192 + 1)); })); await(); }
@Test public void testClearClientSslOptions() { SelfSignedCertificate certificate = SelfSignedCertificate.create(); HttpServerOptions serverOptions = new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTPS_PORT) .setSsl(true) .setKeyCertOptions(certificate.keyCertOptions()); HttpClientOptions clientOptions = new HttpClientOptions() .setTrustAll(true) .setVerifyHost(false); client = vertx.createHttpClient(clientOptions); server = vertx.createHttpServer(serverOptions).websocketHandler(WebSocketBase::close).listen(onSuccess(server -> { RequestOptions requestOptions = new RequestOptions().setPort(HttpTestBase.DEFAULT_HTTPS_PORT).setSsl(true); client.websocket(requestOptions, ws -> { ws.closeHandler(v -> { testComplete(); }); }); })); await(); }