@Test public void routeMissingBecauseOfBadMethod() throws Exception { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder().route(HttpMethod.GET).on("/foo").to(controller, "foo"))); router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.PUT, "/foo").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.DELETE, "/foo").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.POST, "/foo").isUnbound()).isTrue(); }
@Test public void unbindTest() { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder().route(HttpMethod.GET).on("/foo/{path+}").to(controller, "foo"))); router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar").isUnbound()).isFalse(); router.unbindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar").isUnbound()).isTrue(); }
@Test public void routeWithPathParameter() throws Exception { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder().route(HttpMethod.GET).on("/foo/{id}").to(controller, "foo"))); router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/").isUnbound()).isTrue(); Route route = router.getRouteFor(HttpMethod.GET, "/foo/test"); assertThat(route.isUnbound()).isFalse(); assertThat(route.getPathParametersEncoded("/foo/test").get("id")).isEqualToIgnoringCase("test"); }
/** Test made to reproduce #248. */ @Test public void routeWithRegex() { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder().route(HttpMethod.GET).on("/{type<[0-9]+>}").to(controller, "foo"))); router.bindController(controller); Route route = router.getRouteFor(HttpMethod.GET, "/99"); assertThat(route).isNotNull(); assertThat(route.getPathParametersEncoded("/99").get("type")).isEqualToIgnoringCase("99"); route = router.getRouteFor(HttpMethod.GET, "/xx"); assertThat(route.isUnbound()).isTrue(); }
@Test public void subRoute() throws Exception { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder().route(HttpMethod.GET).on("/foo/*").to(controller, "foo"))); router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar")).isNotNull(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar").getControllerObject()) .isEqualTo(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar/baz")).isNotNull(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar/baz").getControllerObject()) .isEqualTo(controller); }
@Test public void subRouteAsParameter() throws Exception { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder().route(HttpMethod.GET).on("/foo/{path+}").to(controller, "foo"))); router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar").isUnbound()).isFalse(); assertThat(router.getRouteFor(HttpMethod.GET, "/foo/bar").getControllerObject()) .isEqualTo(controller); Route route = router.getRouteFor(HttpMethod.GET, "/foo/bar/baz"); assertThat(route).isNotNull(); assertThat(route.getControllerObject()).isEqualTo(controller); assertThat(route.getPathParametersEncoded("/foo/bar/baz").get("path")) .isEqualToIgnoringCase("bar/baz"); }
@Test public void routeMissingBecauseOfBrokenMethod() throws Exception { Controller controller = new DefaultController() { @org.wisdom.api.annotations.Route(method = HttpMethod.GET, uri = "/foo") public String hello() { return "hello"; } @org.wisdom.api.annotations.Route(method = HttpMethod.GET, uri = "/bar") public Result hello2() { return ok("hello"); } }; // Must not trow an exception. router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo").isUnbound()).isTrue(); assertThat(router.getRouteFor(HttpMethod.GET, "/bar").isUnbound()).isTrue(); controller = new DefaultController() { @org.wisdom.api.annotations.Route(method = HttpMethod.GET, uri = "/foo") public Result hello() { return ok("hello"); } @org.wisdom.api.annotations.Route(method = HttpMethod.GET, uri = "/bar") public Result hello2() { return ok("hello"); } }; // Must not trow an exception. router.bindController(controller); assertThat(router.getRouteFor(HttpMethod.GET, "/foo").isUnbound()).isFalse(); assertThat(router.getRouteFor(HttpMethod.GET, "/bar").isUnbound()).isFalse(); }
@Test public void routeWithTwoPathParameters() throws Exception { FakeController controller = new FakeController(); controller.setRoutes( ImmutableList.of( new RouteBuilder() .route(HttpMethod.GET) .on("/foo/{id}/{email}") .to(controller, "foo"))); router.bindController(controller); Route route = router.getRouteFor(HttpMethod.GET, "/foo/1234/[email protected]"); assertThat(route).isNotNull(); assertThat(route.getPathParametersEncoded("/foo/1234/[email protected]").get("id")) .isEqualToIgnoringCase("1234"); assertThat(route.getPathParametersEncoded("/foo/1234/[email protected]").get("email")) .isEqualToIgnoringCase("*****@*****.**"); }