-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttpclient_file_test.cc
94 lines (78 loc) · 2.43 KB
/
httpclient_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
#include "file_utils.hpp"
#include "httpclient.hpp"
#include <gtest/gtest.h>
#include <utility>
std::vector<std::string> paths = {"", "curl", "curl/1", "curl/1/2", "curl/1/2/3"};
void test_download(const std::string &filename)
{
auto filepath = std::filesystem::current_path() / filename;
auto url = "http://127.0.0.1:8080/files/" + filename;
HttpClient httpClient;
httpClient.download(url, filepath);
}
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;
HttpClient httpClient;
httpClient.upload_put(url, filepath);
}
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;
}
HttpClient httpClient;
httpClient.upload_post(url, filepath);
}
void test_delete(const std::string &filename)
{
auto url = "http://127.0.0.1:8080/files/" + filename;
HttpClient httpClient;
httpClient.del(url);
}
TEST(UploadTest, UploadPutFile)
{
auto filename = "curl_upload_put_file.txt";
auto data = "curl 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_upload_post_file.txt";
auto data = "curl 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();
}