Search in sources :

Example 76 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-web by vert-x3.

the class SockJSTestBase method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
    router = Router.router(vertx);
    router.route().handler(CookieHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)).setNagHttps(false).setSessionTimeout(60 * 60 * 1000));
    addHandlersBeforeSockJSHandler(router);
    SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
    sockJSHandler = SockJSHandler.create(vertx, options);
    router.route("/test/*").handler(sockJSHandler);
    server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
    CountDownLatch latch = new CountDownLatch(1);
    server.requestHandler(router).listen(ar -> latch.countDown());
    awaitLatch(latch);
}
Also used : HttpServerOptions(io.vertx.core.http.HttpServerOptions) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 77 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions 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 78 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-openshift-it by cescoffier.

the class Http2IT method testHttp2_H2C.

/**
 * Checks that we can call a HTTP/2 - H2C endpoint exposed by a pod.
 */
@Test
@Ignore("Openshift does not support HTTP/2 H2C route")
public void testHttp2_H2C() throws Exception {
    Assertions.assertThat(client).deployments().pods().isPodReadyForPeriod();
    AtomicReference<String> response = new AtomicReference<>();
    HttpClientOptions options = new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2).setHttp2ClearTextUpgrade(false);
    final String host = securedUrlForRoute(client.routes().withName("aloha").get()).getHost();
    System.out.println("Host: " + host);
    vertx.createHttpClient(options).getNow(80, host, "/", resp -> {
        System.out.println("Got H2C response " + resp.statusCode() + " with protocol " + resp.version());
        resp.bodyHandler(body -> {
            System.out.println("Got H2C data " + body.toString("ISO-8859-1"));
            response.set(body.toString("ISO-8859-1"));
        });
    });
    await().atMost(1, TimeUnit.MINUTES).untilAtomic(response, is(notNullValue()));
    assertThat(response.get()).contains("Aloha HTTP_2");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 79 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-openshift-it by cescoffier.

the class Http2IT method testHttp2_Internal_H2C.

/**
 * Checks that we can call a HTTP/2 - H2C endpoint exposed by a pod from another pod (no route).
 */
@Test
public void testHttp2_Internal_H2C() throws Exception {
    Assertions.assertThat(client).deployments().pods().isPodReadyForPeriod();
    AtomicReference<String> response = new AtomicReference<>();
    HttpClientOptions options = new HttpClientOptions();
    final String host = urlForRoute(client.routes().withName("http2-it").get()).getHost();
    System.out.println("Host: " + host);
    vertx.createHttpClient(options).getNow(80, host, "/aloha", resp -> {
        System.out.println("Got H2C response " + resp.statusCode() + " with protocol " + resp.version());
        resp.bodyHandler(body -> {
            System.out.println("Got H2C data " + body.toString("ISO-8859-1"));
            response.set(body.toString("ISO-8859-1"));
        });
    });
    await().atMost(1, TimeUnit.MINUTES).untilAtomic(response, is(notNullValue()));
    assertThat(response.get()).contains("version = HTTP_2").contains("Aloha from vert.x!").contains("HTTP_1_1");
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 80 with HttpClientOptions

use of io.vertx.core.http.HttpClientOptions in project vertx-openshift-it by cescoffier.

the class GatewayVerticle method webclient.

private void webclient(RoutingContext routingContext) {
    String host = routingContext.request().getParam("host");
    final HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions().setDefaultHost(host).setKeepAlive(false));
    forward(routingContext, httpClient, true);
}
Also used : HttpClient(io.vertx.rxjava.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Aggregations

HttpClientOptions (io.vertx.core.http.HttpClientOptions)101 Test (org.junit.Test)51 HttpClient (io.vertx.core.http.HttpClient)27 HttpClientRequest (io.vertx.core.http.HttpClientRequest)23 HttpServerOptions (io.vertx.core.http.HttpServerOptions)19 CountDownLatch (java.util.concurrent.CountDownLatch)15 HttpMethod (io.vertx.core.http.HttpMethod)14 SSLCustom (org.apache.servicecomb.foundation.ssl.SSLCustom)14 SSLOption (org.apache.servicecomb.foundation.ssl.SSLOption)14 HttpVersion (io.vertx.core.http.HttpVersion)13 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Vertx (io.vertx.core.Vertx)11 MockUp (mockit.MockUp)11 DeploymentOptions (io.vertx.core.DeploymentOptions)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Buffer (io.vertx.core.buffer.Buffer)9 HttpServer (io.vertx.core.http.HttpServer)8 HttpServerResponse (io.vertx.core.http.HttpServerResponse)8 VertxOptions (io.vertx.core.VertxOptions)7 VertxInternal (io.vertx.core.impl.VertxInternal)7