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();
}
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();
}
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();
}
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);
});
}
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");
}
Aggregations