use of io.vertx.core.http.HttpServer in project vertx-micrometer-metrics by vert-x3.
the class MetricsServiceImplTest method setUp.
@Before
public void setUp(TestContext ctx) {
vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MicrometerMetricsOptions().setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true)).setRegistryName(registryName).setEnabled(true))).exceptionHandler(ctx.exceptionHandler());
// Setup server
Async serverReady = ctx.async();
httpServer = vertx.createHttpServer();
httpServer.requestHandler(req -> {
// Timer as artificial processing time
vertx.setTimer(30L, handler -> req.response().setChunked(true).putHeader("Content-Type", "text/plain").write(SERVER_RESPONSE).end());
}).listen(9195, "127.0.0.1", r -> {
if (r.failed()) {
ctx.fail(r.cause());
} else {
serverReady.complete();
}
});
serverReady.awaitSuccess();
}
use of io.vertx.core.http.HttpServer in project vertx-web by vert-x3.
the class OAuth2AuthHandlerTest method testPasswordFlow.
@Test
public void testPasswordFlow() throws Exception {
// lets mock a oauth2 server using code auth code flow
OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.PASSWORD, new OAuth2ClientOptions().setClientID("client-id").setClientSecret("client-secret").setSite("http://localhost:10000"));
final CountDownLatch latch = new CountDownLatch(1);
HttpServer server = vertx.createHttpServer().requestHandler(req -> {
if (req.method() == HttpMethod.POST && "/oauth/token".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> {
final String queryString = buffer.toString();
assertTrue(queryString.contains("username=paulo"));
assertTrue(queryString.contains("password=bananas"));
assertTrue(queryString.contains("grant_type=password"));
req.response().putHeader("Content-Type", "application/json").end(fixture.encode());
});
} else if (req.method() == HttpMethod.POST && "/oauth/revoke".equals(req.path())) {
req.setExpectMultipart(true).bodyHandler(buffer -> req.response().end());
} else {
req.response().setStatusCode(400).end();
}
}).listen(10000, ready -> {
if (ready.failed()) {
throw new RuntimeException(ready.cause());
}
// ready
latch.countDown();
});
latch.await();
AuthHandler oauth2Handler = BasicAuthHandler.create(oauth2);
// protect everything under /protected
router.route("/protected/*").handler(oauth2Handler);
// mount some handler under the protected zone
router.route("/protected/somepage").handler(rc -> {
assertNotNull(rc.user());
rc.response().end("Welcome to the protected resource!");
});
testRequest(HttpMethod.GET, "/protected/somepage", req -> req.putHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("paulo:bananas".getBytes())), res -> {
// in this case we should get the resource
}, 200, "OK", "Welcome to the protected resource!");
testRequest(HttpMethod.GET, "/protected/somepage", 401, "Unauthorized");
server.close();
}
use of io.vertx.core.http.HttpServer in project vertx-web by vert-x3.
the class WebClientTest method testTLS.
private void testTLS(WebClientOptions clientOptions, HttpServerOptions serverOptions, Function<WebClient, HttpRequest<Buffer>> requestProvider, Consumer<HttpServerRequest> serverAssertions) throws Exception {
WebClient sslClient = WebClient.create(vertx, clientOptions);
HttpServer sslServer = vertx.createHttpServer(serverOptions);
sslServer.requestHandler(req -> {
assertEquals(serverOptions.isSsl(), req.isSSL());
if (serverAssertions != null) {
serverAssertions.accept(req);
}
req.response().end();
});
try {
startServer(sslServer);
HttpRequest<Buffer> builder = requestProvider.apply(sslClient);
builder.send(onSuccess(resp -> testComplete()));
await();
} finally {
sslClient.close();
sslServer.close();
}
}
use of io.vertx.core.http.HttpServer in project vertx-web by vert-x3.
the class WebExamples method example2.
public void example2(Vertx vertx) {
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route().handler(routingContext -> {
// This handler will be called for every request
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "text/plain");
// Write to the response and end it
response.end("Hello World from Vert.x-Web!");
});
server.requestHandler(router).listen(8080);
}
use of io.vertx.core.http.HttpServer in project vertx-web by vert-x3.
the class SockJSHandlerImpl method main.
// For debug only
// The sockjs-protocol tests must be run against the tests in the 0.3.3 tag of sockjs-protocol !!
public static void main(String[] args) throws Exception {
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
installTestApplications(router, vertx);
server.requestHandler(router).listen(8081);
}
Aggregations