private void testValidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String subProtocol = "myprotocol"; Buffer buff = Buffer.buffer("AAA"); server = vertx .createHttpServer( new HttpServerOptions() .setPort(HttpTestBase.DEFAULT_HTTP_PORT) .setWebsocketSubProtocol(subProtocol)) .websocketHandler( ws -> { assertEquals(path, ws.path()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen( ar -> { assertTrue(ar.succeeded()); client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol, ws -> { final Buffer received = Buffer.buffer(); ws.handler( data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
@Test public void testWebsocketFrameFactoryArguments() throws Exception { assertNullPointerException(() -> WebSocketFrame.binaryFrame(null, true)); assertNullPointerException(() -> WebSocketFrame.textFrame(null, true)); assertNullPointerException(() -> WebSocketFrame.continuationFrame(null, true)); }
private void testWSFrames(boolean binary, WebsocketVersion version) throws Exception { String path = "/some/path"; String query = "foo=bar&wibble=eek"; String uri = path + "?" + query; // version 0 doesn't support continuations so we just send 1 frame per message int frames = version == WebsocketVersion.V00 ? 1 : 10; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .websocketHandler( ws -> { assertEquals(uri, ws.uri()); assertEquals(path, ws.path()); assertEquals(query, ws.query()); assertEquals("Upgrade", ws.headers().get("Connection")); AtomicInteger count = new AtomicInteger(); ws.frameHandler( frame -> { if (count.get() == 0) { if (binary) { assertTrue(frame.isBinary()); assertFalse(frame.isText()); } else { assertFalse(frame.isBinary()); assertTrue(frame.isText()); } assertFalse(frame.isContinuation()); } else { assertFalse(frame.isBinary()); assertFalse(frame.isText()); assertTrue(frame.isContinuation()); } if (count.get() == frames - 1) { assertTrue(frame.isFinal()); } else { assertFalse(frame.isFinal()); } ws.writeFrame(frame); if (count.incrementAndGet() == frames) { count.set(0); } }); }); server.listen( ar -> { assertTrue(ar.succeeded()); int bsize = 100; int msgs = 10; client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path + "?" + query, null, version, ws -> { final List<Buffer> sent = new ArrayList<>(); final List<Buffer> received = new ArrayList<>(); AtomicReference<Buffer> currentReceived = new AtomicReference<>(Buffer.buffer()); ws.frameHandler( frame -> { // received.appendBuffer(frame.binaryData()); currentReceived.get().appendBuffer(frame.binaryData()); if (frame.isFinal()) { received.add(currentReceived.get()); currentReceived.set(Buffer.buffer()); } if (received.size() == msgs) { int pos = 0; for (Buffer rec : received) { assertEquals(rec, sent.get(pos++)); } testComplete(); } }); AtomicReference<Buffer> currentSent = new AtomicReference<>(Buffer.buffer()); for (int i = 0; i < msgs; i++) { for (int j = 0; j < frames; j++) { Buffer buff; WebSocketFrame frame; if (binary) { buff = Buffer.buffer(TestUtils.randomByteArray(bsize)); if (j == 0) { frame = WebSocketFrame.binaryFrame(buff, false); } else { frame = WebSocketFrame.continuationFrame(buff, j == frames - 1); } } else { String str = TestUtils.randomAlphaString(bsize); buff = Buffer.buffer(str); if (j == 0) { frame = WebSocketFrame.textFrame(str, false); } else { frame = WebSocketFrame.continuationFrame(buff, j == frames - 1); } } currentSent.get().appendBuffer(buff); ws.writeFrame(frame); if (j == frames - 1) { sent.add(currentSent.get()); currentSent.set(Buffer.buffer()); } } } }); }); 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(); }