use of io.vertx.core.http.HttpClient in project vertx-web by vert-x3.
the class StaticHandlerTest method testHttp2Push.
@Test
public void testHttp2Push() throws Exception {
List<Http2PushMapping> mappings = new ArrayList<>();
mappings.add(new Http2PushMapping("style.css", "style", false));
mappings.add(new Http2PushMapping("coin.png", "image", false));
stat.setHttp2PushMapping(mappings).setWebRoot("webroot/somedir3");
router.route().handler(stat);
HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem")));
http2Server.requestHandler(router).listen(8443);
HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem"));
HttpClient client = vertx.createHttpClient(options);
HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> {
assertEquals(200, resp.statusCode());
assertEquals(HttpVersion.HTTP_2, resp.version());
resp.bodyHandler(this::assertNotNull);
});
CountDownLatch latch = new CountDownLatch(2);
request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> {
assertNotNull(pushedResp);
pushedResp.bodyHandler(this::assertNotNull);
latch.countDown();
}));
request.end();
latch.await();
}
use of io.vertx.core.http.HttpClient in project vertx-web by vert-x3.
the class ClusteredSessionHandlerTest method testClusteredSession.
@Test
public void testClusteredSession() throws Exception {
CountDownLatch serversReady = new CountDownLatch(3);
Router router1 = Router.router(vertices[0]);
router1.route().handler(CookieHandler.create());
SessionStore store1 = ClusteredSessionStore.create(vertices[0]);
router1.route().handler(SessionHandler.create(store1));
HttpServer server1 = vertices[0].createHttpServer(new HttpServerOptions().setPort(8081).setHost("localhost"));
server1.requestHandler(router1);
server1.listen(onSuccess(s -> serversReady.countDown()));
HttpClient client1 = vertices[0].createHttpClient(new HttpClientOptions());
Router router2 = Router.router(vertices[1]);
router2.route().handler(CookieHandler.create());
SessionStore store2 = ClusteredSessionStore.create(vertices[1]);
router2.route().handler(SessionHandler.create(store2));
HttpServer server2 = vertices[1].createHttpServer(new HttpServerOptions().setPort(8082).setHost("localhost"));
server2.requestHandler(router2);
server2.listen(onSuccess(s -> serversReady.countDown()));
HttpClient client2 = vertices[0].createHttpClient(new HttpClientOptions());
Router router3 = Router.router(vertices[2]);
router3.route().handler(CookieHandler.create());
SessionStore store3 = ClusteredSessionStore.create(vertices[2]);
router3.route().handler(SessionHandler.create(store3));
HttpServer server3 = vertices[2].createHttpServer(new HttpServerOptions().setPort(8083).setHost("localhost"));
server3.requestHandler(router3);
server3.listen(onSuccess(s -> serversReady.countDown()));
HttpClient client3 = vertices[0].createHttpClient(new HttpClientOptions());
awaitLatch(serversReady);
router1.route().handler(rc -> {
Session sess = rc.session();
sess.put("foo", "bar");
stuffSession(sess);
rc.response().end();
});
router2.route().handler(rc -> {
Session sess = rc.session();
checkSession(sess);
assertEquals("bar", sess.get("foo"));
sess.put("eek", "wibble");
rc.response().end();
});
router3.route().handler(rc -> {
Session sess = rc.session();
checkSession(sess);
assertEquals("bar", sess.get("foo"));
assertEquals("wibble", sess.get("eek"));
rc.response().end();
});
AtomicReference<String> rSetCookie = new AtomicReference<>();
testRequestBuffer(client1, HttpMethod.GET, 8081, "/", null, resp -> {
String setCookie = resp.headers().get("set-cookie");
rSetCookie.set(setCookie);
}, 200, "OK", null);
// FIXME - for now we do an artificial sleep because it's possible the session hasn't been stored properly before
// the next request hits the server
// https://github.com/vert-x3/vertx-web/issues/93
Thread.sleep(1000);
testRequestBuffer(client2, HttpMethod.GET, 8082, "/", req -> req.putHeader("cookie", rSetCookie.get()), null, 200, "OK", null);
Thread.sleep(1000);
testRequestBuffer(client3, HttpMethod.GET, 8083, "/", req -> req.putHeader("cookie", rSetCookie.get()), null, 200, "OK", null);
}
use of io.vertx.core.http.HttpClient in project vertx-openshift-it by cescoffier.
the class MapsIT method testClusterWideUpdate.
@Test
public void testClusterWideUpdate() throws Exception {
HttpClient httpClient = vertx.createHttpClient();
int loops = 30;
CountDownLatch latch = new CountDownLatch(loops);
for (int i = 0; i < loops; i++) {
String key = getKey(i);
String value = getValue(i);
URL url = Kube.urlForRoute(client.routes().withName(APPLICATION_NAME).get(), "/maps/" + testName.getMethodName() + "/" + key);
httpClient.putAbs(url.toString()).handler(resp -> latch.countDown()).exceptionHandler(t -> {
t.printStackTrace();
latch.countDown();
}).end(value);
}
latch.await(1, TimeUnit.MINUTES);
for (int i = 0; i < loops; i++) {
String key = getKey(i);
String value = getValue(i);
URL url = Kube.urlForRoute(client.routes().withName(APPLICATION_NAME).get(), "/maps/" + testName.getMethodName() + "/" + key);
get(url).then().assertThat().statusCode(200).body(equalTo(value));
}
}
use of io.vertx.core.http.HttpClient in project vertx-openshift-it by cescoffier.
the class WebSessionIT method testClusteredWebSession.
@Test
public void testClusteredWebSession() throws Exception {
HttpClient httpClient = vertx.createHttpClient();
AtomicReference<Map.Entry<String, String>> cookie = new AtomicReference<>();
int loops = 30;
for (int i = 0; i < loops; i++) {
String key = getKey(i);
String value = getValue(i);
URL url = Kube.urlForRoute(client.routes().withName(APPLICATION_NAME).get(), "/web-session/" + key);
CountDownLatch latch = new CountDownLatch(1);
HttpClientRequest request = httpClient.putAbs(url.toString());
Optional.ofNullable(cookie.get()).ifPresent(c -> request.headers().add("cookie", c.getKey() + "=" + c.getValue()));
request.handler(resp -> {
resp.cookies().stream().map(c -> c.split("=", 2)).map(split -> new SimpleImmutableEntry<>(split[0], split[1].split(";")[0])).filter(entry -> "vertx-web.session".equals(entry.getKey())).forEach(cookie::set);
latch.countDown();
}).exceptionHandler(t -> {
t.printStackTrace();
latch.countDown();
}).end(value);
latch.await(1, TimeUnit.MINUTES);
// Give some time to the replication operation
TimeUnit.SECONDS.sleep(1);
}
scaleTo(2);
// Give some time to the rebalancing process
TimeUnit.SECONDS.sleep(10);
assertNotNull("No session cookie", cookie.get());
for (int i = 0; i < loops; i++) {
String key = getKey(i);
String value = getValue(i);
URL url = Kube.urlForRoute(client.routes().withName(APPLICATION_NAME).get(), "/web-session/" + key);
given().cookie(cookie.get().getKey(), cookie.get().getValue()).when().get(url).then().assertThat().statusCode(200).body(equalTo(value));
}
}
use of io.vertx.core.http.HttpClient in project mod-inventory-storage by folio-org.
the class ShelfLocationsTest method send.
private static void send(URL url, HttpMethod method, String content, String contentType, Handler<HttpClientResponse> handler) {
HttpClient client = StorageTestSuite.getVertx().createHttpClient();
HttpClientRequest request;
if (content == null) {
content = "";
}
Buffer buffer = Buffer.buffer(content);
if (method == HttpMethod.POST) {
request = client.postAbs(url.toString());
} else if (method == HttpMethod.DELETE) {
request = client.deleteAbs(url.toString());
} else if (method == HttpMethod.GET) {
request = client.getAbs(url.toString());
} else {
request = client.putAbs(url.toString());
}
request.exceptionHandler(error -> {
Assert.fail(error.getLocalizedMessage());
}).handler(handler);
request.putHeader("Authorization", "test_tenant");
request.putHeader("x-okapi-tenant", "test_tenant");
request.putHeader("Accept", "application/json,text/plain");
request.putHeader("Content-type", contentType);
request.end(buffer);
}
Aggregations