@Test(expected = IllegalArgumentException.class) public void testInvalidMountPoint1() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath*", subRouter); }
@Test(expected = IllegalArgumentException.class) public void testInvalidMountPoint2() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath/*", subRouter); }
@Test(expected = IllegalArgumentException.class) public void testSubRoutePattern() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/foo/:abc/bar", subRouter); }
@Test(expected = IllegalArgumentException.class) public void testInvalidMountPoint3() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("subpath", subRouter); }
@Test public void testSubRouterNPE() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/", subRouter); testRequest(HttpMethod.GET, "foo", 404, "Not Found"); }
@Test public void testUnhandledRuntimeException() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo").handler(rc -> { throw new RuntimeException("Balderdash!"); }); testRequest(HttpMethod.GET, "/subpath/foo", 500, "Internal Server Error"); }
@Test public void testStackOverflow() throws Exception { router.get("/files/:id/info").handler(ctx -> ctx.response().end()); router.mountSubRouter("/v1", router); testRequest(HttpMethod.GET, "/v1/files/some-file-id/info", 200, "OK"); testRequest(HttpMethod.GET, "/v1/files//info", 404, "Not Found"); } }
@Test public void testRegexInSubRouter() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api", subRouter); subRouter.routeWithRegex("\\/test").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/test", 200, "sausages"); }
@Test public void testEmptySubrouter() throws Exception { Router subRouter1 = Router.router(vertx); router.mountSubRouter("/foo", subRouter1); testRequest(HttpMethod.GET, "/foo", 404, "Not Found"); testRequest(HttpMethod.GET, "/foo/bar", 404, "Not Found"); testRequest(HttpMethod.GET, "/foo/bar/wibble", 404, "Not Found"); }
@Test public void testServerRelativeToPathAndMountPoint() throws Exception { router.clear(); Router subRouter = Router.router(vertx); subRouter.route("/somedir/*").handler(stat); router.mountSubRouter("/mymount/", subRouter); testRequest(HttpMethod.GET, "/mymount/somedir/otherpage.html", 200, "OK", "<html><body>Other page</body></html>"); }
@Test public void tesHandledRuntimeException2() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> { throw new RuntimeException("Balderdash!"); }); subRouter.route("/foo/*").failureHandler(rc -> { assertEquals(-1, rc.statusCode()); assertEquals("Balderdash!", rc.failure().getMessage()); rc.response().setStatusCode(555).setStatusMessage("Badgers").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 555, "Badgers"); }
@Test public void testNormalised3() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api", subRouter); subRouter.route("/").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/", 200, "sausages"); testRequest(HttpMethod.GET, "/api", 200, "sausages"); testRequest(HttpMethod.GET, "/api///", 200, "sausages"); testRequest(HttpMethod.GET, "//api//", 200, "sausages"); }
@Test public void tesHandledRuntimeException1() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> { throw new RuntimeException("Balderdash!"); }); router.route("/subpath/*").failureHandler(rc -> { assertEquals(-1, rc.statusCode()); assertEquals("Balderdash!", rc.failure().getMessage()); rc.response().setStatusCode(555).setStatusMessage("Badgers").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 555, "Badgers"); }
@Test public void testNormalised4() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api/", subRouter); subRouter.route("/").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/", 200, "sausages"); testRequest(HttpMethod.GET, "/api", 200, "sausages"); testRequest(HttpMethod.GET, "/api///", 200, "sausages"); testRequest(HttpMethod.GET, "//api//", 200, "sausages"); }
@Test public void testNormalised1() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api", subRouter); subRouter.route("/foo").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/foo", 200, "sausages"); testRequest(HttpMethod.GET, "/api/foo/", 200, "sausages"); testRequest(HttpMethod.GET, "/api/foo//", 200, "sausages"); testRequest(HttpMethod.GET, "//api//foo//", 200, "sausages"); testRequest(HttpMethod.GET, "//api//foo///", 200, "sausages"); }
@Test public void testNormalised2() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/api/", subRouter); subRouter.route("/foo").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/api/foo", 200, "sausages"); testRequest(HttpMethod.GET, "/api/foo/", 200, "sausages"); testRequest(HttpMethod.GET, "/api/foo//", 200, "sausages"); testRequest(HttpMethod.GET, "//api//foo//", 200, "sausages"); testRequest(HttpMethod.GET, "//api//foo///", 200, "sausages"); }
@Test public void testSimple() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo").handler(rc -> { assertEquals("/subpath", rc.mountPoint()); rc.response().setStatusMessage(rc.request().path()).end(); }); testRequest(HttpMethod.GET, "/subpath/foo", 200, "/subpath/foo"); testRequest(HttpMethod.GET, "/subpath/", 404, "Not Found"); testRequest(HttpMethod.GET, "/subpath/bar", 404, "Not Found"); testRequest(HttpMethod.GET, "/otherpath", 404, "Not Found"); }
@Test public void testTrailingSlash() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath/", subRouter); subRouter.route("/foo").handler(rc -> { assertEquals("/subpath", rc.mountPoint()); rc.response().setStatusMessage(rc.request().path()).end(); }); testRequest(HttpMethod.GET, "/subpath/foo", 200, "/subpath/foo"); testRequest(HttpMethod.GET, "/subpath/", 404, "Not Found"); testRequest(HttpMethod.GET, "/subpath/bar", 404, "Not Found"); testRequest(HttpMethod.GET, "/otherpath", 404, "Not Found"); }
@Test public void testFailCalled1() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> rc.fail(557)); router.route("/subpath/*").failureHandler(rc -> { assertEquals(557, rc.statusCode()); assertNull(rc.failure()); rc.response().setStatusCode(rc.statusCode()).setStatusMessage("Chipmunks").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 557, "Chipmunks"); }
@Test public void testFailCalled2() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> rc.fail(557)); router.route("/subpath/*").failureHandler(rc -> { assertEquals(557, rc.statusCode()); assertNull(rc.failure()); rc.response().setStatusCode(rc.statusCode()).setStatusMessage("Chipmunks").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 557, "Chipmunks"); }