Skip to content

Commit

Permalink
Test http server (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Nov 20, 2024
1 parent fbfdc18 commit 5823d4a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
9 changes: 8 additions & 1 deletion include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
15 changes: 8 additions & 7 deletions tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ TEST_CASE("test websocket binary data") {
}
}
});

server.set_http_handler<cinatra::GET>(
"/medium_binary",
[](coro_http_request &req,
Expand Down Expand Up @@ -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<cinatra::GET, cinatra::POST>(
"/",
Expand Down Expand Up @@ -1479,21 +1480,21 @@ TEST_CASE("test reverse proxy") {

coro_http_server proxy_wrr(2, 8090);
proxy_wrr.set_http_proxy_handler<GET, POST>(
"/", {"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<GET, POST>(
"/", {"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<GET, POST>(
"/", {"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();
Expand Down Expand Up @@ -1561,15 +1562,15 @@ 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();
std::this_thread::sleep_for(200ms);

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"));
Expand Down

0 comments on commit 5823d4a

Please sign in to comment.