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