Skip to content

Commit 5e10f44

Browse files
committed
[增加文件操作测试和异步HTTP客户端功能]: 增加文件操作测试和异步HTTP客户端功能
- 在 `Curl` 目录下新增 `file_utils.hpp` 文件,提供文件创建、删除和断言文件内容等辅助函数 - 修改 `CMakeLists.txt` 文件,增加 `httpclient_file_test` 和 `httpclient_async_file_test` 两个可执行文件的编译配置,并链接 `GTest` 相关库 - 修改 `httpclient.cc` 和 `httpclient.hpp` 文件,增加 `download`、`upload_put` 和 `upload_post` 方法,实现文件下载和上传功能 - 修改 `httpclient_async.cc` 和 `httpclient_async.hpp` 文件,增加 `download`、`upload_put` 和 `upload_post` 方法,实现异步文件下载和上传功能 - 新增 `httpclient_file_test.cc` 文件,编写文件上传和下载的测试用例,使用 `file_utils.hpp` 中的辅助函数进行测试 - 新增 `httpclient_async_file_test.cc` 文件,编写异步文件上传和下载的测试用例,使用 `file_utils.hpp` 中的辅助函数进行测试
1 parent 7d980fa commit 5e10f44

12 files changed

+1128
-382
lines changed

Curl/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ target_link_libraries(tcpclient_test PRIVATE CURL::libcurl)
44
add_executable(httpclient_test httpclient_test.cc httpclient.cc httpclient.hpp)
55
target_link_libraries(httpclient_test PRIVATE CURL::libcurl)
66

7+
add_executable(httpclient_file_test file_utils.hpp httpclient_file_test.cc
8+
httpclient.cc httpclient.hpp)
9+
target_link_libraries(
10+
httpclient_file_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main
11+
GTest::gmock GTest::gmock_main)
12+
713
add_executable(httpclient_async_test httpclient_async_test.cc
814
httpclient_async.cc httpclient_async.hpp)
915
target_link_libraries(httpclient_async_test PRIVATE CURL::libcurl)
16+
17+
add_executable(
18+
httpclient_async_file_test file_utils.hpp httpclient_async_file_test.cc
19+
httpclient_async.cc httpclient_async.hpp)
20+
target_link_libraries(
21+
httpclient_async_file_test
22+
PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main GTest::gmock
23+
GTest::gmock_main)

Curl/file_utils.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <filesystem>
4+
#include <fstream>
5+
#include <iostream>
6+
7+
void createFile(const std::string &filename, const std::string &data)
8+
{
9+
auto filepath = std::filesystem::current_path() / filename;
10+
std::ofstream file(filepath);
11+
if (!file.is_open()) {
12+
std::cerr << "Cannot open the file: " << filepath << std::endl;
13+
return;
14+
}
15+
file << data;
16+
file.close();
17+
}
18+
19+
void removeFile(const std::string &filename)
20+
{
21+
auto filepath = std::filesystem::current_path() / filename;
22+
if (std::filesystem::exists(filepath)) {
23+
std::filesystem::remove(filepath);
24+
}
25+
}
26+
27+
void assertFileData(const std::string &filename, const std::string &data)
28+
{
29+
auto filepath = std::filesystem::current_path() / filename;
30+
std::ifstream file(filepath);
31+
if (!file.is_open()) {
32+
std::cerr << "Cannot open the file: " << filepath << std::endl;
33+
return;
34+
}
35+
std::string fileData((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
36+
file.close();
37+
EXPECT_EQ(fileData, data);
38+
}

0 commit comments

Comments
 (0)