use of io.netty.handler.codec.http2.Http2FrameAdapter in project vert.x by eclipse.
the class Http2ServerTest method testServerInitialSettings.
@Test
public void testServerInitialSettings() throws Exception {
io.vertx.core.http.Http2Settings settings = TestUtils.randomHttp2Settings();
server.close();
server = vertx.createHttpServer(serverOptions.setInitialSettings(settings));
server.requestHandler(req -> fail());
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings newSettings) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals((Long) settings.getHeaderTableSize(), newSettings.headerTableSize());
assertEquals((Long) settings.getMaxConcurrentStreams(), newSettings.maxConcurrentStreams());
assertEquals((Integer) settings.getInitialWindowSize(), newSettings.initialWindowSize());
assertEquals((Integer) settings.getMaxFrameSize(), newSettings.maxFrameSize());
assertEquals((Long) settings.getMaxHeaderListSize(), newSettings.maxHeaderListSize());
assertEquals(settings.get(''), newSettings.get(''));
testComplete();
});
}
});
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.Http2FrameAdapter 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();
}
use of io.netty.handler.codec.http2.Http2FrameAdapter in project vert.x by eclipse.
the class Http2ServerTest method testStreamPriority.
@Test
public void testStreamPriority() throws Exception {
StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45).setExclusive(true);
StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75).setExclusive(false);
waitFor(4);
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
assertEquals(requestStreamPriority, req.streamPriority());
resp.setStatusCode(200);
resp.setStreamPriority(responseStreamPriority);
resp.end("data");
complete();
});
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("/"), requestStreamPriority.getDependency(), requestStreamPriority.getWeight(), requestStreamPriority.isExclusive(), 0, true, request.context.newPromise());
request.context.flush();
request.decoder.frameListener(new Http2FrameAdapter() {
@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(responseStreamPriority.getDependency(), streamDependency);
assertEquals(responseStreamPriority.getWeight(), weight);
assertEquals(responseStreamPriority.isExclusive(), exclusive);
complete();
});
}
@Override
public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception {
vertx.runOnContext(v -> {
assertEquals(id, streamId);
assertEquals(responseStreamPriority.getDependency(), streamDependency);
assertEquals(responseStreamPriority.getWeight(), weight);
assertEquals(responseStreamPriority.isExclusive(), exclusive);
complete();
});
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
if (endOfStream) {
vertx.runOnContext(v -> {
complete();
});
}
return super.onDataRead(ctx, streamId, data, padding, endOfStream);
}
});
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.Http2FrameAdapter in project vert.x by eclipse.
the class Http2ServerTest method testServerSettings.
@Test
public void testServerSettings() throws Exception {
waitFor(2);
io.vertx.core.http.Http2Settings expectedSettings = TestUtils.randomHttp2Settings();
expectedSettings.setHeaderTableSize((int) io.vertx.core.http.Http2Settings.DEFAULT_HEADER_TABLE_SIZE);
Context otherContext = vertx.getOrCreateContext();
server.connectionHandler(conn -> {
Context ctx = Vertx.currentContext();
otherContext.runOnContext(v -> {
conn.updateSettings(expectedSettings, ar -> {
assertSame(ctx, Vertx.currentContext());
io.vertx.core.http.Http2Settings ackedSettings = conn.settings();
assertEquals(expectedSettings.getMaxHeaderListSize(), ackedSettings.getMaxHeaderListSize());
assertEquals(expectedSettings.getMaxFrameSize(), ackedSettings.getMaxFrameSize());
assertEquals(expectedSettings.getInitialWindowSize(), ackedSettings.getInitialWindowSize());
assertEquals(expectedSettings.getMaxConcurrentStreams(), ackedSettings.getMaxConcurrentStreams());
assertEquals(expectedSettings.getHeaderTableSize(), ackedSettings.getHeaderTableSize());
assertEquals(expectedSettings.get('\u0007'), ackedSettings.get(7));
complete();
});
});
});
server.requestHandler(req -> {
fail();
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
request.decoder.frameListener(new Http2FrameAdapter() {
AtomicInteger count = new AtomicInteger();
Context context = vertx.getOrCreateContext();
@Override
public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings newSettings) throws Http2Exception {
context.runOnContext(v -> {
switch(count.getAndIncrement()) {
case 0:
// Initial settings
break;
case 1:
// Server sent settings
assertEquals((Long) expectedSettings.getMaxHeaderListSize(), newSettings.maxHeaderListSize());
assertEquals((Integer) expectedSettings.getMaxFrameSize(), newSettings.maxFrameSize());
assertEquals((Integer) expectedSettings.getInitialWindowSize(), newSettings.initialWindowSize());
assertEquals((Long) expectedSettings.getMaxConcurrentStreams(), newSettings.maxConcurrentStreams());
assertEquals(null, newSettings.headerTableSize());
complete();
break;
default:
fail();
}
});
}
});
});
fut.sync();
await();
}
use of io.netty.handler.codec.http2.Http2FrameAdapter in project vert.x by eclipse.
the class Http2ServerTest method testStreamPriorityChange.
@Test
public void testStreamPriorityChange() throws Exception {
StreamPriority requestStreamPriority = new StreamPriority().setDependency(123).setWeight((short) 45).setExclusive(true);
StreamPriority requestStreamPriority2 = new StreamPriority().setDependency(223).setWeight((short) 145).setExclusive(false);
StreamPriority responseStreamPriority = new StreamPriority().setDependency(153).setWeight((short) 75).setExclusive(false);
StreamPriority responseStreamPriority2 = new StreamPriority().setDependency(253).setWeight((short) 175).setExclusive(true);
waitFor(6);
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
assertEquals(requestStreamPriority, req.streamPriority());
req.bodyHandler(b -> {
assertEquals(requestStreamPriority2, req.streamPriority());
resp.setStatusCode(200);
resp.setStreamPriority(responseStreamPriority);
resp.write("hello");
resp.setStreamPriority(responseStreamPriority2);
resp.end("world");
complete();
});
req.streamPriorityHandler(streamPriority -> {
assertEquals(requestStreamPriority2, streamPriority);
assertEquals(requestStreamPriority2, req.streamPriority());
complete();
});
});
startServer();
TestClient client = new TestClient();
Context context = vertx.getOrCreateContext();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/"), requestStreamPriority.getDependency(), requestStreamPriority.getWeight(), requestStreamPriority.isExclusive(), 0, false, request.context.newPromise());
request.context.flush();
request.encoder.writePriority(request.context, id, requestStreamPriority2.getDependency(), requestStreamPriority2.getWeight(), requestStreamPriority2.isExclusive(), request.context.newPromise());
request.context.flush();
request.encoder.writeData(request.context, id, Buffer.buffer("hello").getByteBuf(), 0, true, request.context.newPromise());
request.context.flush();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
super.onHeadersRead(ctx, streamId, headers, streamDependency, weight, exclusive, padding, endStream);
context.runOnContext(v -> {
assertEquals(id, streamId);
assertEquals(responseStreamPriority.getDependency(), streamDependency);
assertEquals(responseStreamPriority.getWeight(), weight);
assertEquals(responseStreamPriority.isExclusive(), exclusive);
complete();
});
}
int cnt;
@Override
public void onPriorityRead(ChannelHandlerContext ctx, int streamId, int streamDependency, short weight, boolean exclusive) throws Http2Exception {
context.runOnContext(v -> {
assertEquals(id, streamId);
switch(cnt++) {
case 0:
// HERE
assertEquals(responseStreamPriority.getDependency(), streamDependency);
assertEquals(responseStreamPriority.getWeight(), weight);
assertEquals(responseStreamPriority.isExclusive(), exclusive);
complete();
break;
case 1:
assertEquals(responseStreamPriority2.getDependency(), streamDependency);
assertEquals(responseStreamPriority2.getWeight(), weight);
assertEquals(responseStreamPriority2.isExclusive(), exclusive);
complete();
break;
default:
fail();
break;
}
});
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
if (endOfStream) {
context.runOnContext(v -> {
complete();
});
}
return super.onDataRead(ctx, streamId, data, padding, endOfStream);
}
});
});
fut.sync();
await();
}
Aggregations