Skip to content

Commit

Permalink
[coro_http][unit test]fix and test (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Nov 19, 2024
1 parent 8fe0523 commit 34ab1b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
8 changes: 4 additions & 4 deletions include/ylt/standalone/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
}

void handle_upload_header_with_chunked(
std::unordered_map<std::string, std::string> headers) {
std::unordered_map<std::string, std::string> &headers) {
if (!resp_chunk_str_.empty()) {
resp_chunk_str_.clear();
}
Expand All @@ -899,7 +899,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
template <typename Source>
int64_t handle_upload_header_with_length(
resp_data &data, Source &source,
std::unordered_map<std::string, std::string> headers, uint64_t offset,
std::unordered_map<std::string, std::string> &headers, uint64_t offset,
int64_t content_length) {
if (content_length < 0) {
if constexpr (is_stream_ptr_v<Source>) {
Expand Down Expand Up @@ -1215,7 +1215,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
int64_t content_length = -1 /*upload size*/,
req_content_type content_type = req_content_type::text,
std::unordered_map<std::string, std::string> headers = {}) {
co_return co_await async_upload_impl<upload_type_t::with_length>(
return async_upload_impl<upload_type_t::with_length>(
std::move(uri), method, std::move(source), content_type,
std::move(headers), offset, content_length);
}
Expand All @@ -1226,7 +1226,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
S uri, http_method method, Source source,
req_content_type content_type = req_content_type::text,
std::unordered_map<std::string, std::string> headers = {}) {
co_return co_await async_upload_impl<upload_type_t::chunked>(
return async_upload_impl<upload_type_t::chunked>(
std::move(uri), method, std::move(source), content_type,
std::move(headers));
}
Expand Down
42 changes: 39 additions & 3 deletions src/coro_http/tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ TEST_CASE("test ssl client") {
coro_http_client client{};
client.enable_auto_redirect(true);
bool ok = client.init_ssl();
client.reset();
REQUIRE_MESSAGE(ok == true, "init ssl fail, please check ssl config");
auto result = client.get("https://www.bing.com");
CHECK(result.status >= 200);
Expand Down Expand Up @@ -1475,10 +1476,8 @@ TEST_CASE("test coro_http_client multipart upload") {
#ifdef CINATRA_ENABLE_SSL
TEST_CASE("test ssl upload") {
coro_http_server server(1, 8091);
#ifdef CINATRA_ENABLE_SSL
server.init_ssl("../openssl_files/server.crt", "../openssl_files/server.key",
"test");
#endif
server.set_http_handler<cinatra::PUT>(
"/upload",
[](coro_http_request &req,
Expand Down Expand Up @@ -1532,6 +1531,8 @@ TEST_CASE("test ssl upload") {
coro_http_client client{};
bool r = client.init_ssl();
CHECK(r);
r = client.init_ssl();
CHECK(r);
client.add_header("filename", filename);
auto lazy = client.async_upload(uri, http_method::PUT, filename);
auto result = async_simple::coro::syncAwait(lazy);
Expand Down Expand Up @@ -1581,11 +1582,36 @@ TEST_CASE("test ssl upload") {
coro_http_client client{};
bool r = client.init_ssl();
CHECK(r);
std::string_view file = "test_ssl_upload.txt";
client.add_header("filename", filename);
auto lazy = client.async_upload_chunked(uri, http_method::PUT, filename);
auto lazy = client.async_upload_chunked(uri, http_method::PUT, file);
auto result = async_simple::coro::syncAwait(lazy);
CHECK(result.status == 200);
}

{
coro_http_client client{};
client.enable_sni_hostname(true);
bool r = client.init_ssl();
CHECK(r);
std::unordered_map<std::string, std::string> headers;
headers.emplace("filename", filename);
auto lazy = client.async_upload_chunked(uri, http_method::PUT, filename,
req_content_type::none, headers);
auto result = async_simple::coro::syncAwait(lazy);
CHECK(result.status == 200);
}

{
coro_http_client client{};
client.write_failed_forever_ = true;
bool r = client.init_ssl();
CHECK(r);
client.add_header("filename", filename);
auto lazy = client.async_upload_chunked(uri, http_method::PUT, filename);
auto result = async_simple::coro::syncAwait(lazy);
CHECK(result.status != 200);
}
}
#endif

Expand Down Expand Up @@ -2168,6 +2194,16 @@ TEST_CASE("test inject failed") {
client1.async_upload_multipart("http://baidu.com"));
CHECK(ret.status != 200);
}

{
coro_http_client client1{};
client1.write_failed_forever_ = true;
ret = async_simple::coro::syncAwait(client1.connect("http://baidu.com"));
if (!ret.net_err) {
ret = async_simple::coro::syncAwait(client1.write_websocket("test"));
CHECK(ret.status != 200);
}
}
}
#endif

Expand Down

0 comments on commit 34ab1b9

Please sign in to comment.