From 9b23cd97c10ee7f8bc5f4fc94e373ecf02344625 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 8 Jul 2024 17:01:44 +0800 Subject: [PATCH 1/2] fix out buf --- include/cinatra/coro_http_client.hpp | 8 ++------ tests/test_cinatra.cpp | 1 + 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 02c0ddc6..67c81782 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -1317,17 +1317,13 @@ class coro_http_client : public std::enable_shared_from_this { if (!body_.empty()) { body_.clear(); } - if (!out_buf.empty()) { - out_buf_ = out_buf; - } + + out_buf_ = out_buf; std::shared_ptr guard(nullptr, [this](auto) { if (!req_headers_.empty()) { req_headers_.clear(); } - if (!out_buf_.empty()) { - out_buf_ = {}; - } }); resp_data data{}; diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index 14b0a9fc..299495ce 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -680,6 +680,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()); } } From bd2b51f26458bde6e8dad44fe158945008bb37f6 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 8 Jul 2024 17:16:03 +0800 Subject: [PATCH 2/2] fix case --- tests/test_cinatra.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index 299495ce..08c3de7c 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -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( + "/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; @@ -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{str.data(), str.size()});