-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttpclient_async_file_test.cc
108 lines (89 loc) · 2.93 KB
/
httpclient_async_file_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "file_utils.hpp"
#include "httpclient_async.hpp"
#include <gtest/gtest.h>
#include <thread>
#include <utility>
static HttpClientAsync httpClientAsync;
static std::condition_variable cv;
static std::mutex mutex;
static std::vector<std::string> paths = {"", "curl", "curl/1", "curl/1/2", "curl/1/2/3"};
void wait()
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock);
}
void test_download(const std::string &filename)
{
auto filepath = std::filesystem::current_path() / filename;
auto url = "http://127.0.0.1:8080/files/" + filename;
httpClientAsync.download(url, filepath, {}, [](const std::string &) { cv.notify_one(); });
wait();
// 等待Content销毁,fstream完全关闭
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
void test_upload_put(const std::string &filename)
{
auto filepath = std::filesystem::current_path() / filename;
auto url = "http://127.0.0.1:8080/files/" + filename;
httpClientAsync.upload_put(url, filepath, {}, [](const std::string &) { cv.notify_one(); });
wait();
}
void test_upload_post(const std::string &path, const std::string &filename)
{
auto filepath = std::filesystem::current_path() / filename;
std::string url = "http://127.0.0.1:8080/files";
if (!path.empty()) {
url += "/" + path;
}
httpClientAsync.upload_post(url, filepath, {}, [](const std::string &) { cv.notify_one(); });
wait();
}
void test_delete(const std::string &filename)
{
auto url = "http://127.0.0.1:8080/files/" + filename;
httpClientAsync.del(url, {}, [](const std::string &) { cv.notify_one(); });
wait();
}
TEST(UploadTest, UploadPutFile)
{
auto filename = "curl_async_upload_put_file.txt";
auto data = "curl async upload put file data";
for (const auto &path : std::as_const(paths)) {
std::string filepath = filename;
std::string filedata = data;
if (!path.empty()) {
filepath = path + "/" + filename;
filedata += " " + path;
}
test_delete(filepath);
createFile(filepath, filedata);
test_upload_put(filepath);
removeFile(filepath);
test_download(filepath);
assertFileData(filepath, filedata);
}
}
TEST(UploadTest, UploadPostFile)
{
auto filename = "curl_async_upload_post_file.txt";
auto data = "curl async upload post file data";
for (const auto &path : std::as_const(paths)) {
std::string filepath = filename;
std::string filedata = data;
if (!path.empty()) {
filepath = path + "/" + filename;
filedata += " " + path;
}
test_delete(filepath);
createFile(filepath, filedata);
test_upload_post(path, filepath);
removeFile(filepath);
test_download(filepath);
assertFileData(filepath, filedata);
}
}
auto main(int argc, char *argv[]) -> int
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}