use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class Http2ServerTest method testPushPromiseClearText.
@Test
public void testPushPromiseClearText() throws Exception {
waitFor(2);
server.close();
server = vertx.createHttpServer(serverOptions.setHost(DEFAULT_HTTP_HOST).setPort(DEFAULT_HTTP_PORT).setUseAlpn(false).setSsl(false));
server.requestHandler(req -> {
req.response().push(HttpMethod.GET, "/resource", ar -> {
assertTrue(ar.succeeded());
ar.result().end("the-pushed-response");
});
req.response().end();
});
startServer();
client = vertx.createHttpClient(clientOptions.setUseAlpn(false).setSsl(false));
HttpClientRequest req = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath");
req.handler(resp -> {
assertEquals(HttpVersion.HTTP_2, resp.version());
complete();
}).exceptionHandler(this::fail).pushHandler(pushedReq -> {
assertEquals("http", pushedReq.headers().get(":scheme"));
pushedReq.handler(pushResp -> {
pushResp.bodyHandler(buff -> {
assertEquals("the-pushed-response", buff.toString());
complete();
});
});
}).end();
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testConnectionErrorsGetReportedToRequest.
@Test
public void testConnectionErrorsGetReportedToRequest() throws InterruptedException {
AtomicInteger req1Exceptions = new AtomicInteger();
AtomicInteger req2Exceptions = new AtomicInteger();
AtomicInteger req3Exceptions = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(3);
// This one should cause an error in the Client Exception handler, because it has no exception handler set specifically.
HttpClientRequest req1 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl1", resp -> {
fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
});
req1.exceptionHandler(t -> {
assertEquals("More than one call to req1 exception handler was not expected", 1, req1Exceptions.incrementAndGet());
latch.countDown();
});
HttpClientRequest req2 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", resp -> {
fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
});
req2.exceptionHandler(t -> {
assertEquals("More than one call to req2 exception handler was not expected", 1, req2Exceptions.incrementAndGet());
latch.countDown();
});
HttpClientRequest req3 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", resp -> {
fail("Should never get a response on a bad port, if you see this message than you are running an http server on port 9998");
});
req3.exceptionHandler(t -> {
assertEquals("More than one call to req2 exception handler was not expected", 1, req3Exceptions.incrementAndGet());
latch.countDown();
});
req1.end();
req2.end();
req3.end();
awaitLatch(latch);
testComplete();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testRequestBodyStringAtEnd.
private void testRequestBodyStringAtEnd(String encoding) {
String body = TestUtils.randomUnicodeString(1000);
Buffer bodyBuff;
if (encoding == null) {
bodyBuff = Buffer.buffer(body);
} else {
bodyBuff = Buffer.buffer(body, encoding);
}
server.requestHandler(req -> {
req.bodyHandler(buffer -> {
assertEquals(bodyBuff, buffer);
testComplete();
});
});
server.listen(onSuccess(server -> {
HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler());
if (encoding == null) {
req.end(body);
} else {
req.end(body, encoding);
}
}));
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method testClearHandlersOnEnd.
@Test
public void testClearHandlersOnEnd() {
String path = "/some/path";
server = vertx.createHttpServer(createBaseServerOptions());
server.requestHandler(req -> req.response().setStatusCode(200).end());
server.listen(ar -> {
assertTrue(ar.succeeded());
HttpClientRequest req = client.request(HttpMethod.GET, HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path);
AtomicInteger count = new AtomicInteger();
req.handler(resp -> {
resp.endHandler(v -> {
try {
resp.endHandler(null);
resp.exceptionHandler(null);
resp.handler(null);
} catch (Exception e) {
fail("Was expecting to set to null the handlers when the response is completed");
return;
}
if (count.incrementAndGet() == 2) {
testComplete();
}
});
});
req.endHandler(done -> {
try {
req.handler(null);
req.exceptionHandler(null);
req.endHandler(null);
} catch (Exception e) {
e.printStackTrace();
fail("Was expecting to set to null the handlers when the response is completed");
return;
}
if (count.incrementAndGet() == 2) {
testComplete();
}
});
req.end();
});
await();
}
use of io.vertx.core.http.HttpClientRequest in project vert.x by eclipse.
the class HttpTest method test100ContinueHandledAutomatically.
@Test
public void test100ContinueHandledAutomatically() throws Exception {
Buffer toSend = TestUtils.randomBuffer(1000);
server.requestHandler(req -> {
req.bodyHandler(data -> {
assertEquals(toSend, data);
req.response().end();
});
});
server.listen(onSuccess(s -> {
HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> {
resp.endHandler(v -> testComplete());
});
req.headers().set("Expect", "100-continue");
req.setChunked(true);
req.continueHandler(v -> {
req.write(toSend);
req.end();
});
req.sendHead();
}));
await();
}
Aggregations