From 34ab1b9f4df80055b0572c7f089da66d027a86dd Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 19 Nov 2024 16:11:06 +0800 Subject: [PATCH] [coro_http][unit test]fix and test (#817) --- .../standalone/cinatra/coro_http_client.hpp | 8 ++-- src/coro_http/tests/test_cinatra.cpp | 42 +++++++++++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/include/ylt/standalone/cinatra/coro_http_client.hpp b/include/ylt/standalone/cinatra/coro_http_client.hpp index fd8ed4364..00a1db329 100644 --- a/include/ylt/standalone/cinatra/coro_http_client.hpp +++ b/include/ylt/standalone/cinatra/coro_http_client.hpp @@ -883,7 +883,7 @@ class coro_http_client : public std::enable_shared_from_this { } void handle_upload_header_with_chunked( - std::unordered_map headers) { + std::unordered_map &headers) { if (!resp_chunk_str_.empty()) { resp_chunk_str_.clear(); } @@ -899,7 +899,7 @@ class coro_http_client : public std::enable_shared_from_this { template int64_t handle_upload_header_with_length( resp_data &data, Source &source, - std::unordered_map headers, uint64_t offset, + std::unordered_map &headers, uint64_t offset, int64_t content_length) { if (content_length < 0) { if constexpr (is_stream_ptr_v) { @@ -1215,7 +1215,7 @@ class coro_http_client : public std::enable_shared_from_this { int64_t content_length = -1 /*upload size*/, req_content_type content_type = req_content_type::text, std::unordered_map headers = {}) { - co_return co_await async_upload_impl( + return async_upload_impl( std::move(uri), method, std::move(source), content_type, std::move(headers), offset, content_length); } @@ -1226,7 +1226,7 @@ class coro_http_client : public std::enable_shared_from_this { S uri, http_method method, Source source, req_content_type content_type = req_content_type::text, std::unordered_map headers = {}) { - co_return co_await async_upload_impl( + return async_upload_impl( std::move(uri), method, std::move(source), content_type, std::move(headers)); } diff --git a/src/coro_http/tests/test_cinatra.cpp b/src/coro_http/tests/test_cinatra.cpp index c321f7abd..1175f82f4 100644 --- a/src/coro_http/tests/test_cinatra.cpp +++ b/src/coro_http/tests/test_cinatra.cpp @@ -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); @@ -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( "/upload", [](coro_http_request &req, @@ -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); @@ -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 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 @@ -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