-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.cpp
213 lines (182 loc) · 6.38 KB
/
main.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "httpclient.hpp"
#include <QCoreApplication>
#include <QFile>
#include <QJsonObject>
#include <QTest>
void createFile(const QString &filename, const QByteArray &data)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
QDir().mkpath(QFileInfo(filepath).absoluteDir().path());
QFile file(filepath);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << QString("Cannot open the file: %1!").arg(filepath) << file.errorString();
return;
}
file.write(data);
file.close();
}
void removeFile(const QString &filename)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
QFile file(filepath);
if (file.exists()) {
file.remove();
}
}
void assertFileData(const QString &filename, const QByteArray &data)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
QVERIFY2(false, QString("Cannot open the file: %1!").arg(filepath).toStdString().c_str());
return;
}
auto fileData = file.readAll();
file.close();
QCOMPARE(fileData, data);
}
void test_get()
{
HttpClient httpClient;
auto *reply = httpClient.sendRequest(HttpClient::Method::GET,
QUrl("http://www.baidu.com"),
{},
{},
30);
httpClient.sync(reply);
}
void test_download(const QString &filename)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
QDir().mkpath(QFileInfo(filepath).absoluteDir().path());
auto url = QString("http://127.0.0.1:8080/files/%1").arg(filename);
HttpClient httpClient;
auto *reply = httpClient.downLoad(QUrl(url), filepath, 30);
httpClient.sync(reply);
}
void test_upload_file(const QString &filename)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
auto url = QString("http://127.0.0.1:8080/files/%1").arg(filename);
HttpClient httpClient;
auto *reply = httpClient.upload_put(QUrl(url), filepath, 30);
httpClient.sync(reply);
}
void test_upload_data(const QString &filename, const QByteArray &data)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
auto url = QString("http://127.0.0.1:8080/files/%1").arg(filename);
HttpClient httpClient;
auto *reply = httpClient.upload_put(QUrl(url), data, 30);
httpClient.sync(reply);
}
void test_upload_post(const QString &path, const QString &filename)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
auto url = QString("http://127.0.0.1:8080/files");
if (!path.isEmpty()) {
url += QString("/%1").arg(path);
}
HttpClient httpClient;
auto *reply = httpClient.upload_post(QUrl(url), filepath, 30);
httpClient.sync(reply);
}
void test_upload_post(const QString &path, const QString &filename, const QByteArray &data)
{
auto filepath = QString("%1/%2").arg(qApp->applicationDirPath(), filename);
auto url = QString("http://127.0.0.1:8080/files");
if (!path.isEmpty()) {
url += QString("/%1").arg(path);
}
HttpClient httpClient;
auto *reply = httpClient.upload_post(QUrl(url), QFileInfo(filepath).fileName(), data, 30);
httpClient.sync(reply);
}
void test_delete(const QString &filename)
{
auto url = QString("http://127.0.0.1:8080/files/%1").arg(filename);
HttpClient httpClient;
auto *reply = httpClient.sendRequest(HttpClient::Method::DELETE, QUrl(url), {}, {}, 30);
httpClient.sync(reply);
}
class HttpClientTest : public QObject
{
Q_OBJECT
QStringList paths = {"", "qt", "qt/1", "qt/1/2", "qt/1/2/3"};
private slots:
void onTestGet() { test_get(); }
void onTestUploadPutFile()
{
auto filename = "upload_put_file.txt";
auto data = "upload put file data";
for (const auto &path : std::as_const(paths)) {
QString filepath = filename;
QByteArray filedata = data;
if (!path.isEmpty()) {
filepath = path + "/" + filename;
filedata += QByteArray(" ") + path.toUtf8();
}
test_delete(filepath);
createFile(filepath, filedata);
test_upload_file(filepath);
removeFile(filepath);
test_download(filepath);
assertFileData(filepath, filedata);
}
}
void onTestUploadPutData()
{
auto filename = "upload_put_data.txt";
auto data = "upload put data";
for (const auto &path : std::as_const(paths)) {
QString filepath = filename;
QByteArray filedata = data;
if (!path.isEmpty()) {
filepath = path + "/" + filename;
filedata += QByteArray(" ") + path.toUtf8();
}
test_delete(filepath);
test_upload_data(filepath, filedata);
test_download(filepath);
assertFileData(filepath, filedata);
}
}
void onTestUploadPost()
{
auto filename = "upload_post_file.txt";
auto data = "upload post file data";
for (const auto &path : std::as_const(paths)) {
QString filepath = filename;
QByteArray filedata = data;
if (!path.isEmpty()) {
filepath = path + "/" + filename;
filedata += QByteArray(" ") + path.toUtf8();
}
test_delete(filepath);
createFile(filepath, filedata);
test_upload_post(path, filepath);
removeFile(filepath);
test_download(filepath);
assertFileData(filepath, filedata);
}
}
void onTestUploadPostData()
{
auto filename = "upload_post_data.txt";
auto data = "upload post data";
for (const auto &path : std::as_const(paths)) {
QString filepath = filename;
QByteArray filedata = data;
if (!path.isEmpty()) {
filepath = path + "/" + filename;
filedata += QByteArray(" ") + path.toUtf8();
}
test_delete(filepath);
test_upload_post(path, filepath, filedata);
test_download(filepath);
assertFileData(filepath, filedata);
}
}
};
QTEST_MAIN(HttpClientTest)
#include "main.moc"