use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testWSFrames.
private void testWSFrames(boolean binary, 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;
// 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(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"));
AtomicInteger count = new AtomicInteger();
ws.frameHandler(frame -> {
if (frame.isClose()) {
testComplete();
} else {
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(onSuccess(s -> {
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path + "?" + query).setVersion(version);
client = vertx.createHttpClient();
int bsize = 100;
int msgs = 10;
client.webSocket(options, onSuccess(ws -> {
final List<Buffer> sent = new ArrayList<>();
final List<Buffer> received = new ArrayList<>();
String webSocketLocation = ws.headers().get("sec-websocket-location");
if (version == WebsocketVersion.V00) {
assertEquals("ws://" + DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT + uri, webSocketLocation);
} else {
assertNull(webSocketLocation);
}
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++));
}
ws.close();
}
});
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 = 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();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testHAProxy.
@Test
public void testHAProxy() throws Exception {
waitFor(2);
SocketAddress remote = SocketAddress.inetSocketAddress(56324, "192.168.0.1");
SocketAddress local = SocketAddress.inetSocketAddress(443, "192.168.0.11");
Buffer header = HAProxy.createVersion1TCP4ProtocolHeader(remote, local);
HAProxy proxy = new HAProxy(DEFAULT_HTTP_HOST, DEFAULT_HTTP_PORT, header);
proxy.start(vertx);
server = vertx.createHttpServer(new HttpServerOptions().setUseProxyProtocol(true)).webSocketHandler(ws -> {
assertEquals(remote, ws.remoteAddress());
assertEquals(local, ws.localAddress());
ws.handler(buff -> {
complete();
});
}).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v1 -> {
client = vertx.createHttpClient();
client.webSocket(proxy.getPort(), proxy.getHost(), "/someuri", onSuccess(ws -> {
ws.write(Buffer.buffer("foo"), onSuccess(v -> {
complete();
}));
}));
}));
try {
await();
} finally {
proxy.stop();
}
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testContext.
@Test
public void testContext() throws Exception {
int num = 10;
waitFor(num);
Context serverCtx = vertx.getOrCreateContext();
server = vertx.createHttpServer().webSocketHandler(ws -> {
Context current = Vertx.currentContext();
assertSameEventLoop(serverCtx, current);
ws.handler(buff -> {
assertEquals(current, Vertx.currentContext());
});
ws.frameHandler(frame -> {
assertEquals(current, Vertx.currentContext());
});
ws.closeHandler(v -> {
assertEquals(current, Vertx.currentContext());
});
ws.endHandler(v -> {
assertEquals(current, Vertx.currentContext());
complete();
});
});
CountDownLatch latch = new CountDownLatch(1);
serverCtx.runOnContext(v -> {
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> latch.countDown()));
});
awaitLatch(latch);
client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(num));
for (int i = 0; i < num; i++) {
Context clientCtx = vertx.getOrCreateContext();
clientCtx.runOnContext(v -> {
client.webSocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
assertEquals(clientCtx, Vertx.currentContext());
ws.write(Buffer.buffer("data"));
ws.pongHandler(pong -> {
assertEquals(clientCtx, Vertx.currentContext());
ws.close();
});
ws.writePing(Buffer.buffer("ping"));
}));
});
}
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testWriteTextMessages.
private SocketMessages testWriteTextMessages(List<String> messagesToSend, WebsocketVersion version) {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
for (String messageToSend : messagesToSend) {
ws.writeTextMessage(messageToSend);
}
ws.close();
});
List<String> receivedMessages = new ArrayList<>();
List<Throwable> receivedExceptions = new ArrayList<>();
server.listen(ar -> {
assertTrue(ar.succeeded());
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path).setVersion(version);
client = vertx.createHttpClient();
client.webSocket(options, onSuccess(ws -> {
ws.textMessageHandler(receivedMessages::add);
ws.exceptionHandler(receivedExceptions::add);
ws.closeHandler(v -> testComplete());
}));
});
await();
return new SocketMessages(receivedMessages, receivedExceptions);
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testInvalidHandshake.
// Check client response with the ws handler
private void testInvalidHandshake(Consumer<Handler<AsyncResult<HttpClientResponse>>> requestProvider, boolean expectEvent, boolean upgradeRequest, int expectedStatus) {
client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1));
if (upgradeRequest) {
server = vertx.createHttpServer().webSocketHandler(ws -> {
// Check we can get notified
// handshake fails after this method returns and does not reject the ws
assertTrue(expectEvent);
}).requestHandler(req -> {
req.response().end();
});
} else {
AtomicBoolean first = new AtomicBoolean();
server = vertx.createHttpServer().requestHandler(req -> {
if (first.compareAndSet(false, true)) {
try {
req.toWebSocket();
} catch (Exception e) {
// Expected
}
} else {
req.response().end();
}
});
}
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
requestProvider.accept(onSuccess(resp -> {
assertEquals(expectedStatus, resp.statusCode());
resp.endHandler(v1 -> {
// Make another request to check the connection remains usable
client.request(new RequestOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST)).onComplete(onSuccess(req2 -> {
req2.send(onSuccess(resp2 -> {
resp2.endHandler(v2 -> {
testComplete();
});
}));
}));
});
}));
}));
}
Aggregations