use of io.vertx.core.Context in project vert.x by eclipse.
the class Http2ServerTest method testConnectionHandler.
@Test
public void testConnectionHandler() throws Exception {
waitFor(2);
Context ctx = vertx.getOrCreateContext();
server.close();
server.connectionHandler(conn -> {
assertOnIOContext(ctx);
complete();
});
server.requestHandler(req -> fail());
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
vertx.runOnContext(v -> {
complete();
});
});
fut.sync();
await();
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class HttpTest method testInVerticle.
private void testInVerticle(boolean worker) throws Exception {
client.close();
server.close();
class MyVerticle extends AbstractVerticle {
Context ctx;
@Override
public void start() {
ctx = Vertx.currentContext();
if (worker) {
assertTrue(ctx instanceof WorkerContext);
} else {
assertTrue(ctx instanceof EventLoopContext);
}
Thread thr = Thread.currentThread();
server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
server.requestHandler(req -> {
req.response().end();
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
});
server.listen(ar -> {
assertTrue(ar.succeeded());
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
client = vertx.createHttpClient(new HttpClientOptions());
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> {
assertSame(ctx, Vertx.currentContext());
if (!worker) {
assertSame(thr, Thread.currentThread());
}
assertEquals(200, res.statusCode());
testComplete();
}).end();
});
}
}
MyVerticle verticle = new MyVerticle();
vertx.deployVerticle(verticle, new DeploymentOptions().setWorker(worker));
await();
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class Http2ServerTest method testClientSendGoAwayInternalError.
@Test
public void testClientSendGoAwayInternalError() throws Exception {
Future<Void> abc = Future.future();
Context ctx = vertx.getOrCreateContext();
Handler<HttpServerRequest> requestHandler = req -> {
HttpConnection conn = req.connection();
AtomicInteger status = new AtomicInteger();
conn.goAwayHandler(ga -> {
assertOnIOContext(ctx);
assertEquals(0, status.getAndIncrement());
req.response().end();
});
conn.shutdownHandler(v -> {
assertOnIOContext(ctx);
assertEquals(1, status.getAndIncrement());
});
conn.closeHandler(v -> {
assertEquals(2, status.getAndIncrement());
testComplete();
});
abc.complete();
};
server.requestHandler(requestHandler);
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
Http2ConnectionEncoder encoder = request.encoder;
int id = request.nextStreamId();
encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
abc.setHandler(ar -> {
encoder.writeGoAway(request.context, id, 3, Unpooled.EMPTY_BUFFER, request.context.newPromise());
request.context.flush();
});
});
fut.sync();
await();
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class Http2ServerTest method testGet.
@Test
public void testGet() throws Exception {
String expected = TestUtils.randomAlphaString(1000);
AtomicBoolean requestEnded = new AtomicBoolean();
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
assertOnIOContext(ctx);
req.endHandler(v -> {
assertOnIOContext(ctx);
requestEnded.set(true);
});
HttpServerResponse resp = req.response();
assertEquals(HttpMethod.GET, req.method());
assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host());
assertEquals("/", req.path());
assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.getHeader(":authority"));
assertTrue(req.isSSL());
assertEquals("https", req.getHeader(":scheme"));
assertEquals("/", req.getHeader(":path"));
assertEquals("GET", req.getHeader(":method"));
assertEquals("foo_request_value", req.getHeader("Foo_request"));
assertEquals("bar_request_value", req.getHeader("bar_request"));
assertEquals(2, req.headers().getAll("juu_request").size());
assertEquals("juu_request_value_1", req.headers().getAll("juu_request").get(0));
assertEquals("juu_request_value_2", req.headers().getAll("juu_request").get(1));
assertEquals(Collections.singletonList("cookie_1; cookie_2; cookie_3"), req.headers().getAll("cookie"));
resp.putHeader("content-type", "text/plain");
resp.putHeader("Foo_response", "foo_response_value");
resp.putHeader("bar_response", "bar_response_value");
resp.putHeader("juu_response", (List<String>) Arrays.asList("juu_response_value_1", "juu_response_value_2"));
resp.end(expected);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals(id, streamId);
assertEquals("200", headers.status().toString());
assertEquals("text/plain", headers.get("content-type").toString());
assertEquals("foo_response_value", headers.get("foo_response").toString());
assertEquals("bar_response_value", headers.get("bar_response").toString());
assertEquals(2, headers.getAll("juu_response").size());
assertEquals("juu_response_value_1", headers.getAll("juu_response").get(0).toString());
assertEquals("juu_response_value_2", headers.getAll("juu_response").get(1).toString());
assertFalse(endStream);
});
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
String actual = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(id, streamId);
assertEquals(expected, actual);
assertTrue(endOfStream);
testComplete();
});
return super.onDataRead(ctx, streamId, data, padding, endOfStream);
}
});
Http2Headers headers = GET("/").authority(DEFAULT_HTTPS_HOST_AND_PORT);
headers.set("foo_request", "foo_request_value");
headers.set("bar_request", "bar_request_value");
headers.set("juu_request", "juu_request_value_1", "juu_request_value_2");
headers.set("cookie", Arrays.asList("cookie_1", "cookie_2", "cookie_3"));
request.encoder.writeHeaders(request.context, id, headers, 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.Context in project vert.x by eclipse.
the class Http2ServerTest method testSendPing.
@Test
public void testSendPing() throws Exception {
waitFor(2);
Buffer expected = TestUtils.randomBuffer(8);
Context ctx = vertx.getOrCreateContext();
server.close();
server.connectionHandler(conn -> {
conn.ping(expected, ar -> {
assertSame(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
assertEquals(expected, ar.result());
complete();
});
});
server.requestHandler(req -> fail());
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2EventAdapter() {
@Override
public void onPingRead(ChannelHandlerContext ctx, ByteBuf data) throws Http2Exception {
Buffer buffer = Buffer.buffer(data.copy());
vertx.runOnContext(v -> {
assertEquals(expected, buffer);
complete();
});
}
});
});
fut.sync();
await();
}
Aggregations