/** * This does not set up a handler on the websocket */ private WebSocket setupRawWebsocketClient(String serverPath) throws InterruptedException { String requestURI = serverPath + "/websocket"; AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>(); CountDownLatch openSocketCountDown = new CountDownLatch(1); client.websocket(requestURI, ws -> { openedWebSocketReference.set(ws); openSocketCountDown.countDown(); ws.endHandler(v -> testComplete()); ws.exceptionHandler(this::fail); }); openSocketCountDown.await(5, TimeUnit.SECONDS); return openedWebSocketReference.get(); }
/** * This sets up a handler on the websocket */ private WebSocket setupSockJsClient(String serverPath, List<Buffer> receivedMessagesCollector) throws InterruptedException { String requestURI = serverPath + "/000/000/websocket"; AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>(); CountDownLatch openSocketCountDown = new CountDownLatch(1); client.websocket(requestURI, ws -> { openedWebSocketReference.set(ws); ws.handler(replyBuffer -> { log.debug("Client received " + replyBuffer); String textReply = replyBuffer.toString(); if ("o".equals(textReply)) { openSocketCountDown.countDown(); } else { receivedMessagesCollector.add(replyBuffer); } }); ws.endHandler(v -> testComplete()); ws.exceptionHandler(this::fail); }); openSocketCountDown.await(5, TimeUnit.SECONDS); return openedWebSocketReference.get(); }
@Test public void testInvalidMessageCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("SockJSHandlerTest.testInvalidMessageCode"))) ); vertx.eventBus().consumer("SockJSHandlerTest.testInvalidMessageCode", msg -> msg.reply(new JsonObject())); client.websocket("/ws-timeout/websocket", ws -> { ws.writeFinalBinaryFrame(Buffer.buffer("durp!")); ws.frameHandler(frame -> { // we should get a normal frame with a error message if (!frame.isClose()) { JsonObject msg = new JsonObject(frame.binaryData()); assertEquals("err", msg.getString("type")); assertEquals("invalid_json", msg.getString("body")); testComplete(); ws.close(); } }); }); await(); } }
@Test public void testSendWebsocketContinuationFrames() { // Use raw websocket transport client.websocket("/echo/websocket", ws -> { int size = 65535; Buffer buffer1 = TestUtils.randomBuffer(size); Buffer buffer2 = TestUtils.randomBuffer(size); ws.writeFrame(io.vertx.core.http.WebSocketFrame.binaryFrame(buffer1, false)); ws.writeFrame(io.vertx.core.http.WebSocketFrame.continuationFrame(buffer2, true)); Buffer received = Buffer.buffer(); ws.handler(buff -> { received.appendBuffer(buff); if (received.length() == size * 2) { testComplete(); } }); }); await(); }
@Test public void testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }
@Test public void testCookiesRemoved() throws Exception { router.route("/cookiesremoved/*").handler(SockJSHandler.create(vertx) .socketHandler(sock -> { MultiMap headers = sock.headers(); String cookieHeader = headers.get("cookie"); assertNotNull(cookieHeader); assertEquals("JSESSIONID=wibble", cookieHeader); testComplete(); })); MultiMap headers = new CaseInsensitiveHeaders(); headers.add("cookie", "JSESSIONID=wibble"); headers.add("cookie", "flibble=floob"); client.websocket("/cookiesremoved/websocket", headers, ws -> { String frame = "foo"; ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(frame, true)); }); await(); }
/** * This does not set up a handler on the websocket */ private WebSocket setupRawWebsocketClient(String serverPath) throws InterruptedException { String requestURI = serverPath + "/websocket"; AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>(); CountDownLatch openSocketCountDown = new CountDownLatch(1); client.websocket(requestURI, ws -> { openedWebSocketReference.set(ws); openSocketCountDown.countDown(); ws.endHandler(v -> testComplete()); ws.exceptionHandler(this::fail); }); openSocketCountDown.await(5, TimeUnit.SECONDS); return openedWebSocketReference.get(); }
/** * This sets up a handler on the websocket */ private WebSocket setupSockJsClient(String serverPath, List<Buffer> receivedMessagesCollector) throws InterruptedException { String requestURI = serverPath + "/000/000/websocket"; AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>(); CountDownLatch openSocketCountDown = new CountDownLatch(1); client.websocket(requestURI, ws -> { openedWebSocketReference.set(ws); ws.handler(replyBuffer -> { log.debug("Client received " + replyBuffer); String textReply = replyBuffer.toString(); if ("o".equals(textReply)) { openSocketCountDown.countDown(); } else { receivedMessagesCollector.add(replyBuffer); } }); ws.endHandler(v -> testComplete()); ws.exceptionHandler(this::fail); }); openSocketCountDown.await(5, TimeUnit.SECONDS); return openedWebSocketReference.get(); }
@Test public void testInvalidMessageCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("SockJSHandlerTest.testInvalidMessageCode"))) ); vertx.eventBus().consumer("SockJSHandlerTest.testInvalidMessageCode", msg -> msg.reply(new JsonObject())); client.websocket("/ws-timeout/websocket", ws -> { ws.writeFinalBinaryFrame(Buffer.buffer("durp!")); ws.frameHandler(frame -> { // we should get a normal frame with a error message if (!frame.isClose()) { JsonObject msg = new JsonObject(frame.binaryData()); assertEquals("err", msg.getString("type")); assertEquals("invalid_json", msg.getString("body")); testComplete(); ws.close(); } }); }); await(); } }
@Test public void testSendWebsocketContinuationFrames() { // Use raw websocket transport client.websocket("/echo/websocket", ws -> { int size = 65535; Buffer buffer1 = TestUtils.randomBuffer(size); Buffer buffer2 = TestUtils.randomBuffer(size); ws.writeFrame(io.vertx.core.http.WebSocketFrame.binaryFrame(buffer1, false)); ws.writeFrame(io.vertx.core.http.WebSocketFrame.continuationFrame(buffer2, true)); Buffer received = Buffer.buffer(); ws.handler(buff -> { received.appendBuffer(buff); if (received.length() == size * 2) { testComplete(); } }); }); await(); }
@Test public void testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }
@Test public void testCookiesRemoved() throws Exception { router.route("/cookiesremoved/*").handler(SockJSHandler.create(vertx) .socketHandler(sock -> { MultiMap headers = sock.headers(); String cookieHeader = headers.get("cookie"); assertNotNull(cookieHeader); assertEquals("JSESSIONID=wibble", cookieHeader); testComplete(); })); MultiMap headers = new CaseInsensitiveHeaders(); headers.add("cookie", "JSESSIONID=wibble"); headers.add("cookie", "flibble=floob"); client.websocket("/cookiesremoved/websocket", headers, ws -> { String frame = "foo"; ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(frame, true)); }); await(); }