@Override public void reroute(HttpMethod method, String path) { decoratedContext.reroute(method, path); }
@Override public void reroute(HttpMethod method, String path) { inner.reroute(method, path); }
/** * Restarts the current router with a new path and reusing the original method. All path parameters are then parsed * and available on the params list. * * @param path the new http path. */ default void reroute(String path) { reroute(request().method(), path); }
} else { ctx.reroute(state != null ? state : "/");
@Test public void testRerouteWithParams() throws Exception { router.get("/other").handler(ctx -> ctx.response().end("/other")); router.get("/base").handler(ctx -> ctx.reroute("/other?paramter1=p1¶meter2=p2")); testRequest(HttpMethod.GET, "/base", 200, "OK", "/other"); } }
@Test public void testReroute() throws Exception { router.get("/users/:name").handler(ctx -> ctx.response().end("/users/:name")); router.get("/me").handler(ctx -> ctx.reroute("/users/paulo")); testRequest(HttpMethod.GET, "/me", 200, "OK", "/users/:name"); }
@Test public void testRerouteMethod() throws Exception { router.post("/me").handler(ctx -> ctx.response().end("POST")); router.get("/me").handler(ctx -> ctx.reroute(HttpMethod.POST, "/me")); testRequest(HttpMethod.GET, "/me", 200, "OK", "POST"); }
@Test public void testRerouteReparse() throws Exception { router.get("/users/:name").handler(ctx -> ctx.response().end(ctx.request().getParam("name"))); router.get("/me").handler(ctx -> ctx.reroute("/users/paulo")); testRequest(HttpMethod.GET, "/me", 200, "OK", "paulo"); }
@Test public void testRerouteClearHeader() throws Exception { router.get("/users/:name").handler(ctx -> ctx.response().end("/users/:name")); router.get("/me").handler(ctx -> { ctx.response().putHeader("X-woop", "durp"); ctx.reroute("/users/paulo"); }); testRequest(HttpMethod.GET, "/me", null, res -> assertNull(res.getHeader("X-woop")), 200, "OK", "/users/:name"); }
@Test public void testPathParamsWithReroute() throws Exception { String paramName = "param"; String firstParamValue = "fpv"; String secondParamValue = "secondParamValue"; router.route("/first/:" + paramName + "/route").handler(rc -> { assertEquals(firstParamValue, rc.pathParam(paramName)); rc.reroute(HttpMethod.GET, "/second/" + secondParamValue + "/route"); }); router.route("/second/:" + paramName + "/route").handler(rc -> rc.response().setStatusMessage(rc.pathParam(paramName)).end()); testRequest(HttpMethod.GET, "/first/" + firstParamValue + "/route", 200, secondParamValue); }
@Test public void testDuplicateParams2() throws Exception { router.route("/test/:p").handler(RoutingContext::next); router.route("/test/:p").handler(ctx -> ctx.reroute("/done/abc/cde")); router.route("/done/:a/:p").handler(routingContext -> { assertEquals(1, routingContext.request().params().getAll("p").size()); assertEquals("cde", routingContext.request().getParam("p")); routingContext.response().end(); }); testRequest(HttpMethod.GET, "/test/abc", 200, "OK"); }
@Test public void testRerouteWithBody() throws Exception { router.route("/test/*").handler(BodyHandler.create()); router.route("/test/v1").handler(ctx -> ctx.reroute("/test/v2")); router.route("/test/v2").handler(ctx -> ctx.response().end()); testRequest(HttpMethod.POST, "/test/v1", req -> { req.setChunked(true); req.write("Test HTTP Body"); }, 200, "OK", null); }
@Test public void testRerouteClearHeader2() throws Exception { router.get("/users/:name").handler(ctx -> { ctx.response().putHeader("X-woop", "durp2"); ctx.response().end("/users/:name"); }); router.get("/me").handler(ctx -> { ctx.response().putHeader("X-woop", "durp"); ctx.reroute("/users/paulo"); }); testRequest(HttpMethod.GET, "/me", null, res -> assertEquals("durp2", res.getHeader("X-woop")), 200, "OK", "/users/:name"); }
@Test public void testRerouteFailure() throws Exception { router.get("/error/400").handler(ctx -> ctx.response() .setStatusCode(400) .end("/error/400")); router.get("/me").handler(ctx -> ctx.fail(400)); router.get().failureHandler(ctx -> { if (ctx.statusCode() == 400) { ctx.reroute("/error/400"); } else { ctx.next(); } }); testRequest(HttpMethod.GET, "/me", 400, "Bad Request", "/error/400"); }
@Test public void testRerouteClearHeader3() throws Exception { router.route().handler(CookieHandler.create()); router.get("/users/:name").handler(ctx -> { ctx.response().putHeader("X-woop", "durp2"); ctx.response().end("/users/:name"); }); router.get("/me").handler(ctx -> { ctx.response().putHeader("X-woop", "durp"); ctx.reroute("/users/paulo"); }); testRequest(HttpMethod.GET, "/me", null, res -> { assertEquals("durp2", res.getHeader("X-woop")); assertNull(res.getHeader("Cookie")); }, 200, "OK", "/users/:name"); }
router.route().handler(BodyHandler.create(false).setMergeFormAttributes(true)); router.route("/toBeRerouted").handler(rc -> { rc.reroute("/rerouted"); }); router.route("/rerouted").handler(rc -> {
@Test public void testReroute() throws Exception { router.get("/users/:name").handler(ctx -> ctx.response().end("/users/:name")); router.get("/me").handler(ctx -> ctx.reroute("/users/paulo")); testRequest(HttpMethod.GET, "/me", 200, "OK", "/users/:name"); }
@Test public void testRerouteWithParams() throws Exception { router.get("/other").handler(ctx -> ctx.response().end("/other")); router.get("/base").handler(ctx -> ctx.reroute("/other?paramter1=p1¶meter2=p2")); testRequest(HttpMethod.GET, "/base", 200, "OK", "/other"); } }
@Test public void testPathParamsWithReroute() throws Exception { String paramName = "param"; String firstParamValue = "fpv"; String secondParamValue = "secondParamValue"; router.route("/first/:" + paramName + "/route").handler(rc -> { assertEquals(firstParamValue, rc.pathParam(paramName)); rc.reroute(HttpMethod.GET, "/second/" + secondParamValue + "/route"); }); router.route("/second/:" + paramName + "/route").handler(rc -> rc.response().setStatusMessage(rc.pathParam(paramName)).end()); testRequest(HttpMethod.GET, "/first/" + firstParamValue + "/route", 200, secondParamValue); }
@Test public void testRerouteClearHeader2() throws Exception { router.get("/users/:name").handler(ctx -> { ctx.response().putHeader("X-woop", "durp2"); ctx.response().end("/users/:name"); }); router.get("/me").handler(ctx -> { ctx.response().putHeader("X-woop", "durp"); ctx.reroute("/users/paulo"); }); testRequest(HttpMethod.GET, "/me", null, res -> assertEquals("durp2", res.getHeader("X-woop")), 200, "OK", "/users/:name"); }