Search in sources :

Example 46 with HttpServer

use of io.vertx.core.http.HttpServer in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadEventWaitsForAcceptedOutcome.

/**
 * Verifies that the adapter waits for an event being settled and accepted
 * by a downstream peer before responding with a 202 status to the device.
 */
@Test
public void testUploadEventWaitsForAcceptedOutcome() {
    // GIVEN an adapter with a downstream event consumer attached
    final Future<ProtonDelivery> outcome = Future.future();
    givenAnEventSenderForOutcome(outcome);
    HttpServer server = getHttpServer(false);
    AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
    // WHEN a device publishes an event
    final Buffer payload = Buffer.buffer("some payload");
    final HttpServerResponse response = mock(HttpServerResponse.class);
    final RoutingContext ctx = newRoutingContext(payload, response);
    adapter.uploadEventMessage(ctx, "tenant", "device", payload, "application/text");
    // THEN the device does not get a response
    verify(response, never()).end();
    // until the event has been accepted
    outcome.complete(mock(ProtonDelivery.class));
    verify(response).setStatusCode(202);
    verify(response).end();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) ProtonDelivery(io.vertx.proton.ProtonDelivery) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 47 with HttpServer

use of io.vertx.core.http.HttpServer in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testStartInvokesOnStartupSuccess.

/**
 * Verifies that the <me>onStartupSuccess</em> method is invoked if the http server has been started successfully.
 *
 * @param ctx The helper to use for running async tests on vertx.
 */
@Test
public void testStartInvokesOnStartupSuccess(final TestContext ctx) {
    // GIVEN an adapter with a client provided http server
    HttpServer server = getHttpServer(false);
    Async onStartupSuccess = ctx.async();
    AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, s -> onStartupSuccess.complete());
    adapter.setCredentialsAuthProvider(credentialsAuthProvider);
    adapter.setMetrics(mock(HttpAdapterMetrics.class));
    // WHEN starting the adapter
    Async startup = ctx.async();
    Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertSuccess(s -> {
        startup.complete();
    }));
    adapter.start(startupTracker);
    // THEN the onStartupSuccess method has been invoked
    startup.await();
    onStartupSuccess.await();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ArgumentMatchers(org.mockito.ArgumentMatchers) ProtonDelivery(io.vertx.proton.ProtonDelivery) TenantConstants(org.eclipse.hono.util.TenantConstants) HttpServer(io.vertx.core.http.HttpServer) RunWith(org.junit.runner.RunWith) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) TenantClient(org.eclipse.hono.client.TenantClient) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Mockito(org.mockito.Mockito) HonoClientBasedAuthProvider(org.eclipse.hono.service.auth.device.HonoClientBasedAuthProvider) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 48 with HttpServer

use of io.vertx.core.http.HttpServer in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testStartUpFailsIfCredentialsAuthProviderIsNotSet.

/**
 * Verifies that the <me>onStartupSuccess</em> method is not invoked if no credentials authentication provider is set.
 *
 * @param ctx The helper to use for running async tests on vertx.
 */
@Test
public void testStartUpFailsIfCredentialsAuthProviderIsNotSet(final TestContext ctx) {
    // GIVEN an adapter with a client provided http server
    HttpServer server = getHttpServer(false);
    AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, s -> ctx.fail("should not have invoked onStartupSuccess"));
    // WHEN starting the adapter
    Async startup = ctx.async();
    Future<Void> startupTracker = Future.future();
    startupTracker.setHandler(ctx.asyncAssertFailure(s -> {
        startup.complete();
    }));
    adapter.start(startupTracker);
    // THEN the onStartupSuccess method has been invoked
    startup.await();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpServerRequest(io.vertx.core.http.HttpServerRequest) ArgumentMatchers(org.mockito.ArgumentMatchers) ProtonDelivery(io.vertx.proton.ProtonDelivery) TenantConstants(org.eclipse.hono.util.TenantConstants) HttpServer(io.vertx.core.http.HttpServer) RunWith(org.junit.runner.RunWith) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) TenantClient(org.eclipse.hono.client.TenantClient) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Mockito(org.mockito.Mockito) HonoClientBasedAuthProvider(org.eclipse.hono.service.auth.device.HonoClientBasedAuthProvider) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 49 with HttpServer

use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.

the class VertxWebApplication method start.

@Validate
public void start() throws Exception {
    setUpInitialData();
    TcclSwitch.executeWithTCCLSwitch(() -> {
        Router router = Router.router(vertx);
        router.route().handler(BodyHandler.create());
        router.get("/products/:productID").handler(this::handleGetProduct);
        router.put("/products/:productID").handler(this::handleAddProduct);
        router.get("/products").handler(this::handleListProducts);
        router.get("/assets/*").handler(StaticHandler.create("assets", this.getClass().getClassLoader()));
        LOGGER.info("Creating HTTP server for vert.x web application");
        HttpServer server = vertx.createHttpServer();
        server.requestHandler(router::accept).listen(8081);
    });
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router)

Example 50 with HttpServer

use of io.vertx.core.http.HttpServer in project vertx-examples by vert-x3.

the class Dashboard method start.

@Override
public void start() {
    MetricsService service = MetricsService.create(vertx);
    Router router = Router.router(vertx);
    // Allow outbound traffic to the news-feed address
    BridgeOptions options = new BridgeOptions().addOutboundPermitted(new PermittedOptions().setAddress("metrics"));
    router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(options));
    // Serve the static resources
    router.route().handler(StaticHandler.create());
    HttpServer httpServer = vertx.createHttpServer();
    httpServer.requestHandler(router::accept).listen(8080);
    // Send a metrics events every second
    vertx.setPeriodic(1000, t -> {
        JsonObject metrics = service.getMetricsSnapshot(vertx.eventBus());
        vertx.eventBus().publish("metrics", metrics);
    });
    // Send some messages
    Random random = new Random();
    vertx.eventBus().consumer("whatever", msg -> {
        vertx.setTimer(10 + random.nextInt(50), id -> {
            vertx.eventBus().send("whatever", "hello");
        });
    });
    vertx.eventBus().send("whatever", "hello");
}
Also used : Random(java.util.Random) MetricsService(io.vertx.ext.dropwizard.MetricsService) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) PermittedOptions(io.vertx.ext.web.handler.sockjs.PermittedOptions)

Aggregations

HttpServer (io.vertx.core.http.HttpServer)82 Router (io.vertx.ext.web.Router)37 HttpServerOptions (io.vertx.core.http.HttpServerOptions)32 Test (org.junit.Test)24 JsonObject (io.vertx.core.json.JsonObject)17 Buffer (io.vertx.core.buffer.Buffer)14 HttpServerResponse (io.vertx.core.http.HttpServerResponse)14 Future (io.vertx.core.Future)12 HttpClient (io.vertx.core.http.HttpClient)12 Vertx (io.vertx.core.Vertx)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 Handler (io.vertx.core.Handler)9 HttpMethod (io.vertx.core.http.HttpMethod)9 VertxOptions (io.vertx.core.VertxOptions)8 HttpClientOptions (io.vertx.core.http.HttpClientOptions)8 List (java.util.List)8 AbstractVerticle (io.vertx.core.AbstractVerticle)7 PemKeyCertOptions (io.vertx.core.net.PemKeyCertOptions)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 AsyncResult (io.vertx.core.AsyncResult)6