use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testInvalidSubProtocol.
private void testInvalidSubProtocol(WebsocketVersion version) throws Exception {
String path = "/some/path";
String subProtocol = "myprotocol";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).addWebSocketSubProtocol("invalid")).webSocketHandler(ws -> {
});
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path).setVersion(version).addSubProtocol(subProtocol);
server.listen(onSuccess(ar -> {
client = vertx.createHttpClient();
client.webSocket(options, onFailure(err -> {
// Should fail
testComplete();
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testClientWebSocketSendPingExceeds125Bytes.
@Test
public void testClientWebSocketSendPingExceeds125Bytes() {
// Netty will prevent us from encoding a pingBody greater than 126 bytes by silently throwing an error in the background
String pingBody = randomAlphaString(126);
int maxFrameSize = 256;
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
ws.pongHandler(buffer -> fail());
ws.writeFrame(WebSocketFrame.pingFrame(Buffer.buffer(pingBody)));
vertx.setTimer(2000, id -> testComplete());
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketReceivePingExceedsMaxFrameSize.
@Test
public void testServerWebSocketReceivePingExceedsMaxFrameSize() {
String pingBody = randomAlphaString(113);
Integer maxFrameSize = 64;
Buffer ping1 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(0, maxFrameSize));
Buffer ping2 = Buffer.buffer(Buffer.buffer(pingBody.getBytes()).getBytes(maxFrameSize, pingBody.length()));
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
List<Buffer> pongs = new ArrayList<>();
ws.pongHandler(pong -> {
pongs.add(pong);
if (pongs.size() == 2) {
assertEquals(pongs, Arrays.asList(ping1, ping2));
testComplete();
}
});
try {
ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PING, ping1.copy().getByteBuf(), false));
ws.writeFrame(new WebSocketFrameImpl(WebSocketFrameType.PING, ping2.copy().getByteBuf(), true));
} catch (Throwable t) {
fail(t);
}
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketPingPongCheck.
private void testServerWebSocketPingPongCheck(int maxFrameSize, Function<ServerWebSocket, Future<Void>> check) {
Pattern pattern = Pattern.compile("^P[io]ng cannot exceed maxWebSocketFrameSize or 125 bytes$");
server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST).setMaxWebSocketFrameSize(maxFrameSize));
server.webSocketHandler(ws -> {
ws.pongHandler(buff -> fail());
check.apply(ws).onComplete(onFailure(err -> {
Matcher matcher = pattern.matcher(err.getMessage());
if (matcher.matches()) {
ws.close();
} else {
fail("Unexpected error message" + err.getMessage());
}
}));
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
ws.closeHandler(v2 -> {
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testServerCloseHandshake.
@Test
public void testServerCloseHandshake() {
short status = (short) (4000 + TestUtils.randomPositiveInt() % 100);
waitFor(2);
server = vertx.createHttpServer();
server.webSocketHandler(ws -> {
ws.closeHandler(sc -> {
assertEquals((Short) (short) status, ws.closeStatusCode());
complete();
});
});
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v1 -> {
client = vertx.createHttpClient();
handshake(client, req -> {
req.send(onSuccess(resp -> {
assertEquals(101, resp.statusCode());
Http1xClientConnection conn = (Http1xClientConnection) req.connection();
NetSocketInternal soi = conn.toNetSocket();
soi.channelHandlerContext().pipeline().addBefore("handler", "encoder", new WebSocket13FrameEncoder(true));
soi.channelHandlerContext().pipeline().addBefore("handler", "decoder", new WebSocket13FrameDecoder(false, false, 1000));
String reason = randomAlphaString(10);
soi.writeMessage(new CloseWebSocketFrame(status, reason));
AtomicBoolean closeFrameReceived = new AtomicBoolean();
soi.messageHandler(msg -> {
try {
if (msg instanceof CloseWebSocketFrame) {
CloseWebSocketFrame frame = (CloseWebSocketFrame) msg;
assertEquals(status, frame.statusCode());
assertEquals(reason, frame.reasonText());
closeFrameReceived.set(true);
}
} finally {
ReferenceCountUtil.release(msg);
}
});
soi.closeHandler(v2 -> {
assertTrue(closeFrameReceived.get());
complete();
});
}));
});
}));
await();
}
Aggregations