Search in sources :

Example 61 with HttpServer

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

the class StaticHandlerTest method testNoHttp2Push.

@Test
public void testNoHttp2Push() throws Exception {
    stat.setWebRoot("webroot/somedir3");
    router.route().handler(stat);
    HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem")));
    http2Server.requestHandler(router).listen(8443);
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem"));
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(HttpVersion.HTTP_2, resp.version());
        resp.bodyHandler(this::assertNotNull);
        testComplete();
    });
    request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> {
        fail();
    }));
    request.end();
    await();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Utils(io.vertx.ext.web.impl.Utils) HttpVersion(io.vertx.core.http.HttpVersion) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) WebTestBase(io.vertx.ext.web.WebTestBase) DateFormat(java.text.DateFormat) Set(java.util.Set) Test(org.junit.Test) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 62 with HttpServer

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

the class StaticHandlerTest method testHttp2Push.

@Test
public void testHttp2Push() throws Exception {
    List<Http2PushMapping> mappings = new ArrayList<>();
    mappings.add(new Http2PushMapping("style.css", "style", false));
    mappings.add(new Http2PushMapping("coin.png", "image", false));
    stat.setHttp2PushMapping(mappings).setWebRoot("webroot/somedir3");
    router.route().handler(stat);
    HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions().setUseAlpn(true).setSsl(true).setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem")));
    http2Server.requestHandler(router).listen(8443);
    HttpClientOptions options = new HttpClientOptions().setSsl(true).setUseAlpn(true).setProtocolVersion(HttpVersion.HTTP_2).setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem"));
    HttpClient client = vertx.createHttpClient(options);
    HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(HttpVersion.HTTP_2, resp.version());
        resp.bodyHandler(this::assertNotNull);
    });
    CountDownLatch latch = new CountDownLatch(2);
    request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> {
        assertNotNull(pushedResp);
        pushedResp.bodyHandler(this::assertNotNull);
        latch.countDown();
    }));
    request.end();
    latch.await();
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Utils(io.vertx.ext.web.impl.Utils) HttpVersion(io.vertx.core.http.HttpVersion) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) WebTestBase(io.vertx.ext.web.WebTestBase) DateFormat(java.text.DateFormat) Set(java.util.Set) Test(org.junit.Test) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpMethod(io.vertx.core.http.HttpMethod) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientRequest(io.vertx.core.http.HttpClientRequest) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Http2PushMapping(io.vertx.ext.web.Http2PushMapping) HttpClient(io.vertx.core.http.HttpClient) ArrayList(java.util.ArrayList) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) CountDownLatch(java.util.concurrent.CountDownLatch) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Test(org.junit.Test)

Example 63 with HttpServer

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

the class OAuth2AuthHandlerTest method testAuthCodeFlow.

@Test
public void testAuthCodeFlow() throws Exception {
    // lets mock a oauth2 server using code auth code flow
    OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, 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 -> 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();
    // create a oauth2 handler on our domain to the callback: "http://localhost:8080/callback"
    OAuth2AuthHandler oauth2Handler = OAuth2AuthHandler.create(oauth2, "http://localhost:8080/callback");
    // setup the callback handler for receiving the callback
    oauth2Handler.setupCallback(router.route());
    // 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", null, resp -> {
        // in this case we should get a redirect
        redirectURL = resp.getHeader("Location");
        assertNotNull(redirectURL);
    }, 302, "Found", null);
    // fake the redirect
    testRequest(HttpMethod.GET, "/callback?state=/protected/somepage&code=1", null, resp -> {
    }, 200, "OK", "Welcome to the protected resource!");
    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 64 with HttpServer

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

the class ClusteredSessionHandlerTest method testClusteredSession.

