use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testHandleWSManually.
@Test
public // Let's manually handle the WebSocket handshake and write a frame to the client
void testHandleWSManually() throws Exception {
String path = "/some/path";
String message = "here is some text data";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
getUpgradedNetSocket(req, path).onComplete(onSuccess(sock -> {
// Let's write a Text frame raw
Buffer buff = Buffer.buffer();
// Text frame
buff.appendByte((byte) 129);
buff.appendByte((byte) message.length());
buff.appendString(message);
sock.write(buff);
}));
});
server.listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
ws.handler(buff -> {
assertEquals(message, buff.toString("UTF-8"));
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testNoRequestHandler.
@Test
public void testNoRequestHandler() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
vertx.createHttpServer().webSocketHandler(ws -> fail()).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v -> latch.countDown()));
awaitLatch(latch);
client = vertx.createHttpClient();
client.request(new RequestOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT)).onComplete(onSuccess(req -> {
req.send(onSuccess(resp -> {
resp.endHandler(v -> {
assertEquals(400, resp.statusCode());
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testWSWriteStream.
private void testWSWriteStream(WebsocketVersion version) throws Exception {
String host = DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT;
String scheme = "http";
String path = "/some/path";
String query = "handshake=bar&wibble=eek";
String uri = path + "?" + query;
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
assertEquals(host, ws.host());
assertEquals(scheme, ws.scheme());
assertEquals(uri, ws.uri());
assertEquals(path, ws.path());
assertEquals(query, ws.query());
assertEquals("upgrade", ws.headers().get("Connection"));
ws.handler(data -> {
ws.write(data);
});
});
server.listen(onSuccess(s -> {
int bsize = 100;
int sends = 10;
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path + "?" + query).setVersion(version);
client = vertx.createHttpClient();
client.webSocket(options, onSuccess(ws -> {
final Buffer received = Buffer.buffer();
ws.handler(data -> {
received.appendBuffer(data);
if (received.length() == bsize * sends) {
ws.close();
testComplete();
}
});
final Buffer sent = Buffer.buffer();
for (int i = 0; i < sends; i++) {
Buffer buff = Buffer.buffer(TestUtils.randomByteArray(bsize));
ws.write(buff);
sent.appendBuffer(buff);
}
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testValidSubProtocol.
private void testValidSubProtocol(WebsocketVersion version) throws Exception {
String path = "/some/path";
List<String> clientSubProtocols = Arrays.asList("clientproto", "commonproto");
List<String> serverSubProtocols = Arrays.asList("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(onSuccess(s -> {
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path).setVersion(version).setSubProtocols(clientSubProtocols);
client = vertx.createHttpClient();
client.webSocket(options, onSuccess(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();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testWriteOnEnd.
@Test
public void testWriteOnEnd() {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(WebSocketBase::close);
server.listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
ws.endHandler(v2 -> {
ws.write(Buffer.buffer("test"), onFailure(err -> {
testComplete();
}));
});
}));
}));
await();
}
Aggregations