client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", ws -> { ws.closeHandler(v -> latchClient.countDown()); ws.close(); });
} else if (receivedFirstFrame.get() && received.isFinal()) { assertEquals(continuationFrame, receivedBuffer.toString()); ws.close(); testComplete();
private void testCloseCallHandlers(boolean local) { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { if (!local) { ws.close(); } }); AtomicInteger doneCount = new AtomicInteger(); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null). endHandler(done -> doneCount.incrementAndGet()). handler(ws -> { assertEquals(0, doneCount.get()); boolean[] closed = new boolean[1]; ws.closeHandler(v -> { closed[0] = true; assertEquals(1, doneCount.get()); testComplete(); }); if (local) { vertx.runOnContext(v -> { ws.close(); }); } }); }); await(); }
private void testWriteFromConnectHandler(WebsocketVersion version) throws Exception { String path = "/some/path"; Buffer buff = Buffer.buffer("AAA"); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { assertEquals(path, ws.path()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { Buffer received = Buffer.buffer(); ws.handler(data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
}); }); ws.close(); }); ws.write(Buffer.buffer("hello"));
@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); }
}); }); ws.close(); }); ws.write(Buffer.buffer("hello"));
@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); }
client = vertx.createHttpClient(opts).websocket(port, "localhost", "/", ws -> { addResult(new AssertionError("Websocket unexpectedly connected")); ws.close(); }, t -> { addResult(t);
@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 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(); }
@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(); }
received.appendBuffer(data); if (received.length() == bsize * sends) { ws.close(); testComplete();
private void testValidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String clientSubProtocols = "clientproto,commonproto"; String serverSubProtocols = "serverproto,commonproto"; Buffer buff = Buffer.buffer("AAA"); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols(serverSubProtocols)).websocketHandler(ws -> { assertEquals(path, ws.path()); assertNull(ws.subProtocol()); ws.accept(); assertEquals("commonproto", ws.subProtocol()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, clientSubProtocols, ws -> { assertEquals("commonproto", ws.subProtocol()); final Buffer received = Buffer.buffer(); ws.handler(data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete();
if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete();
if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete();
received.appendBuffer(data); if (received.length() == size) { ws.close(); testComplete();
} else { frameConsumer.accept(frame); ws.close();