From 5823d4a5ee29aebdd7ed61cf294a1ede29997897 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 20 Nov 2024 15:10:52 +0800 Subject: [PATCH] Test http server (#647) --- include/cinatra/coro_http_server.hpp | 9 ++++++++- tests/CMakeLists.txt | 1 + tests/test_cinatra.cpp | 2 +- tests/test_coro_http_server.cpp | 15 ++++++++------- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/cinatra/coro_http_server.hpp b/include/cinatra/coro_http_server.hpp index 0403d109..4b16c9ae 100644 --- a/include/cinatra/coro_http_server.hpp +++ b/include/cinatra/coro_http_server.hpp @@ -351,7 +351,14 @@ class coro_http_server { } if (!file_path.empty()) { - static_dir_ = std::filesystem::path(file_path).make_preferred().string(); + file_path = std::filesystem::path(file_path).filename().string(); + if (file_path.empty()) { + static_dir_ = fs::absolute(fs::current_path().string()).string(); + } + else { + static_dir_ = + std::filesystem::path(file_path).make_preferred().string(); + } } else { static_dir_ = fs::absolute(fs::current_path().string()).string(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cce3397c..bb321a80 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,6 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests) set(project_name test_cinatra) +add_definitions(-DINJECT_FOR_HTTP_CLIENT_TEST) add_executable(${project_name} test_coro_http_server.cpp test_cinatra.cpp diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index e0819b98..c5aa414d 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -1337,7 +1337,7 @@ TEST_CASE("test multiple ranges download") { TEST_CASE("test ranges download") { create_file("test_range.txt", 64); coro_http_server server(1, 8090); - server.set_static_res_dir("", ""); + server.set_static_res_dir("", "./"); server.async_start(); coro_http_client client{}; diff --git a/tests/test_coro_http_server.cpp b/tests/test_coro_http_server.cpp index be6caaa2..d9852153 100644 --- a/tests/test_coro_http_server.cpp +++ b/tests/test_coro_http_server.cpp @@ -1013,6 +1013,7 @@ TEST_CASE("test websocket binary data") { } } }); + server.set_http_handler( "/medium_binary", [](coro_http_request &req, @@ -1440,7 +1441,7 @@ TEST_CASE("test reverse proxy") { std::invalid_argument); } - cinatra::coro_http_server web_one(1, 9001); + cinatra::coro_http_server web_one(1, 9004); web_one.set_http_handler( "/", @@ -1479,21 +1480,21 @@ TEST_CASE("test reverse proxy") { coro_http_server proxy_wrr(2, 8090); proxy_wrr.set_http_proxy_handler( - "/", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"}, + "/", {"http://127.0.0.1:9004", "127.0.0.1:9002", "127.0.0.1:9003"}, coro_io::load_blance_algorithm::WRR, {10, 5, 5}, log_t{}, check_t{}); coro_http_server proxy_rr(2, 8091); proxy_rr.set_http_proxy_handler( - "/", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"}, + "/", {"127.0.0.1:9004", "127.0.0.1:9002", "127.0.0.1:9003"}, coro_io::load_blance_algorithm::RR, {}, log_t{}); coro_http_server proxy_random(2, 8092); proxy_random.set_http_proxy_handler( - "/", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"}); + "/", {"127.0.0.1:9004", "127.0.0.1:9002", "127.0.0.1:9003"}); coro_http_server proxy_all(2, 8093); proxy_all.set_http_proxy_handler( - "/", {"127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"}); + "/", {"127.0.0.1:9004", "127.0.0.1:9002", "127.0.0.1:9003"}); proxy_wrr.async_start(); proxy_rr.async_start(); @@ -1561,7 +1562,7 @@ TEST_CASE("test reverse proxy websocket") { }); server.async_start(); - coro_http_server proxy_server(1, 9002); + coro_http_server proxy_server(1, 9005); proxy_server.set_websocket_proxy_handler("/ws_echo", {"ws://127.0.0.1:9001/ws_echo"}); proxy_server.async_start(); @@ -1569,7 +1570,7 @@ TEST_CASE("test reverse proxy websocket") { coro_http_client client{}; auto r = async_simple::coro::syncAwait( - client.connect("ws://127.0.0.1:9002/ws_echo")); + client.connect("ws://127.0.0.1:9005/ws_echo")); CHECK(!r.net_err); for (int i = 0; i < 10; i++) { async_simple::coro::syncAwait(client.write_websocket("test websocket"));