Search in sources :

Example 66 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-micrometer-metrics by vert-x3.

the class MetricsServiceImplTest method runClientRequests.

private void runClientRequests(TestContext ctx, HttpClient httpClient, int count, String path) {
    Async async = ctx.async(count);
    for (int i = 0; i < count; i++) {
        httpClient.post(9195, "127.0.0.1", path, response -> {
            async.countDown();
            if (response.statusCode() != 200) {
                ctx.fail(response.statusMessage());
            }
        }).exceptionHandler(t -> {
            async.countDown();
            ctx.fail(t);
        }).putHeader("Content-Length", String.valueOf(CLIENT_REQUEST.getBytes().length)).write(CLIENT_REQUEST).end();
    }
    async.await();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) VertxPrometheusOptions(io.vertx.micrometer.VertxPrometheusOptions) UUID(java.util.UUID) MetricsService(io.vertx.micrometer.MetricsService) JsonArray(io.vertx.core.json.JsonArray) MicrometerMetricsOptions(io.vertx.micrometer.MicrometerMetricsOptions) List(java.util.List) After(org.junit.After) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) HttpClient(io.vertx.core.http.HttpClient) Before(org.junit.Before) Async(io.vertx.ext.unit.Async)

Example 67 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-micrometer-metrics by vert-x3.

the class MetricsServiceImplTest method shouldGetCompleteSnapshot.

