Skip to content

Commit 17f6fd9

Browse files
committed
added testcase for catch_all handler called and check request content in it.
1 parent 2d06fe7 commit 17f6fd9

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

‎tests/unittest.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,64 @@ TEST_CASE("catchall")
22912291
}
22922292
} // catchall
22932293

2294+
2295+
TEST_CASE("catchall_check_full_handling")
2296+
{
2297+
struct NotLocalMiddleware {
2298+
bool before_handle_called{false};
2299+
bool after_handle_called{false};
2300+
2301+
struct context {
2302+
};
2303+
2304+
void before_handle(request& /*req*/, response& /*res*/, context& /*ctx*/) {
2305+
before_handle_called = true;
2306+
}
2307+
2308+
void after_handle(request& /*req*/, response& /*res*/, context& /*ctx*/) {
2309+
after_handle_called = true;
2310+
}
2311+
};
2312+
2313+
App<NotLocalMiddleware> app;
2314+
bool headers_empty=true;
2315+
CROW_ROUTE(app, "/")
2316+
([]() {
2317+
return "works!";
2318+
});
2319+
2320+
CROW_CATCHALL_ROUTE(app) ([&headers_empty](const crow::request& req,response& res) {
2321+
headers_empty = req.headers.empty();
2322+
auto req_h = req.headers;
2323+
auto res_h = res.headers;
2324+
res.add_header("bla","bla_val");
2325+
res.body = "bla_body";
2326+
2327+
});
2328+
2329+
app.validate();
2330+
2331+
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).server_name("lol").run_async();
2332+
app.wait_for_server_start();
2333+
2334+
{
2335+
// call not handled url to get response from catch_all handler
2336+
auto resp = HttpClient::request(LOCALHOST_ADDRESS, 45451,
2337+
"GET /catch_all HTTP/1.0\r\nHost: localhost\r\n\r\n");
2338+
2339+
CHECK(resp.find("bla") != std::string::npos);
2340+
2341+
auto global_middleware = app.get_middleware<NotLocalMiddleware>();
2342+
CHECK(global_middleware.after_handle_called==true);
2343+
CHECK(global_middleware.before_handle_called==true);
2344+
CHECK(headers_empty==false);
2345+
2346+
}
2347+
2348+
app.stop();
2349+
} // local_middleware
2350+
2351+
22942352
TEST_CASE("blueprint")
22952353
{
22962354
SimpleApp app;

0 commit comments

Comments
 (0)