@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(); }
RawWSSockJSSocket(Vertx vertx, Session webSession, User webUser, ServerWebSocket ws) { super(vertx, webSession, webUser); this.ws = ws; ws.closeHandler(v -> { // Make sure the writeHandler gets unregistered RawWSSockJSSocket.super.close(); }); }
@Test public void testCloseStatusCodeFromClient() 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(); }); socket.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(); }); }); awaitLatch(latch); }
@Test public void testCloseCustomPayloadFromClient() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(STATUS_CODE, REASON); }); }); awaitLatch(latch); }
@Test public void testCloseFrame() throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { if (frame.isText()) { assertIllegalStateException(frame::closeStatusCode); } else { assertEquals(frame.closeReason(), "It was a good talk"); assertEquals(frame.closeStatusCode(), 1001); } latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.writeTextMessage("Hello"); ws.close((short)1001, "It was a good talk"); }); }); awaitLatch(latch); }
@Test public void testCloseCustomPayloadFromServer() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; 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(STATUS_CODE, REASON)); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }); }); awaitLatch(latch); }
@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 testServerWebSocket() { server = vertx.createHttpServer(); server.websocketHandler(ws -> { FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server); WebSocketMetric metric = metrics.getMetric(ws); assertNotNull(metric); assertNotNull(metric.soMetric); ws.handler(ws::write); ws.closeHandler(closed -> { assertNull(metrics.getMetric(ws)); testComplete(); }); }); server.listen(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, ar -> { assertTrue(ar.succeeded()); client = vertx.createHttpClient(); client.websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.write(Buffer.buffer("wibble")); ws.handler(buff -> ws.close()); }); }); await(); }
server = vertx.createHttpServer(); server.websocketHandler(ws -> { ws.closeHandler(v -> { complete(); });
@Test public void testServerWebSocketShouldBeClosedWhenTheClosedHandlerIsCalled() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), ws); sender.send(); ws.closeHandler(v -> { Throwable failure = sender.close(); if (failure != null) { fail(failure); } else { testComplete(); } }); }); server.listen(onSuccess(s -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", ws -> { vertx.setTimer(1000, id -> { ws.close(); }); }); })); await(); }
@Test public void testServerWebSocketUpgrade() { server = vertx.createHttpServer(); server.requestHandler(req -> { FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server); assertNotNull(metrics.getMetric(req)); ServerWebSocket ws = req.upgrade(); assertNull(metrics.getMetric(req)); WebSocketMetric metric = metrics.getMetric(ws); assertNotNull(metric); assertNotNull(metric.soMetric); ws.handler(buffer -> ws.write(buffer)); ws.closeHandler(closed -> { WebSocketMetric a = metrics.getMetric(ws); assertNull(a); testComplete(); }); }); server.listen(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, ar -> { assertTrue(ar.succeeded()); client = vertx.createHttpClient(); client.websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.write(Buffer.buffer("wibble")); ws.handler(buff -> { ws.close(); }); }); }); await(); }
ws.closeHandler(v -> { closed[0] = true;
WebSocketListener(ServerWebSocket ws, SockJSSession session) { this.ws = ws; this.session = session; ws.textMessageHandler(this::handleMessages); ws.closeHandler(v -> { closed = true; session.shutdown(); }); ws.exceptionHandler(t -> { closed = true; session.shutdown(); session.handleException(t); }); }
@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()); HttpClientRequest request = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/some/path"); handshake(request, 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(); }
@Test public void testCloseCustomPayloadFromClient() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(STATUS_CODE, REASON); }); }); awaitLatch(latch); }
@Test public void testCloseStatusCodeFromClient() 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(); }); socket.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(); }); }); awaitLatch(latch); }
@Test public void testCloseCustomPayloadFromServer() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; 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(STATUS_CODE, REASON)); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }); }); awaitLatch(latch); }
@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 testServerWebSocket() { server = vertx.createHttpServer(); server.websocketHandler(ws -> { FakeHttpServerMetrics metrics = FakeMetricsBase.getMetrics(server); WebSocketMetric metric = metrics.getMetric(ws); assertNotNull(metric); assertNotNull(metric.soMetric); ws.handler(ws::write); ws.closeHandler(closed -> { assertNull(metrics.getMetric(ws)); testComplete(); }); }); server.listen(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, ar -> { assertTrue(ar.succeeded()); client = vertx.createHttpClient(); client.websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.write(Buffer.buffer("wibble")); ws.handler(buff -> ws.close()); }); }); await(); }
@Test public void testServerWebSocketShouldBeClosedWhenTheClosedHandlerIsCalled() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), ws); sender.send(); ws.closeHandler(v -> { Throwable failure = sender.close(); if (failure != null) { fail(failure); } else { testComplete(); } }); }); server.listen(onSuccess(s -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", ws -> { vertx.setTimer(1000, id -> { ws.close(); }); }); })); await(); }