use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class Http2ServerTest method testResponseCompressionEnabled.
@Test
public void testResponseCompressionEnabled() throws Exception {
waitFor(2);
String expected = TestUtils.randomAlphaString(1000);
server.close();
server = vertx.createHttpServer(serverOptions.setCompressionSupported(true));
server.requestHandler(req -> {
req.response().end(expected);
});
startServer();
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("gzip", headers.get(HttpHeaderNames.CONTENT_ENCODING).toString());
complete();
});
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
byte[] bytes = new byte[data.readableBytes()];
data.readBytes(bytes);
vertx.runOnContext(v -> {
String decoded;
try {
GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (true) {
int i = in.read();
if (i == -1) {
break;
}
baos.write(i);
;
}
decoded = baos.toString();
} catch (IOException e) {
fail(e);
return;
}
assertEquals(expected, decoded);
complete();
});
return super.onDataRead(ctx, streamId, data, padding, endOfStream);
}
});
int id = request.nextStreamId();
request.encoder.writeHeaders(request.context, id, GET("/").add("accept-encoding", "gzip"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class Http2ServerTest method testNetSocketSendFile.
private void testNetSocketSendFile(Buffer expected, String path, long offset, long length) throws Exception {
server.requestHandler(req -> {
NetSocket socket = req.netSocket();
socket.sendFile(path, offset, length, ar -> {
assertTrue(ar.succeeded());
socket.end();
});
});
startServer();
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("200", headers.status().toString());
assertFalse(endStream);
});
}
Buffer received = Buffer.buffer();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
received.appendBuffer(Buffer.buffer(data.copy()));
if (endOfStream) {
vertx.runOnContext(v -> {
assertEquals(received, expected);
testComplete();
});
}
return data.readableBytes() + padding;
}
});
request.encoder.writeHeaders(request.context, id, GET("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf in project vert.x by eclipse.
the class Http2ServerTest method testServerCloseNetSocket.
@Test
public void testServerCloseNetSocket() throws Exception {
waitFor(2);
AtomicInteger status = new AtomicInteger();
server.requestHandler(req -> {
NetSocket socket = req.netSocket();
socket.handler(buff -> {
switch(status.getAndIncrement()) {
case 0:
assertEquals(Buffer.buffer("some-data"), buff);
socket.write(buff);
socket.close();
break;
case 1:
assertEquals(Buffer.buffer("last-data"), buff);
break;
default:
fail();
break;
}
});
socket.endHandler(v -> {
assertEquals(2, status.getAndIncrement());
});
socket.closeHandler(v -> {
assertEquals(3, status.getAndIncrement());
complete();
});
});
startServer();
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
request.decoder.frameListener(new Http2EventAdapter() {
int count = 0;
@Override
public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
int c = count++;
vertx.runOnContext(v -> {
assertEquals(0, c);
});
request.encoder.writeData(request.context, id, Buffer.buffer("some-data").getByteBuf(), 0, false, request.context.newPromise());
request.context.flush();
}
StringBuilder received = new StringBuilder();
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
String s = data.toString(StandardCharsets.UTF_8);
received.append(s);
if (endOfStream) {
request.encoder.writeData(request.context, id, Buffer.buffer("last-data").getByteBuf(), 0, true, request.context.newPromise());
vertx.runOnContext(v -> {
assertEquals("some-data", received.toString());
complete();
});
}
return data.readableBytes() + padding;
}
});
request.encoder.writeHeaders(request.context, id, GET("/"), 0, false, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf 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 org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf 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