Skip to content

Commit a1b3b17

Browse files
committed
added testcase for catch_all handler called and check request content in it.
1 parent cadd7c4 commit a1b3b17

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

tests/unittest.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,67 @@ TEST_CASE("catchall")
36123612
}
36133613
} // catchall
36143614

3615+
3616+
TEST_CASE("catchall_check_full_handling")
3617+
{
3618+
struct LocalSecretMiddleware : crow::ILocalMiddleware
3619+
{
3620+
struct context {
3621+
bool before_handle_called{false};
3622+
bool after_handle_called{false};
3623+
};
3624+
3625+
void before_handle(request& /*req*/, response& res, context& ctx)
3626+
{
3627+
ctx.before_handle_called = true;
3628+
3629+
res.code = 403;
3630+
res.end();
3631+
}
3632+
3633+
void after_handle(request& /*req*/, response& /*res*/, context& ctx) {
3634+
ctx.after_handle_called = true;
3635+
3636+
}
3637+
};
3638+
3639+
App<LocalSecretMiddleware> app;
3640+
bool headers_empty=true;
3641+
CROW_ROUTE(app, "/")
3642+
([]() {
3643+
return "works!";
3644+
});
3645+
3646+
CROW_CATCHALL_ROUTE(app) ([&headers_empty](const crow::request& req,response& res) {
3647+
headers_empty = req.headers.empty();
3648+
res.add_header("bla","bla_val");
3649+
res.body = "bla_body";
3650+
3651+
});
3652+
3653+
CROW_ROUTE(app, "/secret")
3654+
.middlewares<decltype(app), LocalSecretMiddleware>()([]() {
3655+
return "works!";
3656+
});
3657+
3658+
app.validate();
3659+
3660+
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async();
3661+
app.wait_for_server_start();
3662+
3663+
{
3664+
// call not handled url to get response from catch_all handler
3665+
auto resp = HttpClient::request(LOCALHOST_ADDRESS, 45451,
3666+
"GET /catch_all\r\n\r\n");
3667+
3668+
CHECK(resp.find("bla") != std::string::npos);
3669+
CHECK(headers_empty==false);
3670+
}
3671+
3672+
app.stop();
3673+
} // local_middleware
3674+
3675+
36153676
TEST_CASE("blueprint")
36163677
{
36173678
SimpleApp app;

0 commit comments

Comments
 (0)