@Test
public void testClusteredSession() throws Exception {
    CountDownLatch serversReady = new CountDownLatch(3);
    Router router1 = Router.router(vertices[0]);
    router1.route().handler(CookieHandler.create());
    SessionStore store1 = ClusteredSessionStore.create(vertices[0]);
    router1.route().handler(SessionHandler.create(store1));
    HttpServer server1 = vertices[0].createHttpServer(new HttpServerOptions().setPort(8081).setHost("localhost"));
    server1.requestHandler(router1);
    server1.listen(onSuccess(s -> serversReady.countDown()));
    HttpClient client1 = vertices[0].createHttpClient(new HttpClientOptions());
    Router router2 = Router.router(vertices[1]);
    router2.route().handler(CookieHandler.create());
    SessionStore store2 = ClusteredSessionStore.create(vertices[1]);
    router2.route().handler(SessionHandler.create(store2));
    HttpServer server2 = vertices[1].createHttpServer(new HttpServerOptions().setPort(8082).setHost("localhost"));
    server2.requestHandler(router2);
    server2.listen(onSuccess(s -> serversReady.countDown()));
    HttpClient client2 = vertices[0].createHttpClient(new HttpClientOptions());
    Router router3 = Router.router(vertices[2]);
    router3.route().handler(CookieHandler.create());
    SessionStore store3 = ClusteredSessionStore.create(vertices[2]);
    router3.route().handler(SessionHandler.create(store3));
    HttpServer server3 = vertices[2].createHttpServer(new HttpServerOptions().setPort(8083).setHost("localhost"));
    server3.requestHandler(router3);
    server3.listen(onSuccess(s -> serversReady.countDown()));
    HttpClient client3 = vertices[0].createHttpClient(new HttpClientOptions());
    awaitLatch(serversReady);
    router1.route().handler(rc -> {
        Session sess = rc.session();
        sess.put("foo", "bar");
        stuffSession(sess);
        rc.response().end();
    });
    router2.route().handler(rc -> {
        Session sess = rc.session();
        checkSession(sess);
        assertEquals("bar", sess.get("foo"));
        sess.put("eek", "wibble");
        rc.response().end();
    });
    router3.route().handler(rc -> {
        Session sess = rc.session();
        checkSession(sess);
        assertEquals("bar", sess.get("foo"));
        assertEquals("wibble", sess.get("eek"));
        rc.response().end();
    });
    AtomicReference<String> rSetCookie = new AtomicReference<>();
    testRequestBuffer(client1, HttpMethod.GET, 8081, "/", null, resp -> {
        String setCookie = resp.headers().get("set-cookie");
        rSetCookie.set(setCookie);
    }, 200, "OK", null);
    // FIXME - for now we do an artificial sleep because it's possible the session hasn't been stored properly before
    // the next request hits the server
    // https://github.com/vert-x3/vertx-web/issues/93
    Thread.sleep(1000);
    testRequestBuffer(client2, HttpMethod.GET, 8082, "/", req -> req.putHeader("cookie", rSetCookie.get()), null, 200, "OK", null);
    Thread.sleep(1000);
    testRequestBuffer(client3, HttpMethod.GET, 8083, "/", req -> req.putHeader("cookie", rSetCookie.get()), null, 200, "OK", null);
}
Also used : SessionHandlerTestBase(io.vertx.ext.web.handler.SessionHandlerTestBase) CookieHandler(io.vertx.ext.web.handler.CookieHandler) Session(io.vertx.ext.web.Session) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) HttpServer(io.vertx.core.http.HttpServer) VertxOptions(io.vertx.core.VertxOptions) Router(io.vertx.ext.web.Router) SessionImpl(io.vertx.ext.web.sstore.impl.SessionImpl) Test(org.junit.Test) FakeClusterManager(io.vertx.test.fakecluster.FakeClusterManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Repeat(io.vertx.test.core.Repeat) Buffer(io.vertx.core.buffer.Buffer) TestUtils(io.vertx.test.core.TestUtils) HttpMethod(io.vertx.core.http.HttpMethod) SessionHandler(io.vertx.ext.web.handler.SessionHandler) SomeSerializable(io.vertx.ext.web.handler.SomeSerializable) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClient(io.vertx.core.http.HttpClient) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.ext.web.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Session(io.vertx.ext.web.Session) Test(org.junit.Test)

Example 65 with HttpServer

use of io.vertx.core.http.HttpServer in project vertx-openshift-it by cescoffier.

the class ReceiverVerticle method startHttpServer.

private Future<HttpServer> startHttpServer() {
    Future<HttpServer> future = Future.future();
    Router router = setupRouter();
    vertx.createHttpServer().requestHandler(router::accept).listen(8080, future);
    return future;
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router)

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