use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ServerTest method testBodyEndHandler.
@Test
public void testBodyEndHandler() throws Exception {
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.setChunked(true);
AtomicInteger count = new AtomicInteger();
resp.bodyEndHandler(v -> {
assertEquals(0, count.getAndIncrement());
assertTrue(resp.ended());
});
resp.write("something");
assertEquals(0, count.get());
resp.end();
assertEquals(1, count.get());
testComplete();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ServerTest method testHeadersEndHandler.
@Test
public void testHeadersEndHandler() throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
resp.setChunked(true);
resp.putHeader("some", "some-header");
resp.headersEndHandler(v -> {
assertOnIOContext(ctx);
assertFalse(resp.headWritten());
resp.putHeader("extra", "extra-header");
});
resp.write("something");
assertTrue(resp.headWritten());
resp.end();
});
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 onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals("some-header", headers.get("some").toString());
assertEquals("extra-header", headers.get("extra").toString());
testComplete();
});
}
});
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ClientTest method testHeaders.
@Test
public void testHeaders() throws Exception {
AtomicInteger reqCount = new AtomicInteger();
server.requestHandler(req -> {
assertEquals("https", req.scheme());
assertEquals(HttpMethod.GET, req.method());
assertEquals("/somepath", req.path());
assertEquals(DEFAULT_HTTPS_HOST_AND_PORT, req.host());
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));
reqCount.incrementAndGet();
HttpServerResponse resp = req.response();
resp.putHeader("content-type", "text/plain");
resp.putHeader("Foo_response", "foo_value");
resp.putHeader("bar_response", "bar_value");
resp.putHeader("juu_response", (List<String>) Arrays.asList("juu_value_1", "juu_value_2"));
resp.end();
});
startServer();
HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath");
req.handler(resp -> {
Context ctx = vertx.getOrCreateContext();
assertOnIOContext(ctx);
assertEquals(3, req.streamId());
assertEquals(1, reqCount.get());
assertEquals(HttpVersion.HTTP_2, resp.version());
assertEquals(200, resp.statusCode());
assertEquals("OK", resp.statusMessage());
assertEquals("text/plain", resp.getHeader("content-type"));
assertEquals("200", resp.getHeader(":status"));
assertEquals("foo_value", resp.getHeader("foo_response"));
assertEquals("bar_value", resp.getHeader("bar_response"));
assertEquals(2, resp.headers().getAll("juu_response").size());
assertEquals("juu_value_1", resp.headers().getAll("juu_response").get(0));
assertEquals("juu_value_2", resp.headers().getAll("juu_response").get(1));
resp.endHandler(v -> {
assertOnIOContext(ctx);
testComplete();
});
}).putHeader("Foo_request", "foo_request_value").putHeader("bar_request", "bar_request_value").putHeader("juu_request", Arrays.<CharSequence>asList("juu_request_value_1", "juu_request_value_2")).exceptionHandler(err -> fail()).end();
await();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ClientTest method test100Continue.
@Test
public void test100Continue() throws Exception {
AtomicInteger status = new AtomicInteger();
server.close();
server = vertx.createHttpServer(serverOptions.setHandle100ContinueAutomatically(true));
server.requestHandler(req -> {
assertEquals(0, status.getAndIncrement());
HttpServerResponse resp = req.response();
req.bodyHandler(body -> {
assertEquals(2, status.getAndIncrement());
assertEquals("request-body", body.toString());
resp.putHeader("wibble", "wibble-value").end("response-body");
});
});
startServer();
HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
assertEquals(3, status.getAndIncrement());
resp.bodyHandler(body -> {
assertEquals(4, status.getAndIncrement());
assertEquals("response-body", body.toString());
testComplete();
});
});
req.putHeader("expect", "100-continue");
req.continueHandler(v -> {
Context ctx = Vertx.currentContext();
assertOnIOContext(ctx);
assertEquals(1, status.getAndIncrement());
req.end(Buffer.buffer("request-body"));
});
req.sendHead(version -> {
assertEquals(3, req.streamId());
});
await();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ServerTest method testPushPromise.
private void testPushPromise(Http2Headers requestHeaders, BiConsumer<HttpServerResponse, Handler<AsyncResult<HttpServerResponse>>> pusher, Consumer<Http2Headers> headerChecker) throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
Handler<AsyncResult<HttpServerResponse>> handler = ar -> {
assertSame(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
assertIllegalStateException(() -> response.push(HttpMethod.GET, "/wibble2", resp -> {
}));
};
pusher.accept(req.response(), handler);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, requestHeaders, 0, true, request.context.newPromise());
Map<Integer, Http2Headers> pushed = new HashMap<>();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushed.put(promisedStreamId, headers);
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
String content = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(Collections.singleton(streamId), pushed.keySet());
assertEquals("the_content", content);
Http2Headers pushedHeaders = pushed.get(streamId);
headerChecker.accept(pushedHeaders);
testComplete();
});
return delta;
}
});
});
fut.sync();
await();
}
Aggregations