use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class HttpTest method testDeliverPausedBufferWhenResume.
@Test
public void testDeliverPausedBufferWhenResume() throws Exception {
Buffer data = TestUtils.randomBuffer(20);
int num = 10;
waitFor(num);
List<CompletableFuture<Void>> resumes = Collections.synchronizedList(new ArrayList<>());
for (int i = 0; i < num; i++) {
resumes.add(new CompletableFuture<>());
}
server.requestHandler(req -> {
int idx = Integer.parseInt(req.path().substring(1));
HttpServerResponse resp = req.response();
resumes.get(idx).thenAccept(v -> {
resp.end();
});
resp.setChunked(true).write(data);
});
startServer();
client.close();
client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(1).setKeepAlive(true));
for (int i = 0; i < num; i++) {
int idx = i;
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/" + i, resp -> {
Buffer body = Buffer.buffer();
resp.handler(buff -> {
resumes.get(idx).complete(null);
body.appendBuffer(buff);
});
resp.endHandler(v -> {
assertEquals(data, body);
complete();
});
resp.pause();
vertx.setTimer(10, id -> {
resp.resume();
});
}).end();
}
await();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ServerTest method test100ContinueHandledAutomatically.
@Test
public void test100ContinueHandledAutomatically() throws Exception {
server.close();
server = vertx.createHttpServer(serverOptions.setHandle100ContinueAutomatically(true));
server.requestHandler(req -> {
HttpServerResponse resp = req.response();
req.bodyHandler(body -> {
assertEquals("the-body", body.toString());
resp.putHeader("wibble", "wibble-value").end();
});
});
test100Continue();
}
use of io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class HttpTest method testUseResponseAfterComplete.
@Test
public void testUseResponseAfterComplete() {
server.requestHandler(req -> {
Buffer buff = Buffer.buffer();
HttpServerResponse resp = req.response();
assertFalse(resp.ended());
resp.end();
assertTrue(resp.ended());
assertIllegalStateException(() -> resp.drainHandler(noOpHandler()));
assertIllegalStateException(() -> resp.end());
assertIllegalStateException(() -> resp.end("foo"));
assertIllegalStateException(() -> resp.end(buff));
assertIllegalStateException(() -> resp.end("foo", "UTF-8"));
assertIllegalStateException(() -> resp.exceptionHandler(noOpHandler()));
assertIllegalStateException(() -> resp.setChunked(false));
assertIllegalStateException(() -> resp.setWriteQueueMaxSize(123));
assertIllegalStateException(() -> resp.write(buff));
assertIllegalStateException(() -> resp.write("foo"));
assertIllegalStateException(() -> resp.write("foo", "UTF-8"));
assertIllegalStateException(() -> resp.write(buff));
assertIllegalStateException(() -> resp.writeQueueFull());
assertIllegalStateException(() -> resp.sendFile("asokdasokd"));
testComplete();
});
server.listen(onSuccess(s -> {
client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()).end();
}));
await();
}
use of io.vertx.core.http.HttpServerResponse 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 io.vertx.core.http.HttpServerResponse in project vert.x by eclipse.
the class Http2ServerTest method testRequestResponseLifecycle.
@Test
public void testRequestResponseLifecycle() throws Exception {
waitFor(2);
server.requestHandler(req -> {
req.endHandler(v -> {
assertIllegalStateException(() -> req.setExpectMultipart(false));
assertIllegalStateException(() -> req.handler(buf -> {
}));
assertIllegalStateException(() -> req.uploadHandler(upload -> {
}));
assertIllegalStateException(() -> req.endHandler(v2 -> {
}));
complete();
});
HttpServerResponse resp = req.response();
resp.setChunked(true).write(Buffer.buffer("whatever"));
assertTrue(resp.headWritten());
assertIllegalStateException(() -> resp.setChunked(false));
assertIllegalStateException(() -> resp.setStatusCode(100));
assertIllegalStateException(() -> resp.setStatusMessage("whatever"));
assertIllegalStateException(() -> resp.putHeader("a", "b"));
assertIllegalStateException(() -> resp.putHeader("a", (CharSequence) "b"));
assertIllegalStateException(() -> resp.putHeader("a", (Iterable<String>) Arrays.asList("a", "b")));
assertIllegalStateException(() -> resp.putHeader("a", (Arrays.<CharSequence>asList("a", "b"))));
assertIllegalStateException(resp::writeContinue);
resp.end();
assertIllegalStateException(() -> resp.write("a"));
assertIllegalStateException(() -> resp.write("a", "UTF-8"));
assertIllegalStateException(() -> resp.write(Buffer.buffer("a")));
assertIllegalStateException(resp::end);
assertIllegalStateException(() -> resp.end("a"));
assertIllegalStateException(() -> resp.end("a", "UTF-8"));
assertIllegalStateException(() -> resp.end(Buffer.buffer("a")));
assertIllegalStateException(() -> resp.sendFile("the-file.txt"));
assertIllegalStateException(() -> resp.reset(0));
assertIllegalStateException(() -> resp.closeHandler(v -> {
}));
assertIllegalStateException(() -> resp.endHandler(v -> {
}));
assertIllegalStateException(() -> resp.drainHandler(v -> {
}));
assertIllegalStateException(() -> resp.exceptionHandler(err -> {
}));
assertIllegalStateException(resp::writeQueueFull);
assertIllegalStateException(() -> resp.setWriteQueueMaxSize(100));
assertIllegalStateException(() -> resp.putTrailer("a", "b"));
assertIllegalStateException(() -> resp.putTrailer("a", (CharSequence) "b"));
assertIllegalStateException(() -> resp.putTrailer("a", (Iterable<String>) Arrays.asList("a", "b")));
assertIllegalStateException(() -> resp.putTrailer("a", (Arrays.<CharSequence>asList("a", "b"))));
assertIllegalStateException(() -> resp.push(HttpMethod.GET, "/whatever", ar -> {
}));
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("/"), 0, true, request.context.newPromise());
request.context.flush();
});
fut.sync();
await();
}
Aggregations