use of io.vertx.core.http.impl.Http1xClientConnection 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