Search in sources :

Example 41 with HttpClient

use of io.vertx.core.http.HttpClient in project vert.x by eclipse.

the class HAProxyTest method testHttpWithoutHAProxySupport.

@Test
public void testHttpWithoutHAProxySupport() {
    Vertx vertx = Vertx.vertx();
    try {
        vertx.createHttpServer(new HttpServerOptions().setUseProxyProtocol(true)).requestHandler(req -> {
            req.response().end("hello");
        }).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(s -> {
            HttpClient client = vertx.createHttpClient();
            client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(req -> {
                req.send(onSuccess(resp -> {
                    resp.body(onSuccess(body -> {
                        assertEquals("hello", body.toString());
                        testComplete();
                    }));
                }));
            }));
        }));
        await();
    } finally {
        vertx.close();
    }
}
Also used : DEFAULT_HTTP_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST) HttpMethod(io.vertx.core.http.HttpMethod) Vertx(io.vertx.core.Vertx) DEFAULT_HTTP_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test) VertxTestBase(io.vertx.test.core.VertxTestBase) HttpClient(io.vertx.core.http.HttpClient) HttpClient(io.vertx.core.http.HttpClient) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Vertx(io.vertx.core.Vertx) Test(org.junit.Test)

Example 42 with HttpClient

use of io.vertx.core.http.HttpClient in project vert.x by eclipse.

the class VertxTest method testFinalizeHttpClient.

@Test
public void testFinalizeHttpClient() throws Exception {
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    try {
        CountDownLatch latch = new CountDownLatch(1);
        AtomicReference<NetSocket> socketRef = new AtomicReference<>();
        vertx.createNetServer().connectHandler(socketRef::set).listen(8080, "localhost").onComplete(onSuccess(server -> latch.countDown()));
        awaitLatch(latch);
        AtomicBoolean closed = new AtomicBoolean();
        // No keep alive so the connection is not held in the pool ????
        CloseFuture closeFuture = new CloseFuture();
        closeFuture.future().onComplete(ar -> closed.set(true));
        HttpClient client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false), closeFuture);
        vertx.addCloseHook(closeFuture);
        client.request(HttpMethod.GET, 8080, "localhost", "/").compose(HttpClientRequest::send).onComplete(onFailure(err -> {
        }));
        WeakReference<HttpClient> ref = new WeakReference<>(client);
        closeFuture = null;
        client = null;
        assertWaitUntil(() -> socketRef.get() != null);
        for (int i = 0; i < 10; i++) {
            Thread.sleep(10);
            RUNNER.runSystemGC();
            assertFalse(closed.get());
            assertNotNull(ref.get());
        }
        socketRef.get().close();
        long now = System.currentTimeMillis();
        while (true) {
            assertTrue(System.currentTimeMillis() - now < 20_000);
            RUNNER.runSystemGC();
            if (ref.get() == null) {
                assertTrue(closed.get());
                break;
            }
        }
    } finally {
        vertx.close(ar -> {
            testComplete();
        });
    }
    await();
}
Also used : NetSocket(io.vertx.core.net.NetSocket) VertxInternal(io.vertx.core.impl.VertxInternal) URL(java.net.URL) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) HttpClientRequest(io.vertx.core.http.HttpClientRequest) RepeatRule(io.vertx.test.core.RepeatRule) OptionsBuilder(org.openjdk.jmh.runner.options.OptionsBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) URLClassLoader(java.net.URLClassLoader) Rule(org.junit.Rule) AsyncTestBase(io.vertx.test.core.AsyncTestBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpMethod(io.vertx.core.http.HttpMethod) CloseFuture(io.vertx.core.impl.CloseFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) NetClient(io.vertx.core.net.NetClient) Runner(org.openjdk.jmh.runner.Runner) WeakReference(java.lang.ref.WeakReference) HttpClient(io.vertx.core.http.HttpClient) NetSocket(io.vertx.core.net.NetSocket) CloseFuture(io.vertx.core.impl.CloseFuture) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpClient(io.vertx.core.http.HttpClient) WeakReference(java.lang.ref.WeakReference) Test(org.junit.Test)

Example 43 with HttpClient

use of io.vertx.core.http.HttpClient in project vert.x by eclipse.

the class NettyCompatTest method testHttp2.

@Test
public void testHttp2() {
    vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setSslEngineOptions(new OpenSSLEngineOptions()).setKeyCertOptions(Cert.SERVER_JKS.get())).requestHandler(req -> req.response().end("OK")).listen(8443, "localhost", onSuccess(s -> {
        HttpClient client = vertx.createHttpClient(new HttpClientOptions().setSsl(true).setSslEngineOptions(new OpenSSLEngineOptions()).setTrustStoreOptions(Trust.SERVER_JKS.get()));
        client.getNow(8443, "localhost", "/somepath", resp -> {
            resp.bodyHandler(buff -> {
                assertEquals("OK", buff.toString());
                testComplete();
            });
        });
    }));
    await();
}
Also used : OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) VertxInternal(io.vertx.core.impl.VertxInternal) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Cert(io.vertx.test.core.tls.Cert) VertxTestBase(io.vertx.test.core.VertxTestBase) HttpClient(io.vertx.core.http.HttpClient) Trust(io.vertx.test.core.tls.Trust) HttpClient(io.vertx.core.http.HttpClient) HttpServerOptions(io.vertx.core.http.HttpServerOptions) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 44 with HttpClient

use of io.vertx.core.http.HttpClient in project vert.x by eclipse.

the class DummyMetricsTest method testDummyHttpClientMetrics.

@Test
public void testDummyHttpClientMetrics() {
    HttpClient client = vertx.createHttpClient(new HttpClientOptions());
    assertFalse(client.isMetricsEnabled());
}
Also used : HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 45 with HttpClient

use of io.vertx.core.http.HttpClient in project vert.x by eclipse.

the class ProxyErrorTest method proxyTest.

private void proxyTest(int error, String username, String url, Handler<HttpClientResponse> assertResponse, boolean completeOnException) throws Exception {
    startProxy(error, username);
    final HttpClientOptions options = new HttpClientOptions().setSsl(url.startsWith("https")).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()));
    HttpClient client = vertx.createHttpClient(options);
    client.getAbs(url, assertResponse).exceptionHandler(e -> {
        if (completeOnException) {
            testComplete();
        } else {
            fail(e);
        }
    }).end();
    await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientResponse(io.vertx.core.http.HttpClientResponse) ProxyOptions(io.vertx.core.net.ProxyOptions) ProxyType(io.vertx.core.net.ProxyType) HttpProxy(io.vertx.test.core.HttpProxy) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) VertxTestBase(io.vertx.test.core.VertxTestBase) LoggerFactory(io.vertx.core.logging.LoggerFactory) HttpClient(io.vertx.core.http.HttpClient) ProxyOptions(io.vertx.core.net.ProxyOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

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