use of io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT in project vert.x by eclipse.
the class HAProxyTest method testHttpWithoutHAProxySupport.
@Test
public void testHttpWithoutHAProxySupport() {
Vertx vertx = Vertx.vertx();
try {
vertx.createHttpServer(new HttpServerOptions().setUseProxyProtocol(true)).requestHandler(req -> {
req.response().end("hello");
}).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
HttpClient client = vertx.createHttpClient();
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(req -> {
req.send(onSuccess(resp -> {
resp.body(onSuccess(body -> {
assertEquals("hello", body.toString());
testComplete();
}));
}));
}));
}));
await();
} finally {
vertx.close();
}
}
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 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_PORT 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();
}
}
Aggregations