#include "Includes/Router.h" #include "Includes/Request.h" #include "Includes/Response.h" #include namespace VWeb { class FailureRoute final : public Route { public: bool IsAllowed(Request &request) override { return false; } }; TEST_CASE("route_that_exists_returns_route", "[Router]") { Router router; Ref request = CreateRef(); request->URI = "/test"; router.Register("/test"); REQUIRE(router.FindRoute(request) != nullptr); } TEST_CASE("route_that_not_exists_returns_nullptr", "[Router]") { Router router; Ref request = CreateRef(); request->URI = "/testre"; router.Register("/test"); REQUIRE(router.FindRoute(request) == nullptr); } TEST_CASE("route_matches_exactly", "[Router]") { Router router; Ref request = CreateRef(); request->URI = "/test/id"; router.Register("/test/:id"); router.Register("/test/id"); auto route = router.FindRoute(request); REQUIRE(route); REQUIRE(route->IsAllowed(*request)); } TEST_CASE("route_can_handle_variants", "[Router]") { Router router; Ref request = CreateRef(); request->URI = "/test/1"; router.Register("/test/id"); router.Register("/test/:id"); auto route = router.FindRoute(request); REQUIRE(route); REQUIRE(route->IsAllowed(*request)); } TEST_CASE("router_returns_function_instead_of_class", "[Router]") { Router router; Ref request = CreateRef(); request->URI = "/test/id"; router.Register("/test/:id"); router.Get("/test/id", [](Request &, Response &) { return true; }); auto route = router.FindRoute(request); REQUIRE(route); REQUIRE(route->IsAllowed(*request)); } TEST_CASE("router_returns_class_if_function_is_provided", "[Router]") { Router router; Ref request = CreateRef(); request->URI = "/test/1"; router.Register("/test/:id"); router.Get("/test/id", [](Request &, Response &) { return true; }); auto route = router.FindRoute(request); REQUIRE(route); REQUIRE_FALSE(route->IsAllowed(*request)); } } // namespace VWeb