use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testCloseServer.
@Test
public void testCloseServer() {
client = vertx.createHttpClient();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(socket -> {
socket.textMessageHandler(msg -> {
server.close();
});
}).listen(onSuccess(s -> {
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
ws.writeTextMessage("ping");
AtomicBoolean closeFrameReceived = new AtomicBoolean();
ws.frameHandler(frame -> {
if (frame.isClose()) {
closeFrameReceived.set(true);
}
});
ws.endHandler(v -> {
assertTrue(closeFrameReceived.get());
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testReceiveHttpResponseHeadersOnClient.
@Test
public void testReceiveHttpResponseHeadersOnClient() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
handshakeWithCookie(req);
});
AtomicReference<WebSocket> webSocketRef = new AtomicReference<>();
server.listen(onSuccess(s -> {
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/some/path", onSuccess(ws -> {
MultiMap entries = ws.headers();
assertNotNull(entries);
assertFalse(entries.isEmpty());
assertEquals("websocket".toLowerCase(), entries.get("Upgrade").toLowerCase());
assertEquals("upgrade".toLowerCase(), entries.get("Connection").toLowerCase());
Set<String> cookiesToSet = new HashSet(entries.getAll("Set-Cookie"));
assertEquals(2, cookiesToSet.size());
assertTrue(cookiesToSet.contains("SERVERID=test-server-id"));
assertTrue(cookiesToSet.contains("JSONID=test-json-id"));
webSocketRef.set(ws);
vertx.runOnContext(v -> {
assertNull(ws.headers());
testComplete();
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testWebSocketPauseAndResume.
@Test
public void testWebSocketPauseAndResume() {
client = vertx.createHttpClient(new HttpClientOptions().setConnectTimeout(1000));
this.server = vertx.createHttpServer(new HttpServerOptions().setAcceptBacklog(1).setPort(DEFAULT_HTTP_PORT));
AtomicBoolean paused = new AtomicBoolean();
ReadStream<ServerWebSocket> stream = server.webSocketStream();
stream.handler(ws -> {
assertFalse(paused.get());
ws.writeBinaryMessage(Buffer.buffer("whatever"));
ws.close();
});
server.listen(listenAR -> {
assertTrue(listenAR.succeeded());
stream.pause();
paused.set(true);
connectUntilWebSocketReject(client, 0, res -> {
if (!res.succeeded()) {
fail(new AssertionError("Was expecting error to be WebSocketHandshakeException", res.cause()));
}
assertTrue(paused.get());
paused.set(false);
stream.resume();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", onSuccess(ws -> {
ws.handler(buffer -> {
assertEquals("whatever", buffer.toString("UTF-8"));
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 testCloseClient.
@Test
public void testCloseClient() {
client = vertx.createHttpClient();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
AtomicBoolean closeFrameReceived = new AtomicBoolean();
ws.frameHandler(frame -> {
if (frame.isClose()) {
closeFrameReceived.set(true);
}
});
ws.endHandler(v -> {
assertTrue(closeFrameReceived.get());
testComplete();
});
}).listen(onSuccess(s -> {
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", onSuccess(ws -> {
client.close();
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class WebSocketTest method testDontReceiveMessagerAfterCloseHandlerCalled.
@Test
public void testDontReceiveMessagerAfterCloseHandlerCalled() {
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
boolean[] closed = new boolean[1];
ws.handler(msg -> {
// We will still receive messages after the close frame is sent
if (closed[0]) {
fail("Should not receive a message after close handler callback");
}
});
ws.closeHandler(v -> {
closed[0] = true;
// Let some time to let message arrive in the handler
vertx.setTimer(10, id -> {
testComplete();
});
});
vertx.setTimer(500, id -> {
// Fill the buffer, so the close frame will be delayed
while (!ws.writeQueueFull()) {
ws.write(TestUtils.randomBuffer(1000));
}
// Send the close frame, the TCP connection will be closed after that frame is sent
ws.close();
});
});
server.listen(onSuccess(s -> {
// Create a new client that will use the same event-loop than the server
// so the ws.writeQueueFull() will return true since the client won't be able to read the socket
// when the server is busy writing the WebSocket
client = vertx.createHttpClient();
client.webSocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(ws -> {
CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), ws);
ws.closeHandler(v -> sender.close());
sender.send();
}));
}));
await();
}
Aggregations