Skip to content

Commit 3b81e16

Browse files
Validate if the CROW_ROUTE starts with a '/' (#559) (#597)
1 parent f6cfa84 commit 3b81e16

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

include/crow/routing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ namespace crow
649649

650650
void validate() override
651651
{
652+
if (rule_.at(0) != '/')
653+
throw std::runtime_error("Internal error: Routes must start with a '/'");
654+
652655
if (!handler_)
653656
{
654657
throw std::runtime_error(name_ + (!name_.empty() ? ": " : "") + "no handler for url " + rule_);

tests/unittest.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <sys/stat.h>
55

66
#include <iostream>
7-
#include <sstream>
87
#include <vector>
98
#include <thread>
109
#include <chrono>
@@ -149,6 +148,27 @@ TEST_CASE("PathRouting")
149148
}
150149
} // PathRouting
151150

151+
TEST_CASE("InvalidPathRouting")
152+
{
153+
SimpleApp app;
154+
155+
CROW_ROUTE(app, "invalid_route")
156+
([] {
157+
return "should not arrive here";
158+
});
159+
160+
try
161+
{
162+
app.validate();
163+
FAIL_CHECK();
164+
}
165+
catch (std::exception& e)
166+
{
167+
auto expected_exception_text = "Internal error: Routes must start with a '/'";
168+
CHECK(strcmp(expected_exception_text, e.what()) == 0);
169+
}
170+
} // InvalidPathRouting
171+
152172
TEST_CASE("RoutingTest")
153173
{
154174
SimpleApp app;

0 commit comments

Comments
 (0)