Skip to content

Commit cb25fb7

Browse files
committed
[HttpClient重构与功能扩展]: 对Curl模块进行重构,扩展HTTP客户端功能并优化代码结构
- 重构HttpClient和HttpClientAsync类,引入PIMPL模式隐藏实现细节,提升代码可维护性 - 为HttpClient添加文件上传和下载功能,支持PUT和POST方法 - 为HttpClientAsync添加异步文件上传和下载功能,支持PUT和POST方法 - 添加文件工具类`file_utils`,提供文件创建、删除和数据断言功能 - 优化CMakeLists.txt,引入Google Test框架进行单元测试 - 修复HttpClient和HttpClientAsync中部分逻辑错误,提升代码稳定性 - 为TcpClient类添加错误信息获取功能,优化连接状态管理
1 parent 7d980fa commit cb25fb7

13 files changed

+1248
-404
lines changed

Curl/CMakeLists.txt

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
add_executable(tcpclient_test tcpclient_test.cc tcpclient.cc tcpclient.hpp)
2-
target_link_libraries(tcpclient_test PRIVATE CURL::libcurl)
2+
target_link_libraries(
3+
tcpclient_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main
4+
GTest::gmock GTest::gmock_main)
35

4-
add_executable(httpclient_test httpclient_test.cc httpclient.cc httpclient.hpp)
5-
target_link_libraries(httpclient_test PRIVATE CURL::libcurl)
6+
set(HttpClientSource file_utils.cc file_utils.hpp httpclient.cc httpclient.hpp)
67

7-
add_executable(httpclient_async_test httpclient_async_test.cc
8-
httpclient_async.cc httpclient_async.hpp)
9-
target_link_libraries(httpclient_async_test PRIVATE CURL::libcurl)
8+
add_executable(httpclient_test ${HttpClientSource} httpclient_test.cc)
9+
target_link_libraries(
10+
httpclient_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main
11+
GTest::gmock GTest::gmock_main)
12+
13+
add_executable(httpclient_file_test ${HttpClientSource} httpclient_file_test.cc)
14+
target_link_libraries(
15+
httpclient_file_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main
16+
GTest::gmock GTest::gmock_main)
17+
18+
set(HttpClientAsyncSource file_utils.cc file_utils.hpp httpclient_async.cc
19+
httpclient_async.hpp)
20+
21+
add_executable(httpclient_async_test ${HttpClientAsyncSource}
22+
httpclient_async_test.cc)
23+
target_link_libraries(
24+
httpclient_async_test PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main
25+
GTest::gmock GTest::gmock_main)
26+
27+
add_executable(httpclient_async_file_test ${HttpClientAsyncSource}
28+
httpclient_async_file_test.cc)
29+
target_link_libraries(
30+
httpclient_async_file_test
31+
PRIVATE CURL::libcurl GTest::gtest GTest::gtest_main GTest::gmock
32+
GTest::gmock_main)

Curl/file_utils.cc

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "file_utils.hpp"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include <filesystem>
6+
#include <fstream>
7+
#include <iostream>
8+
9+
void createFile(const std::string &filename, const std::string &data)
10+
{
11+
auto filepath = std::filesystem::current_path() / filename;
12+
std::filesystem::create_directories(filepath.parent_path());
13+
std::ofstream file(filepath);
14+
if (!file.is_open()) {
15+
std::cerr << "Cannot open the file: " << filepath << std::endl;
16+
return;
17+
}
18+
file << data;
19+
file.close();
20+
}
21+
22+
void removeFile(const std::string &filename)
23+
{
24+
auto filepath = std::filesystem::current_path() / filename;
25+
if (std::filesystem::exists(filepath)) {
26+
std::filesystem::remove(filepath);
27+
}
28+
}
29+
30+
void assertFileData(const std::string &filename, const std::string &data)
31+
{
32+
auto filepath = std::filesystem::current_path() / filename;
33+
std::ifstream file(filepath);
34+
if (!file.is_open()) {
35+
std::cerr << "Cannot open the file: " << filepath << std::endl;
36+
return;
37+
}
38+
std::string fileData((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
39+
file.close();
40+
EXPECT_EQ(fileData, data);
41+
}
42+
43+
auto formatBytes(double value, int precision) -> std::string
44+
{
45+
std::vector<std::string> units = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
46+
47+
int i = 0;
48+
while (value > 1024) {
49+
value /= 1024;
50+
i++;
51+
}
52+
53+
std::ostringstream out;
54+
out.precision(precision);
55+
out << std::fixed << value;
56+
57+
return out.str() + " " + units[i];
58+
}

Curl/file_utils.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
void createFile(const std::string &filename, const std::string &data);
6+
7+
void removeFile(const std::string &filename);
8+
9+
void assertFileData(const std::string &filename, const std::string &data);
10+
11+
auto formatBytes(double value, int precision = 2) -> std::string;

0 commit comments

Comments
 (0)