private void testInvalidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String subProtocol = "myprotocol"; server = vertx .createHttpServer( new HttpServerOptions() .setPort(HttpTestBase.DEFAULT_HTTP_PORT) .setWebsocketSubProtocol("invalid")) .websocketHandler(ws -> {}); server.listen( onSuccess( ar -> { client .websocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol) .exceptionHandler( t -> { // Should fail testComplete(); }) .handler(ws -> {}); })); await(); }
private void testReject(WebsocketVersion version) throws Exception { String path = "/some/path"; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .websocketHandler( ws -> { assertEquals(path, ws.path()); ws.reject(); }); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version) .exceptionHandler(t -> testComplete()) .handler(ws -> fail("Should not be called")); }); await(); }
@Test public void testClearClientHandlersOnEnd() { String path = "/some/path"; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .websocketHandler(WebSocketBase::close); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null) .handler( ws -> { ws.endHandler( v -> { try { ws.endHandler(null); ws.exceptionHandler(null); ws.handler(null); } catch (Exception e) { fail( "Was expecting to set to null the handlers when the socket is closed"); return; } testComplete(); }); }); }); await(); }
@Test public void testEndHandlerCalled() { String path = "/some/path"; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .websocketHandler(WebSocketBase::close); AtomicInteger doneCount = new AtomicInteger(); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null) .endHandler(done -> doneCount.incrementAndGet()) .handler( ws -> { assertEquals(0, doneCount.get()); ws.closeHandler( v -> { assertEquals(1, doneCount.get()); testComplete(); }); }); }); await(); }
@Test public void testWebsocketPauseAndResume() { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setConnectTimeout(1000)); String path = "/some/path"; this.server = vertx.createHttpServer( new HttpServerOptions().setAcceptBacklog(1).setPort(HttpTestBase.DEFAULT_HTTP_PORT)); AtomicBoolean paused = new AtomicBoolean(); ReadStream<ServerWebSocket> stream = server.websocketStream(); stream.handler( ws -> { assertFalse(paused.get()); ws.writeMessage(Buffer.buffer("whatever")); ws.close(); }); server.listen( listenAR -> { assertTrue(listenAR.succeeded()); stream.pause(); paused.set(true); client .websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path) .exceptionHandler( err -> { assertTrue(paused.get()); assertTrue(err instanceof WebSocketHandshakeException); paused.set(false); stream.resume(); client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, ws -> { ws.handler( buffer -> { assertEquals("whatever", buffer.toString("UTF-8")); ws.closeHandler( v2 -> { testComplete(); }); }); }); }) .handler(ws -> fail()); }); await(); }
@Test // Let's manually handle the websocket handshake and write a frame to the client public void testHandleWSManually() throws Exception { String path = "/some/path"; String message = "here is some text data"; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .requestHandler( req -> { NetSocket sock = getUpgradedNetSocket(req, path); // Let's write a Text frame raw Buffer buff = Buffer.buffer(); buff.appendByte((byte) 129); // Text frame buff.appendByte((byte) message.length()); buff.appendString(message); sock.write(buff); }); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path) .exceptionHandler(t -> fail(t.getMessage())) .handler( ws -> { ws.handler( buff -> { assertEquals(message, buff.toString("UTF-8")); testComplete(); }); }); }); await(); }
private void testTLS( KS clientCert, TS clientTrust, KS serverCert, TS serverTrust, boolean requireClientAuth, boolean serverUsesCrl, boolean clientTrustAll, boolean clientUsesCrl, boolean shouldPass, String... enabledCipherSuites) throws Exception { HttpClientOptions options = new HttpClientOptions(); options.setSsl(true); if (clientTrustAll) { options.setTrustAll(true); } if (clientUsesCrl) { options.addCrlPath(findFileOnClasspath("tls/ca/crl.pem")); } options.setTrustStoreOptions(getClientTrustOptions(clientTrust)); options.setKeyStoreOptions(getClientCertOptions(clientCert)); for (String suite : enabledCipherSuites) { options.addEnabledCipherSuite(suite); } client = vertx.createHttpClient(options); HttpServerOptions serverOptions = new HttpServerOptions(); serverOptions.setSsl(true); serverOptions.setTrustStoreOptions(getServerTrustOptions(serverTrust)); serverOptions.setKeyStoreOptions(getServerCertOptions(serverCert)); if (requireClientAuth) { serverOptions.setClientAuthRequired(true); } if (serverUsesCrl) { serverOptions.addCrlPath(findFileOnClasspath("tls/ca/crl.pem")); } for (String suite : enabledCipherSuites) { serverOptions.addEnabledCipherSuite(suite); } server = vertx.createHttpServer(serverOptions.setPort(4043)); server.websocketHandler( ws -> { ws.handler(ws::write); }); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket(4043, HttpTestBase.DEFAULT_HTTP_HOST, "/") .exceptionHandler( t -> { if (shouldPass) { t.printStackTrace(); fail("Should not throw exception"); } else { testComplete(); } }) .handler( ws -> { int size = 100; Buffer received = Buffer.buffer(); ws.handler( data -> { received.appendBuffer(data); if (received.length() == size) { ws.close(); testComplete(); } }); Buffer buff = Buffer.buffer(TestUtils.randomByteArray(size)); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); }); await(); }