use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testContinuationWriteFromConnectHandler.
private void testContinuationWriteFromConnectHandler(WebsocketVersion version) throws Exception {
String path = "/some/path";
String firstFrame = "AAA";
String continuationFrame = "BBB";
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();
// Incomplete Text frame
buff.appendByte((byte) 0x01);
buff.appendByte((byte) firstFrame.length());
buff.appendString(firstFrame);
sock.write(buff);
buff = Buffer.buffer();
// Complete continuation frame
buff.appendByte((byte) (0x00 | 0x80));
buff.appendByte((byte) continuationFrame.length());
buff.appendString(continuationFrame);
sock.write(buff);
}));
});
server.listen(onSuccess(s -> {
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path).setVersion(version);
client = vertx.createHttpClient();
client.webSocket(options, onSuccess(ws -> {
AtomicBoolean receivedFirstFrame = new AtomicBoolean();
ws.frameHandler(received -> {
Buffer receivedBuffer = Buffer.buffer(received.textData());
if (!received.isFinal()) {
assertEquals(firstFrame, receivedBuffer.toString());
receivedFirstFrame.set(true);
} else if (receivedFirstFrame.get() && received.isFinal()) {
assertEquals(continuationFrame, receivedBuffer.toString());
ws.close();
testComplete();
}
});
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testConnectWithWebSocketCompressionDisabled.
@Test
public // Test server accepting no compression
void testConnectWithWebSocketCompressionDisabled() throws Exception {
String path = "/some/path";
Buffer buff = Buffer.buffer("AAA");
// Server should have basic compression enabled by default,
// client needs to ask for it
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setPerFrameWebSocketCompressionSupported(false).setPerMessageWebSocketCompressionSupported(false)).webSocketHandler(ws -> {
assertEquals("upgrade", ws.headers().get("Connection"));
assertNull(ws.headers().get("sec-websocket-extensions"));
ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
});
server.listen(onSuccess(s -> {
HttpClientOptions options = new HttpClientOptions();
client = vertx.createHttpClient(options);
client.webSocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
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_HOST in project vert.x by eclipse.
the class WebSocketTest method testRequestEntityTooLarge.
@Test
public void testRequestEntityTooLarge() {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> fail());
server.listen(onSuccess(ar -> {
client = vertx.createHttpClient();
client.request(new RequestOptions().setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT).setURI(path), onSuccess(req -> req.putHeader("Upgrade", "Websocket").putHeader("Connection", "Upgrade").send(TestUtils.randomBuffer(8192 + 1), onSuccess(resp -> {
assertEquals(413, resp.statusCode());
resp.request().connection().closeHandler(v -> {
testComplete();
});
}))));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testReject.
private void testReject(WebsocketVersion version, Integer rejectionStatus, int expectedRejectionStatus) throws Exception {
String path = "/some/path";
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
assertEquals(path, ws.path());
if (rejectionStatus != null) {
ws.reject(rejectionStatus);
} else {
ws.reject();
}
});
server.listen(onSuccess(s -> {
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(DEFAULT_HTTP_PORT).setHost(DEFAULT_HTTP_HOST).setURI(path).setVersion(version);
client = vertx.createHttpClient();
client.webSocket(options, onFailure(t -> {
assertTrue(t instanceof UpgradeRejectedException);
assertEquals(expectedRejectionStatus, ((UpgradeRejectedException) t).getStatus());
testComplete();
}));
}));
await();
}
use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST in project vert.x by eclipse.
the class WebSocketTest method testNormalWSPermessageDeflateCompressionNegotiation.
@Test
public // Test normal negotiation of WebSocket compression
void testNormalWSPermessageDeflateCompressionNegotiation() throws Exception {
String path = "/some/path";
Buffer buff = Buffer.buffer("AAA");
// Server should have basic compression enabled by default,
// client needs to ask for it
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).webSocketHandler(ws -> {
assertEquals("upgrade", ws.headers().get("Connection"));
assertEquals("permessage-deflate;client_max_window_bits", ws.headers().get("sec-websocket-extensions"));
ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
});
server.listen(ar -> {
assertTrue(ar.succeeded());
HttpClientOptions options = new HttpClientOptions();
options.setTryUsePerMessageWebSocketCompression(true);
client = vertx.createHttpClient(options);
client.webSocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, path, onSuccess(ws -> {
final Buffer received = Buffer.buffer();
ws.handler(data -> {
received.appendBuffer(data);
if (received.length() == buff.length()) {
assertEquals(buff, received);
ws.close();
testComplete();
}
});
}));
});
await();
}
Aggregations