Skip to content

Commit

Permalink
fix out buf (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 8, 2024
1 parent f792cd6 commit 3896424
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
8 changes: 2 additions & 6 deletions include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,17 +1317,13 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
if (!body_.empty()) {
body_.clear();
}
if (!out_buf.empty()) {
out_buf_ = out_buf;
}

out_buf_ = out_buf;

std::shared_ptr<int> guard(nullptr, [this](auto) {
if (!req_headers_.empty()) {
req_headers_.clear();
}
if (!out_buf_.empty()) {
out_buf_ = {};
}
});

resp_data data{};
Expand Down
16 changes: 13 additions & 3 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,17 @@ TEST_CASE("test default http handler") {
}

TEST_CASE("test request with out buffer") {
coro_http_server server(1, 8090);
server.set_http_handler<GET>(
"/test", [](coro_http_request &req, coro_http_response &resp) {
resp.set_status_and_content(status_type::ok,
"it is a test string, more than 10 bytes");
});
server.async_start();

std::string str;
str.resize(10);
std::string url = "http://cn.bing.com";
std::string url = "http://127.0.0.1:8090/test";

{
coro_http_client client;
Expand All @@ -666,12 +674,13 @@ TEST_CASE("test request with out buffer") {
auto result = async_simple::coro::syncAwait(ret);
std::cout << result.status << "\n";
std::cout << result.net_err.message() << "\n";
CHECK(result.status < 400);
std::cout << result.resp_body << "\n";
CHECK(result.status == 200);
CHECK(!client.is_body_in_out_buf());
}

{
detail::resize(str, 102400);
detail::resize(str, 1024);
coro_http_client client;
auto ret = client.async_request(url, http_method::GET, req_context<>{}, {},
std::span<char>{str.data(), str.size()});
Expand All @@ -680,6 +689,7 @@ TEST_CASE("test request with out buffer") {
CHECK(ok);
std::string_view sv(str.data(), result.resp_body.size());
CHECK(result.resp_body == sv);
CHECK(client.is_body_in_out_buf());
}
}

Expand Down

0 comments on commit 3896424

Please sign in to comment.