VWeb/Tests/Router.cpp

84 lines
2.2 KiB
C++
Raw Permalink Normal View History

#include "Includes/Router.h"
#include "Includes/Request.h"
#include "Includes/Response.h"
#include <catch2/catch_test_macros.hpp>
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> request = CreateRef<Request>();
request->URI = "/test";
router.Register<Route>("/test");
REQUIRE(router.FindRoute(request) != nullptr);
}
TEST_CASE("route_that_not_exists_returns_nullptr", "[Router]") {
Router router;
Ref<Request> request = CreateRef<Request>();
request->URI = "/testre";
router.Register<Route>("/test");
REQUIRE(router.FindRoute(request) == nullptr);
}
TEST_CASE("route_matches_exactly", "[Router]") {
Router router;
Ref<Request> request = CreateRef<Request>();
request->URI = "/test/id";
router.Register<FailureRoute>("/test/:id");
router.Register<Route>("/test/id");
auto route = router.FindRoute(request);
REQUIRE(route);
REQUIRE(route->IsAllowed(*request));
}
TEST_CASE("route_can_handle_variants", "[Router]") {
Router router;
Ref<Request> request = CreateRef<Request>();
request->URI = "/test/1";
router.Register<FailureRoute>("/test/id");
router.Register<Route>("/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> request = CreateRef<Request>();
request->URI = "/test/id";
router.Register<FailureRoute>("/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> request = CreateRef<Request>();
request->URI = "/test/1";
router.Register<FailureRoute>("/test/:id");
router.Get("/test/id", [](Request &, Response &) {
return true;
});
auto route = router.FindRoute(request);
REQUIRE(route);
REQUIRE_FALSE(route->IsAllowed(*request));
}
} // namespace VWeb