@Test
public void shouldGetCompleteSnapshot(TestContext ctx) throws InterruptedException {
    HttpClient httpClient = vertx.createHttpClient();
    runClientRequests(ctx, httpClient, 10, "/r1");
    runClientRequests(ctx, httpClient, 5, "/r2");
    httpClient.close();
    JsonObject snapshot = MetricsService.create(vertx).getMetricsSnapshot();
    assertThat(snapshot).extracting(Map.Entry::getKey).containsExactly("vertx.http.client.bytesReceived", "vertx.http.client.bytesSent", "vertx.http.client.connections", "vertx.http.client.requestCount", "vertx.http.client.requests", "vertx.http.client.responseCount", "vertx.http.client.responseTime", "vertx.http.server.bytesReceived", "vertx.http.server.bytesSent", "vertx.http.server.connections", "vertx.http.server.requestCount", "vertx.http.server.requests", "vertx.http.server.responseTime");
    assertThat(snapshot).flatExtracting(e -> (List<JsonObject>) ((JsonArray) (e.getValue())).getList()).filteredOn(obj -> obj.getString("type").equals("counter")).hasSize(6).flatExtracting(JsonObject::fieldNames).contains("count");
    assertThat(snapshot).flatExtracting(e -> (List<JsonObject>) ((JsonArray) (e.getValue())).getList()).filteredOn(obj -> obj.getString("type").equals("summary")).hasSize(4).flatExtracting(JsonObject::fieldNames).contains("mean", "max", "total");
    assertThat(snapshot).flatExtracting(e -> (List<JsonObject>) ((JsonArray) (e.getValue())).getList()).filteredOn(obj -> obj.getString("type").equals("timer")).hasSize(4).flatExtracting(JsonObject::fieldNames).contains("totalTimeMs", "meanMs", "maxMs");
}
Also used : JsonArray(io.vertx.core.json.JsonArray) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) VertxPrometheusOptions(io.vertx.micrometer.VertxPrometheusOptions) UUID(java.util.UUID) MetricsService(io.vertx.micrometer.MetricsService) JsonArray(io.vertx.core.json.JsonArray) MicrometerMetricsOptions(io.vertx.micrometer.MicrometerMetricsOptions) List(java.util.List) After(org.junit.After) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) HttpClient(io.vertx.core.http.HttpClient) Before(org.junit.Before) HttpClient(io.vertx.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Map(java.util.Map) Test(org.junit.Test)

Example 68 with HttpClient

use of io.vertx.core.http.HttpClient in project strimzi by strimzi.

the class Main method isOnOpenShift.

static Future<Boolean> isOnOpenShift(Vertx vertx, KubernetesClient client) {
    URL kubernetesApi = client.getMasterUrl();
    Future<Boolean> fut = Future.future();
    HttpClientOptions httpClientOptions = new HttpClientOptions();
    httpClientOptions.setDefaultHost(kubernetesApi.getHost());
    if (kubernetesApi.getPort() == -1) {
        httpClientOptions.setDefaultPort(kubernetesApi.getDefaultPort());
    } else {
        httpClientOptions.setDefaultPort(kubernetesApi.getPort());
    }
    if (kubernetesApi.getProtocol().equals("https")) {
        httpClientOptions.setSsl(true);
        httpClientOptions.setTrustAll(true);
    }
    HttpClient httpClient = vertx.createHttpClient(httpClientOptions);
    httpClient.getNow("/oapi", res -> {
        if (res.statusCode() == HttpResponseStatus.OK.code()) {
            log.debug("{} returned {}. We are on OpenShift.", res.request().absoluteURI(), res.statusCode());
            // We should be on OpenShift based on the /oapi result. We can now safely try isAdaptable() to be 100% sure.
            Boolean isOpenShift = Boolean.TRUE.equals(client.isAdaptable(OpenShiftClient.class));
            fut.complete(isOpenShift);
        } else {
            log.debug("{} returned {}. We are not on OpenShift.", res.request().absoluteURI(), res.statusCode());
            fut.complete(Boolean.FALSE);
        }
    });
    return fut;
}
Also used : HttpClient(io.vertx.core.http.HttpClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) URL(java.net.URL) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 69 with HttpClient

use of io.vertx.core.http.HttpClient in project vertx-sync by vert-x3.

the class TestVerticle method testFiberHandler.

@Suspendable
protected void testFiberHandler() {
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestHandler(fiberHandler(req -> {
        String res = awaitResult(h -> ai.methodWithParamsAndHandlerNoReturn("oranges", 23, h));
        assertEquals("oranges23", res);
        req.response().end();
    }));
    server.listen(res -> {
        assertTrue(res.succeeded());
        HttpClient client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
        client.getNow("/somepath", resp -> {
            assertTrue(resp.statusCode() == 200);
            client.close();
            server.close(res2 -> {
                complete();
            });
        });
    });
}
Also used : VertxException(io.vertx.core.VertxException) Strand(co.paralleluniverse.strands.Strand) HttpServer(io.vertx.core.http.HttpServer) Context(io.vertx.core.Context) Is(org.hamcrest.core.Is) Channel(co.paralleluniverse.strands.channels.Channel) Suspendable(co.paralleluniverse.fibers.Suspendable) EventBus(io.vertx.core.eventbus.EventBus) SyncVerticle(io.vertx.ext.sync.SyncVerticle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReturnedInterface(io.vertx.ext.sync.testmodel.ReturnedInterface) Channels(co.paralleluniverse.strands.channels.Channels) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Method(java.lang.reflect.Method) Sync(io.vertx.ext.sync.Sync) AsyncInterface(io.vertx.ext.sync.testmodel.AsyncInterface) Vertx(io.vertx.core.Vertx) HandlerReceiverAdaptor(io.vertx.ext.sync.HandlerReceiverAdaptor) Message(io.vertx.core.eventbus.Message) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) TimeUnit(java.util.concurrent.TimeUnit) ReceivePort(co.paralleluniverse.strands.channels.ReceivePort) HttpServerOptions(io.vertx.core.http.HttpServerOptions) AsyncInterfaceImpl(io.vertx.ext.sync.testmodel.AsyncInterfaceImpl) Assert(org.junit.Assert) HttpClient(io.vertx.core.http.HttpClient) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Suspendable(co.paralleluniverse.fibers.Suspendable)

Example 70 with HttpClient

use of io.vertx.core.http.HttpClient in project engine by Lumeer.

the class RestImportTest method startServer.

private void startServer(final List<RequestDto> messages) {
    final Vertx vertx = Vertx.vertx();
    final HttpServer server = vertx.createHttpServer();
    final Router router = Router.router(vertx);
    final HttpClient client = vertx.createHttpClient();
    router.route("/*").handler(BodyHandler.create());
    router.route("/*").handler((context) -> {
        messages.add(new RequestDto(context.request().method().toString(), context.getBodyAsString(), context.request().headers()));
        context.response().setStatusCode(200).end();
    });
    this.server = server;
    this.serverThread = new Thread(() -> server.requestHandler(router::accept).listen(8091));
    this.serverThread.start();
}
Also used : HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) Vertx(io.vertx.core.Vertx)

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