Search in sources :

Example 56 with HttpClient

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();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Utils(io.vertx.ext.web.impl.Utils) HttpVersion(io.vertx.core.http.HttpVersion) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) WebTestBase(io.vertx.ext.web.WebTestBase) DateFormat(java.text.DateFormat) Set(java.util.Set) Test(org.junit.Test) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpClient(io.vertx.core.http.HttpClient) ArrayList(java.util.ArrayList) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 57 with HttpClient

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);
}
Also used : SessionHandlerTestBase(io.vertx.ext.web.handler.SessionHandlerTestBase) CookieHandler(io.vertx.ext.web.handler.CookieHandler) Session(io.vertx.ext.web.Session) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) HttpServer(io.vertx.core.http.HttpServer) VertxOptions(io.vertx.core.VertxOptions) Router(io.vertx.ext.web.Router) SessionImpl(io.vertx.ext.web.sstore.impl.SessionImpl) Test(org.junit.Test) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) Buffer(io.vertx.core.buffer.Buffer) TestUtils(io.vertx.test.core.TestUtils) HttpMethod(io.vertx.core.http.HttpMethod) SessionHandler(io.vertx.ext.web.handler.SessionHandler) SomeSerializable(io.vertx.ext.web.handler.SomeSerializable) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Session(io.vertx.ext.web.Session) Test(org.junit.Test)

Example 58 with HttpClient

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));
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) OC(io.vertx.it.openshift.utils.OC) Route(io.fabric8.openshift.api.model.Route) OpenShiftHelper(io.vertx.it.openshift.utils.OpenShiftHelper) AbstractTestClass(io.vertx.it.openshift.utils.AbstractTestClass) Ensure(io.vertx.it.openshift.utils.Ensure) TestName(org.junit.rules.TestName) After(org.junit.After) Assertions(org.assertj.core.api.Assertions) Service(io.fabric8.kubernetes.api.model.Service) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Kube(io.vertx.it.openshift.utils.Kube) Test(org.junit.Test) IOException(java.io.IOException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) TreeMap(java.util.TreeMap) RestAssured(io.restassured.RestAssured) Awaitility(org.awaitility.Awaitility) SortedMap(java.util.SortedMap) HttpClient(io.vertx.core.http.HttpClient) HttpClient(io.vertx.core.http.HttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) Test(org.junit.Test)

Example 59 with HttpClient

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));
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) BeforeClass(org.junit.BeforeClass) URL(java.net.URL) OC(io.vertx.it.openshift.utils.OC) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Route(io.fabric8.openshift.api.model.Route) OpenShiftHelper(io.vertx.it.openshift.utils.OpenShiftHelper) AbstractTestClass(io.vertx.it.openshift.utils.AbstractTestClass) Ensure(io.vertx.it.openshift.utils.Ensure) After(org.junit.After) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) Service(io.fabric8.kubernetes.api.model.Service) Before(org.junit.Before) Vertx(io.vertx.core.Vertx) Kube(io.vertx.it.openshift.utils.Kube) Test(org.junit.Test) IOException(java.io.IOException) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) TreeMap(java.util.TreeMap) Optional(java.util.Optional) RestAssured.given(io.restassured.RestAssured.given) RestAssured(io.restassured.RestAssured) Assert(org.junit.Assert) Awaitility(org.awaitility.Awaitility) SortedMap(java.util.SortedMap) HttpClient(io.vertx.core.http.HttpClient) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) Test(org.junit.Test)

Example 60 with HttpClient

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);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ResponseHandler(org.folio.rest.support.ResponseHandler) HttpURLConnection(java.net.HttpURLConnection) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Response(org.folio.rest.support.Response) URL(java.net.URL) MaterialTypesClient(org.folio.rest.support.client.MaterialTypesClient) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) HttpResponseMatchers.statusCodeIs(org.folio.rest.support.HttpResponseMatchers.statusCodeIs) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) Assert.assertThat(org.junit.Assert.assertThat) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) JsonObject(io.vertx.core.json.JsonObject) Before(org.junit.Before) InterfaceUrls(org.folio.rest.support.http.InterfaceUrls) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test) UUID(java.util.UUID) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Handler(io.vertx.core.Handler) Assert(org.junit.Assert) HttpClient(io.vertx.core.http.HttpClient) LoanTypesClient(org.folio.rest.support.client.LoanTypesClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)77 Test (org.junit.Test)47 HttpClientRequest (io.vertx.core.http.HttpClientRequest)36 HttpClientOptions (io.vertx.core.http.HttpClientOptions)25 Vertx (io.vertx.core.Vertx)22 HttpMethod (io.vertx.core.http.HttpMethod)22 JsonObject (io.vertx.core.json.JsonObject)22 Handler (io.vertx.core.Handler)18 Buffer (io.vertx.core.buffer.Buffer)18 HttpClientResponse (io.vertx.core.http.HttpClientResponse)16 TimeUnit (java.util.concurrent.TimeUnit)16 HttpServer (io.vertx.core.http.HttpServer)15 Async (io.vertx.ext.unit.Async)15 Before (org.junit.Before)15 File (java.io.File)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 URL (java.net.URL)12 HttpServerOptions (io.vertx.core.http.HttpServerOptions)11 IOException (java.io.IOException)10 List (java.util.List)10