use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class Http2ClientTest method testGet.
@Test
public void testGet() throws Exception {
ServerBootstrap bootstrap = createH2Server((decoder, encoder) -> 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 -> {
assertTrue(endStream);
encoder.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, true, ctx.newPromise());
ctx.flush();
});
}
@Override
public void onGoAwayRead(ChannelHandlerContext ctx, int lastStreamId, long errorCode, ByteBuf debugData) throws Http2Exception {
vertx.runOnContext(v -> {
testComplete();
});
}
});
ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
try {
HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath");
req.handler(resp -> {
Context ctx = vertx.getOrCreateContext();
assertOnIOContext(ctx);
resp.endHandler(v -> {
assertOnIOContext(ctx);
req.connection().close();
});
}).end();
await();
} finally {
s.channel().close().sync();
}
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class Http2ClientTest method testUnknownFrame.
@Test
public void testUnknownFrame() throws Exception {
Buffer expectedSend = TestUtils.randomBuffer(500);
Buffer expectedRecv = TestUtils.randomBuffer(500);
server.requestHandler(req -> {
req.customFrameHandler(frame -> {
assertEquals(10, frame.type());
assertEquals(253, frame.flags());
assertEquals(expectedSend, frame.payload());
req.response().writeCustomFrame(12, 134, expectedRecv).end();
});
});
startServer();
AtomicInteger status = new AtomicInteger();
HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
Context ctx = Vertx.currentContext();
assertEquals(0, status.getAndIncrement());
resp.customFrameHandler(frame -> {
assertOnIOContext(ctx);
assertEquals(1, status.getAndIncrement());
assertEquals(12, frame.type());
assertEquals(134, frame.flags());
assertEquals(expectedRecv, frame.payload());
});
resp.endHandler(v -> {
assertEquals(2, status.getAndIncrement());
testComplete();
});
});
req.sendHead(version -> {
assertSame(HttpVersion.HTTP_2, version);
req.writeCustomFrame(10, 253, expectedSend);
req.end();
});
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class Http2ClientTest method testPushPromise.
@Test
public void testPushPromise() throws Exception {
waitFor(2);
server.requestHandler(req -> {
req.response().push(HttpMethod.GET, "/wibble?a=b", ar -> {
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
}).end();
});
startServer();
AtomicReference<Context> ctx = new AtomicReference<>();
HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
Context current = Vertx.currentContext();
if (ctx.get() == null) {
ctx.set(current);
} else {
assertEquals(ctx.get(), current);
}
resp.endHandler(v -> {
complete();
});
});
req.pushHandler(pushedReq -> {
Context current = Vertx.currentContext();
if (ctx.get() == null) {
ctx.set(current);
} else {
assertEquals(ctx.get(), current);
}
assertOnIOContext(current);
assertEquals(HttpMethod.GET, pushedReq.method());
assertEquals("/wibble?a=b", pushedReq.uri());
assertEquals("/wibble", pushedReq.path());
assertEquals("a=b", pushedReq.query());
pushedReq.handler(resp -> {
assertEquals(200, resp.statusCode());
Buffer content = Buffer.buffer();
resp.handler(content::appendBuffer);
resp.endHandler(v -> {
complete();
});
});
});
req.end();
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class Http2ClientTest method testClientRequestWriteability.
@Test
public void testClientRequestWriteability() throws Exception {
Buffer content = Buffer.buffer();
Buffer expected = Buffer.buffer();
String chunk = TestUtils.randomAlphaString(100);
CompletableFuture<Void> done = new CompletableFuture<>();
AtomicBoolean paused = new AtomicBoolean();
AtomicInteger numPause = new AtomicInteger();
server.requestHandler(req -> {
Context ctx = vertx.getOrCreateContext();
done.thenAccept(v1 -> {
paused.set(false);
ctx.runOnContext(v2 -> {
req.resume();
});
});
numPause.incrementAndGet();
req.pause();
paused.set(true);
req.handler(content::appendBuffer);
req.endHandler(v -> {
assertEquals(expected, content);
req.response().end();
});
});
startServer();
HttpClientRequest req = client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
testComplete();
}).setChunked(true).exceptionHandler(err -> {
fail();
});
AtomicInteger sent = new AtomicInteger();
AtomicInteger count = new AtomicInteger();
AtomicInteger drained = new AtomicInteger();
vertx.setPeriodic(1, timerID -> {
Context ctx = vertx.getOrCreateContext();
if (req.writeQueueFull()) {
assertTrue(paused.get());
assertEquals(1, numPause.get());
req.drainHandler(v -> {
assertOnIOContext(ctx);
assertEquals(0, drained.getAndIncrement());
assertEquals(1, numPause.get());
assertFalse(paused.get());
req.end();
});
vertx.cancelTimer(timerID);
done.complete(null);
} else {
count.incrementAndGet();
expected.appendString(chunk);
req.write(chunk);
sent.addAndGet(chunk.length());
}
});
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class Http2ClientTest method testResetPendingPushPromise.
@Test
public void testResetPendingPushPromise() throws Exception {
server.requestHandler(req -> {
req.response().push(HttpMethod.GET, "/wibble", ar -> {
assertFalse(ar.succeeded());
testComplete();
});
});
startServer();
client.close();
client = vertx.createHttpClient(clientOptions.setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(0L)));
HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
fail();
});
req.pushHandler(pushedReq -> {
pushedReq.reset(Http2Error.CANCEL.code());
});
req.end();
await();
}
Aggregations