use of io.vertx.core.buffer.Buffer 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.buffer.Buffer 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.buffer.Buffer in project vert.x by eclipse.
the class Http2ClientTest method testResponseBody.
private void testResponseBody(String expected) throws Exception {
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.end(expected);
});
startServer();
client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
AtomicInteger count = new AtomicInteger();
Buffer content = Buffer.buffer();
resp.handler(buff -> {
content.appendBuffer(buff);
count.incrementAndGet();
});
resp.endHandler(v -> {
assertTrue(count.get() > 0);
assertEquals(expected, content.toString());
testComplete();
});
}).exceptionHandler(err -> fail()).end();
await();
}
use of io.vertx.core.buffer.Buffer in project vert.x by eclipse.
the class Http2ClientTest method testPost.
private void testPost(String expected) throws Exception {
Buffer content = Buffer.buffer();
AtomicInteger count = new AtomicInteger();
server.requestHandler(req -> {
assertEquals(HttpMethod.POST, req.method());
req.handler(buff -> {
content.appendBuffer(buff);
count.getAndIncrement();
});
req.endHandler(v -> {
assertTrue(count.get() > 0);
req.response().end();
});
});
startServer();
client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
resp.endHandler(v -> {
assertEquals(expected, content.toString());
testComplete();
});
}).exceptionHandler(err -> {
fail();
}).end(expected);
await();
}
use of io.vertx.core.buffer.Buffer 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();
}
Aggregations