Search in sources :

Example 16 with HttpServer

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();
}
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) VertxPrometheusOptions(io.vertx.micrometer.VertxPrometheusOptions) Async(io.vertx.ext.unit.Async) VertxOptions(io.vertx.core.VertxOptions) MicrometerMetricsOptions(io.vertx.micrometer.MicrometerMetricsOptions) Before(org.junit.Before)

Example 17 with HttpServer

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();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) HttpMethod(io.vertx.core.http.HttpMethod) HttpServer(io.vertx.core.http.HttpServer) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) OAuth2FlowType(io.vertx.ext.auth.oauth2.OAuth2FlowType) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) OAuth2ClientOptions(io.vertx.ext.auth.oauth2.OAuth2ClientOptions) HttpServer(io.vertx.core.http.HttpServer) CountDownLatch(java.util.concurrent.CountDownLatch) OAuth2Auth(io.vertx.ext.auth.oauth2.OAuth2Auth) Test(org.junit.Test)

Example 18 with HttpServer

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();
    }
}
Also used : Buffer(io.vertx.core.buffer.Buffer) VertxException(io.vertx.core.VertxException) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Arrays(java.util.Arrays) DecodeException(io.vertx.core.json.DecodeException) HttpServer(io.vertx.core.http.HttpServer) MultiMap(io.vertx.core.MultiMap) BodyCodec(io.vertx.ext.web.codec.BodyCodec) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Cert(io.vertx.test.core.tls.Cert) AddressResolverOptions(io.vertx.core.dns.AddressResolverOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpTestBase(io.vertx.test.core.HttpTestBase) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WriteStream(io.vertx.core.streams.WriteStream) ReadStream(io.vertx.core.streams.ReadStream) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) ConnectException(java.net.ConnectException) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpConnection(io.vertx.core.http.HttpConnection) ProxyOptions(io.vertx.core.net.ProxyOptions) OpenOptions(io.vertx.core.file.OpenOptions) Files(java.nio.file.Files) VertxOptions(io.vertx.core.VertxOptions) HttpHeaders(io.vertx.core.http.HttpHeaders) Test(org.junit.Test) File(java.io.File) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) ProxyType(io.vertx.core.net.ProxyType) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerResponse(io.vertx.core.http.HttpServerResponse) WineAndCheese(io.vertx.ext.web.client.jackson.WineAndCheese) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) Collections(java.util.Collections) HttpServer(io.vertx.core.http.HttpServer)

Example 19 with HttpServer

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);
}
Also used : HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer)

Example 20 with HttpServer

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);
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) Vertx(io.vertx.core.Vertx)

Aggregations

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