Skip to content

Commit

Permalink
Fix and tests (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jan 14, 2025
1 parent cf9bc04 commit 6ee8f04
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 88 deletions.
1 change: 1 addition & 0 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class coro_http_response {
boundary_.clear();
has_set_content_ = false;
cookies_.clear();
need_date_ = true;
}

void set_shrink_to_fit(bool r) { need_shrink_every_time_ = r; }
Expand Down
28 changes: 0 additions & 28 deletions include/cinatra/uri.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,34 +261,6 @@ class uri_t {
std::string get_query() const { return std::string(query); }
};

inline std::string url_encode(const std::string &str) {
std::string new_str = "";
char c;
int ic;
const char *chars = str.c_str();
char buf_hex[10];
size_t len = strlen(chars);

for (size_t i = 0; i < len; i++) {
c = chars[i];
ic = c;
// uncomment this if you want to encode spaces with +
/*if (c==' ') new_str += '+';
else */
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
new_str += c;
else {
sprintf(buf_hex, "%X", c);
if (ic < 16)
new_str += "%0";
else
new_str += "%";
new_str += buf_hex;
}
}
return new_str;
}

struct context {
std::string host;
std::string port;
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ endif ()
add_executable(test_http_parse
test_http_parse.cpp
)
if (UNIX)
target_link_libraries(test_http_parse ${ZLIB_LIBRARIES})
endif()

if (ENABLE_SIMD STREQUAL "AARCH64")
if (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
Expand Down
19 changes: 10 additions & 9 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,15 @@ TEST_CASE("test response") {
server.set_http_handler<GET>(
"/empty1", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_content_type<2>();
CHECK(!resp.need_date());
CHECK(resp.need_date());
resp.add_header_span({span.data(), span.size()});

resp.set_status_and_content_view(status_type::ok, "");
});
server.set_http_handler<GET>(
"/empty2", [&](coro_http_request &req, coro_http_response &resp) {
resp.set_content_type<2>();
CHECK(!resp.need_date());
CHECK(resp.need_date());
resp.add_header_span({span.data(), span.size()});

resp.set_status_and_content(status_type::ok, "");
Expand Down Expand Up @@ -1380,9 +1380,11 @@ TEST_CASE("test request with out buffer") {
auto result = async_simple::coro::syncAwait(ret);
bool ok = result.status == 200 || result.status == 301;
CHECK(ok);
std::string_view sv(str.data(), result.resp_body.size());
// CHECK(result.resp_body == sv);
CHECK(client.is_body_in_out_buf());
if (ok && result.resp_body.size() <= 1024 * 64) {
std::string_view sv(str.data(), result.resp_body.size());
// CHECK(result.resp_body == sv);
CHECK(client.is_body_in_out_buf());
}
}
}

Expand Down Expand Up @@ -1480,7 +1482,7 @@ TEST_CASE("test coro_http_client async_http_connect") {

CHECK(r.status >= 200);
r = async_simple::coro::syncAwait(client1.connect("http://cn.bing.com"));
CHECK(r.status == 200);
CHECK(r.status >= 200);
}

TEST_CASE("test collect all") {
Expand Down Expand Up @@ -2501,9 +2503,8 @@ TEST_CASE("test multipart and chunked return error") {

TEST_CASE("test coro_http_client get") {
coro_http_client client{};
auto r = client.get("http://www.baidu.com");
CHECK(!r.net_err);
CHECK(r.status < 400);
client.set_conn_timeout(1s);
client.get("http://www.baidu.com");
}

TEST_CASE("test coro_http_client add header and url queries") {
Expand Down
108 changes: 103 additions & 5 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ struct check_t {
}
};

struct check_t1 {
bool before(coro_http_request &, coro_http_response &resp) {
std::cout << "check1 before" << std::endl;
resp.set_status_and_content(status_type::bad_request, "check failed");
return false;
}
};

struct get_data {
bool before(coro_http_request &req, coro_http_response &res) {
req.set_aspect_data("hello", "world");
Expand Down Expand Up @@ -607,8 +615,13 @@ TEST_CASE("test aspects") {
co_return;
},
get_data{});
server.set_http_handler<GET, POST>(
"/check1",
[](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok, "ok");
},
check_t1{}, log_t{});
server.async_start();
std::this_thread::sleep_for(300ms);

coro_http_client client{};
auto result = client.get("http://127.0.0.1:9001/");
Expand Down Expand Up @@ -636,6 +649,9 @@ TEST_CASE("test aspects") {

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

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

TEST_CASE("use out context") {
Expand Down Expand Up @@ -696,9 +712,32 @@ TEST_CASE("delay reply, server stop, form-urlencode, qureies, throw") {
throw std::invalid_argument("invalid arguments");
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<cinatra::GET>(
"/coro_throw",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
CHECK(req.get_boundary().empty());
throw std::invalid_argument("invalid arguments");
resp.set_status_and_content(status_type::ok, "ok");
co_return;
});
server.set_http_handler<cinatra::GET>(
"/throw1", [](coro_http_request &req, coro_http_response &resp) {
CHECK(req.get_boundary().empty());
throw 1;
resp.set_status_and_content(status_type::ok, "ok");
});
server.set_http_handler<cinatra::GET>(
"/coro_throw1",
[](coro_http_request &req,
coro_http_response &resp) -> async_simple::coro::Lazy<void> {
CHECK(req.get_boundary().empty());
throw 1;
resp.set_status_and_content(status_type::ok, "ok");
co_return;
});

server.async_start();
std::this_thread::sleep_for(200ms);

resp_data result;
coro_http_client client1{};
Expand All @@ -715,6 +754,15 @@ TEST_CASE("delay reply, server stop, form-urlencode, qureies, throw") {
result = client1.get("http://127.0.0.1:9001/throw");
CHECK(result.status == 503);

result = client1.get("http://127.0.0.1:9001/coro_throw");
CHECK(result.status == 503);

result = client1.get("http://127.0.0.1:9001/throw1");
CHECK(result.status == 503);

result = client1.get("http://127.0.0.1:9001/coro_throw1");
CHECK(result.status == 503);

server.stop();
std::cout << "ok\n";
}
Expand Down Expand Up @@ -1269,13 +1317,63 @@ TEST_CASE("test restful api") {
CHECK(req.matches_.str(2) == "200");
response.set_status_and_content(status_type::ok, "number regex ok");
});
server.set_http_handler<cinatra::GET, cinatra::POST>(
"/test4/{}", [](coro_http_request &req, coro_http_response &response) {
CHECK(req.matches_.str(1) == "100");
response.set_status_and_content(status_type::ok, "number regex ok");
});

server.async_start();
std::this_thread::sleep_for(200ms);

coro_http_client client;
client.get("http://127.0.0.1:9001/test2/name/test3/test");
client.get("http://127.0.0.1:9001/numbers/100/test/200");
auto result = client.get("http://127.0.0.1:9001/test2/name/test3/test");
result = client.get("http://127.0.0.1:9001/numbers/100/test/200");
result = client.get("http://127.0.0.1:9001/test4/100");
CHECK(result.status == 200);
}

TEST_CASE("test response standalone") {
coro_http_response resp(nullptr);
resp.set_status_and_content(status_type::ok, "ok");
CHECK(resp.content() == "ok");
CHECK(resp.content_size() == 2);
CHECK(resp.need_date());
resp.need_date_head(false);
CHECK(!resp.need_date());

std::string str;
resp.build_resp_str(str);
CHECK(!str.empty());
CHECK(str.find("200") != std::string::npos);
resp.clear();
str.clear();

resp.set_status_and_content(status_type::ok, "");
std::vector<http_header> v{{"hello", "world"}};
resp.add_header_span(v);
resp.build_resp_str(str);
CHECK(str.find("200") != std::string::npos);
resp.clear();
str.clear();

resp.set_keepalive(true);
resp.build_resp_str(str);
CHECK(str.find("501") != std::string::npos);
resp.set_format_type(format_type::chunked);
resp.build_resp_str(str);
CHECK(str.find("501") != std::string::npos);
resp.clear();
str.clear();

std::string_view out = "hello";
resp.set_status_and_content_view(status_type::ok, out);
resp.build_resp_str(str);
CHECK(str.find("200") != std::string::npos);

std::vector<asio::const_buffer> buffers;
resp.set_content_type<4>();
resp.build_resp_head(buffers);
CHECK(buffers.size() == 13);
}

TEST_CASE("test radix tree restful api") {
Expand Down
Loading

0 comments on commit 6ee8f04

Please sign in to comment.