Search in sources :

Example 1 with HttpServer

use of io.vertx.mutiny.core.http.HttpServer in project smallrye-mutiny-vertx-bindings by smallrye.

the class MicrometerMetricsTest method test.

@Test
public void test() {
    Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MicrometerMetricsOptions().setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true)).setEnabled(true)));
    Router router = Router.router(vertx);
    router.route("/metrics").handler(x -> PrometheusScrapingHandler.create().handle(x));
    HttpServer server = vertx.createHttpServer().requestHandler(router).listenAndAwait(8080);
    WebClient client = WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));
    String s = client.get("/metrics").sendAndAwait().bodyAsString();
    assertThat(s).contains("vertx_http_client_active_connections");
    MetricsService metricsService = MetricsService.create(server);
    JsonObject metrics = metricsService.getMetricsSnapshot();
    System.out.println(metrics);
    assertThat(metrics.getJsonArray("vertx.http.server.active.connections")).isNotNull();
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) VertxPrometheusOptions(io.vertx.micrometer.VertxPrometheusOptions) HttpServer(io.vertx.mutiny.core.http.HttpServer) Router(io.vertx.mutiny.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) Vertx(io.vertx.mutiny.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) WebClient(io.vertx.mutiny.ext.web.client.WebClient) MicrometerMetricsOptions(io.vertx.micrometer.MicrometerMetricsOptions) Test(org.junit.Test)

Example 2 with HttpServer

use of io.vertx.mutiny.core.http.HttpServer in project smallrye-mutiny-vertx-bindings by smallrye.

the class OtherWebClientTest method testResponseBodyAsAsJsonMapped.

@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
    JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestStream().handler(req -> req.response().endAndForget(expected.encode()));
    try {
        server.listenAndAwait();
        client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
        Uni<HttpResponse<WineAndCheese>> uni = client.get(8080, "localhost", "/the_uri").as(BodyCodec.json(WineAndCheese.class)).send();
        uni.subscribe().with(resp -> {
            assertEquals(200, resp.statusCode());
            assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
            testComplete();
        }, this::fail);
        await();
    } finally {
        server.closeAndAwait();
    }
}
Also used : HttpServer(io.vertx.mutiny.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 3 with HttpServer

use of io.vertx.mutiny.core.http.HttpServer in project smallrye-mutiny-vertx-bindings by smallrye.

the class OtherWebClientTest method testGet.

@Test
public void testGet() {
    int times = 5;
    waitFor(times);
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestStream().handler(req -> req.response().setChunked(true).endAndForget("some_content"));
    try {
        server.listenAndAwait();
        client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
        Uni<HttpResponse<Buffer>> uni = client.get(8080, "localhost", "/the_uri").as(BodyCodec.buffer()).send();
        for (int i = 0; i < times; i++) {
            uni.subscribe().with(resp -> {
                Buffer body = resp.body();
                assertEquals("some_content", body.toString("UTF-8"));
                complete();
            }, this::fail);
        }
        await();
    } finally {
        server.closeAndAwait();
    }
}
Also used : Buffer(io.vertx.mutiny.core.buffer.Buffer) HttpServer(io.vertx.mutiny.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 4 with HttpServer

use of io.vertx.mutiny.core.http.HttpServer in project smallrye-mutiny-vertx-bindings by smallrye.

the class OtherWebClientTest method testPost.

@Test
public void testPost() {
    int times = 5;
    waitFor(times);
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestStream().handler(req -> req.bodyHandler(buff -> {
        assertEquals("onetwothree", buff.toString());
        req.response().endAndForget();
    }));
    try {
        server.listenAndAwait();
        client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
        Multi<Buffer> stream = Multi.createFrom().items(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three"));
        Uni<HttpResponse<Buffer>> uni = client.post(8080, "localhost", "/the_uri").sendStream(stream);
        for (int i = 0; i < times; i++) {
            uni.subscribe().with(resp -> complete(), this::fail);
        }
        await();
    } finally {
        server.closeAndAwait();
    }
}
Also used : HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) Test(org.junit.Test) VertxTestBase(io.vertx.test.core.VertxTestBase) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Buffer(io.vertx.mutiny.core.buffer.Buffer) HttpServer(io.vertx.mutiny.core.http.HttpServer) Vertx(io.vertx.mutiny.core.Vertx) WebClient(io.vertx.mutiny.ext.web.client.WebClient) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) BodyCodec(io.vertx.mutiny.ext.web.codec.BodyCodec) Buffer(io.vertx.mutiny.core.buffer.Buffer) HttpServer(io.vertx.mutiny.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 5 with HttpServer

use of io.vertx.mutiny.core.http.HttpServer in project smallrye-mutiny-vertx-bindings by smallrye.

the class OtherWebClientTest method testResponseMissingBody.

@Test
public void testResponseMissingBody() throws Exception {
    int times = 5;
    waitFor(times);
    HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
    server.requestStream().handler(req -> req.response().setStatusCode(403).endAndForget());
    try {
        server.listenAndAwait();
        client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
        Uni<HttpResponse<Buffer>> uni = client.get(8080, "localhost", "/the_uri").send();
        for (int i = 0; i < times; i++) {
            uni.subscribe().with(resp -> {
                System.out.println("done");
                assertEquals(403, resp.statusCode());
                assertNull(resp.body());
                complete();
            }, this::fail);
        }
        await();
    } finally {
        server.closeAndAwait();
    }
}
Also used : HttpServer(io.vertx.mutiny.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Aggregations

HttpServer (io.vertx.mutiny.core.http.HttpServer)5 Test (org.junit.Test)5 HttpClientOptions (io.vertx.core.http.HttpClientOptions)4 HttpServerOptions (io.vertx.core.http.HttpServerOptions)4 HttpResponse (io.vertx.mutiny.ext.web.client.HttpResponse)4 JsonObject (io.vertx.core.json.JsonObject)3 Vertx (io.vertx.mutiny.core.Vertx)2 Buffer (io.vertx.mutiny.core.buffer.Buffer)2 WebClient (io.vertx.mutiny.ext.web.client.WebClient)2 Multi (io.smallrye.mutiny.Multi)1 Uni (io.smallrye.mutiny.Uni)1 VertxOptions (io.vertx.core.VertxOptions)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 MicrometerMetricsOptions (io.vertx.micrometer.MicrometerMetricsOptions)1 VertxPrometheusOptions (io.vertx.micrometer.VertxPrometheusOptions)1 Router (io.vertx.mutiny.ext.web.Router)1 BodyCodec (io.vertx.mutiny.ext.web.codec.BodyCodec)1 VertxTestBase (io.vertx.test.core.VertxTestBase)1