use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testUpgrade.
private void testUpgrade(boolean delayed) {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
server.requestHandler(request -> {
Runnable runner = () -> {
request.toWebSocket().onComplete(onSuccess(ws -> {
HttpServerResponse response = request.response();
assertTrue(response.ended());
try {
response.putHeader("foo", "bar");
fail();
} catch (IllegalStateException ignore) {
}
try {
response.end();
fail();
} catch (IllegalStateException ignore) {
}
ws.handler(buff -> {
ws.write(Buffer.buffer("helloworld"));
ws.close();
});
}));
};
if (delayed) {
// This tests the case where the last http content comes of the request (its not full) comes in
// before the upgrade has happened and before HttpServerImpl.expectWebsockets is true
request.pause();
vertx.runOnContext(v -> {
runner.run();
});
} else {
runner.run();
}
});
server.listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
Buffer buff = Buffer.buffer();
ws.handler(buff::appendBuffer);
ws.endHandler(v -> {
// Last two bytes are status code payload
assertEquals("helloworld", buff.toString("UTF-8"));
testComplete();
});
ws.write(Buffer.buffer("foo"));
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testCloseStatusCodeFromClient.
@Test
public void testCloseStatusCodeFromClient() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(2);
client = vertx.createHttpClient();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(socket -> {
socket.closeHandler(a -> {
latch.countDown();
});
socket.frameHandler(frame -> {
assertEquals(1000, frame.binaryData().getByteBuf().getShort(0));
assertEquals(1000, frame.closeStatusCode());
assertNull(frame.closeReason());
latch.countDown();
});
}).listen(ar -> {
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(WebSocketBase::close));
});
awaitLatch(latch);
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testPausedBeforeClosed.
@Test
public void testPausedBeforeClosed() {
waitFor(2);
Buffer expected = TestUtils.randomBuffer(128);
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
AtomicBoolean paused = new AtomicBoolean(true);
ws.pause();
ws.closeHandler(v1 -> {
paused.set(false);
vertx.runOnContext(v2 -> {
ws.resume();
});
});
ws.handler(buffer -> {
assertFalse(paused.get());
assertEquals(expected, buffer);
complete();
});
ws.endHandler(v -> {
assertFalse(paused.get());
complete();
});
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
ws.write(expected);
ws.close();
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testWorker.
@Test
public void testWorker() {
waitFor(2);
DeploymentOptions deploymentOptions = new DeploymentOptions().setWorker(true);
vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start(Promise<Void> startPromise) {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
server.webSocketHandler(ws -> {
assertTrue(Context.isOnWorkerThread());
ws.handler(msg -> {
assertTrue(Context.isOnWorkerThread());
ws.write(Buffer.buffer("pong"));
});
ws.endHandler(v -> {
assertTrue(Context.isOnWorkerThread());
complete();
});
});
server.listen().<Void>mapEmpty().onComplete(startPromise);
}
}, deploymentOptions, onSuccess(serverID -> {
vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start() {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
assertTrue(Context.isOnWorkerThread());
ws.write(Buffer.buffer("ping"));
ws.handler(buf -> {
assertTrue(Context.isOnWorkerThread());
ws.end();
});
ws.endHandler(v -> {
assertTrue(Context.isOnWorkerThread());
complete();
});
}));
}
}, deploymentOptions, onSuccess(id -> {
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testServerWebSocketSendPongExceeds125Bytes.
@Test
public void testServerWebSocketSendPongExceeds125Bytes() {
// 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 -> {
ws.writeFrame(WebSocketFrame.pongFrame(Buffer.buffer(pingBody)));
}).listen(onSuccess(v -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
ws.pongHandler(buff -> fail());
vertx.setTimer(2000, id -> testComplete());
}));
}));
await();
}
Aggregations