Skip to content

Commit

Permalink
add more methods (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Feb 2, 2024
1 parent e8f41be commit 018a180
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
11 changes: 11 additions & 0 deletions include/cinatra/coro_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <any>
#include <charconv>
#include <initializer_list>
#include <optional>
#include <regex>
#include <string>

#include "async_simple/coro/Lazy.h"
#include "define.h"
Expand Down Expand Up @@ -212,6 +214,15 @@ class coro_http_request {
aspect_data_.push_back(std::move(data));
}

void set_aspect_data(std::vector<std::string> data) {
aspect_data_ = std::move(data);
}

template <typename... Args>
void set_aspect_data(Args... args) {
(aspect_data_.push_back(std::move(args)), ...);
}

std::vector<std::string> &get_aspect_data() { return aspect_data_; }

std::unordered_map<std::string_view, std::string_view> get_cookies(
Expand Down
22 changes: 17 additions & 5 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ struct check_t : public base_aspect {
}
};

struct get_data : public base_aspect {
bool before(coro_http_request &req, coro_http_response &res) {
req.set_aspect_data("hello", "world");
return true;
}
};

TEST_CASE("test aspects") {
coro_http_server server(1, 9001);
server.set_static_res_dir("", "");
Expand All @@ -532,19 +539,24 @@ TEST_CASE("test aspects") {
server.set_http_handler<GET, POST>(
"/",
[](coro_http_request &req, coro_http_response &resp) {
resp.add_header("aaaa", "bbcc");
resp.set_status_and_content(status_type::ok, "ok");
},
{std::make_shared<log_t>(), std::make_shared<check_t>()});

server.set_http_handler<GET, POST>(
"/coro",
"/aspect",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
auto &val = req.get_aspect_data();
CHECK(val[0] == "hello");
CHECK(val[1] == "world");
resp.set_status_and_content(status_type::ok, "ok");
co_return;
},
{std::make_shared<log_t>(), std::make_shared<check_t>()});
{std::make_shared<get_data>()});
server.async_start();
std::this_thread::sleep_for(300ms);

coro_http_client client{};
auto result = client.get("http://127.0.0.1:9001/");
Expand All @@ -564,14 +576,14 @@ TEST_CASE("test aspects") {

check(result);

result = client.get("http://127.0.0.1:9001/coro");
check(result);

result = client.get("http://127.0.0.1:9001/test_aspect.txt");
CHECK(result.status == 200);

result = client.get("http://127.0.0.1:9001/test_file.txt");
CHECK(result.status == 200);

result = client.get("http://127.0.0.1:9001/aspect");
CHECK(result.status == 200);
}

TEST_CASE("use out context") {
Expand Down

0 comments on commit 018a180

Please sign in to comment.