Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix case #673

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions include/cinatra/coro_http_router.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class coro_http_router {
if (ok) {
co_await handler(req, resp);
}
ok = true;
(do_after(asps, req, resp, ok), ...);
};
}
Expand Down Expand Up @@ -113,6 +114,7 @@ class coro_http_router {
if (ok) {
handler(req, resp);
}
ok = true;
(do_after(asps, req, resp, ok), ...);
};
}
Expand Down Expand Up @@ -155,20 +157,17 @@ class coro_http_router {
}
ok = aspect.before(req, resp);
}
else {
ok = true;
}
}

template <typename T>
void do_after(T& aspect, coro_http_request& req, coro_http_response& resp,
bool& ok) {
if constexpr (has_after_v<T>) {
if (!ok) {
return;
}
ok = aspect.after(req, resp);
}
else {
ok = true;
}
}

std::function<void(coro_http_request& req, coro_http_response& resp)>*
Expand Down
28 changes: 25 additions & 3 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,32 @@ struct add_more_data {
}
};

std::vector<std::string> aspect_test_vec;

struct auth_t {
bool before(coro_http_request &req, coro_http_response &res) { return true; }
bool after(coro_http_request &req, coro_http_response &res) {
aspect_test_vec.push_back("enter auth_t after");
return false;
}
};

struct dely_t {
bool before(coro_http_request &req, coro_http_response &res) {
res.set_status_and_content(status_type::unauthorized, "unauthorized");
return false;
}
bool after(coro_http_request &req, coro_http_response &res) {
aspect_test_vec.push_back("enter delay_t after");
return true;
}
};

struct another_t {
bool after(coro_http_request &req, coro_http_response &res) {
// won't comming
return true;
}
};

TEST_CASE("test aspect") {
Expand Down Expand Up @@ -594,7 +611,7 @@ TEST_CASE("test aspect") {
[](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
},
dely_t{}, auth_t{});
dely_t{}, auth_t{}, another_t{});
server.set_http_handler<GET>(
"/exception", [](coro_http_request &req, coro_http_response &resp) {
throw std::invalid_argument("invalid argument");
Expand Down Expand Up @@ -628,6 +645,7 @@ TEST_CASE("test aspect") {
CHECK(result.status == 200);
result = async_simple::coro::syncAwait(client.async_get("/auth"));
CHECK(result.status == 401);
CHECK(aspect_test_vec.size() == 2);
CHECK(result.resp_body == "unauthorized");
result = async_simple::coro::syncAwait(client.async_get("/exception"));
CHECK(result.status == 503);
Expand Down Expand Up @@ -2341,7 +2359,9 @@ TEST_CASE("test multipart and chunked return error") {
std::string uri1 = "http://127.0.0.1:8090/chunked";
auto result = async_simple::coro::syncAwait(
client.async_upload_chunked(uri1, http_method::PUT, filename));
CHECK(result.resp_body == "invalid headers");
CHECK(result.status != 200);
if (!result.resp_body.empty())
CHECK(result.resp_body == "invalid headers");
}

{
Expand All @@ -2350,7 +2370,9 @@ TEST_CASE("test multipart and chunked return error") {
client.add_str_part("test", "test value");
auto result =
async_simple::coro::syncAwait(client.async_upload_multipart(uri2));
CHECK(result.resp_body == "invalid headers");
CHECK(result.status != 200);
if (!result.resp_body.empty())
CHECK(result.resp_body == "invalid headers");
}

{
Expand Down
Loading