-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.cxx
172 lines (156 loc) · 4.7 KB
/
test.cxx
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
#include <picotest/picotest.h>
#undef ok
#define CLASK_TEST
#include <clask/core.hpp>
#include <unordered_map>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void test_clask_params() {
std::unordered_map<std::string, std::string> result;
result = clask::params("foo");
_ok(result.size() == 0, R"(result.size() == 0)");
result = clask::params("foo=bar");
_ok(result.size() == 1, R"(result.size() == 1)");
_ok(result["foo"] == "bar", R"(result["foo"] == "bar")");
result = clask::params("foo=bar&bar=baz");
_ok(result.size() == 2, R"(result.size() == 2)");
_ok(result["foo"] == "bar", R"(result["foo"] == "bar")");
_ok(result["bar"] == "baz", R"(result["bar"] == "baz")");
}
void test_clask_request_parse_multipart1() {
std::vector<clask::part> parts;
bool result;
parts.clear();
clask::request req(
"GET",
"/",
"/",
{},
{},
"");
result = req.parse_multipart(parts);
_ok(result == false, R"(result == false)");
}
void test_clask_request_parse_multipart2() {
std::vector<clask::part> parts;
bool result;
parts.clear();
clask::request req(
"GET",
"/",
"/",
{},
{
{ "Content-Type", R"(multipart/form-data;boundary="boundary")" },
},
"--boundary\r\n"
"Content-Disposition: form-data; name=\"field1\"\r\n"
"\r\n"
"value1\r\n"
"--boundary--\r\n");
result = req.parse_multipart(parts);
_ok(result == true, R"(result == true)");
_ok(parts.size() == 1, R"(parts.size() == 1)");
_ok(parts[0].name() == "field1", R"(parts[0].name() == "field1")");
}
void test_clask_request_parse_multipart3() {
std::vector<clask::part> parts;
bool result;
parts.clear();
clask::request req(
"GET",
"/",
"/",
{},
{
{ "Content-Type", R"(multipart/form-data;boundary="boundary")" },
},
"--boundary\r\n"
"Content-Disposition: form-data; filename=README.md; name=\"field1\"\r\n"
"\r\n"
"value1\r\n"
"--boundary--\r\n");
result = req.parse_multipart(parts);
_ok(result == true, R"(result == true)");
_ok(parts.size() == 1, R"(parts.size() == 1)");
_ok(parts[0].name() == "field1", R"(parts[0].name() == "field1")");
_ok(parts[0].filename() == "README.md", R"(parts[0].filename() == "README.md")");
}
void test_clask_request_parse_multipart4() {
std::vector<clask::part> parts;
bool result;
parts.clear();
clask::request req(
"GET",
"/",
"/",
{},
{
{ "Content-Type", R"(multipart/form-data;boundary="boundary")" },
},
"--boundary\r\n"
"Content-Disposition: form-data; filename=README.md name=\"field1\"\r\n"
"\r\n"
"value1\r\n"
"--boundary--\r\n");
result = req.parse_multipart(parts);
_ok(result == true, R"(result == true)");
_ok(parts.size() == 1, R"(parts.size() == 1)");
_ok(parts[0].name() == "", R"(parts[0].name() == "")");
_ok(parts[0].filename() == "README.md name=\"field1", R"(parts[0].filename() == "README.md name=\"field1")");
}
void test_clask_to_wstring() {
_ok(clask::to_wstring("あいうえお") == L"あいうえお", R"(clask::to_wstring("あいうえお") == L"あいうえお")");
}
void test_clask_request_uri_param() {
typedef struct {
bool result;
std::string path;
std::vector<std::string> args;
} test_param;
std::vector<test_param> tests = {
{
.result = false,
.path = "/foa",
.args = {},
},
{
.result = true,
.path = "/foo",
.args = {},
},
{
.result = true,
.path = "/foo/boo",
.args = { "boo" },
},
{
.result = true,
.path = "/foo/ぼえ~",
.args = { "ぼえ~" },
},
};
auto s = clask::server();
s.GET("/foo/:bar", [](clask::request& /*req*/) -> std::string {
return "OK";
});
for(auto x : tests) {
std::vector<std::string> req_args;
auto result = s.test_match("GET", x.path, [&](const clask::func_t& /*fn*/, const std::vector<std::string>& args) {
req_args = args;
});
_ok(result == x.result, R"(result == x.result)");
_ok(req_args.size() == x.args.size(), R"(req.args.size() == x.args.size())");
}
}
int main() {
subtest("test_clask_params", test_clask_params);
subtest("test_clask_request_parse_multipart1", test_clask_request_parse_multipart1);
subtest("test_clask_request_parse_multipart2", test_clask_request_parse_multipart2);
subtest("test_clask_request_parse_multipart3", test_clask_request_parse_multipart3);
subtest("test_clask_request_parse_multipart4", test_clask_request_parse_multipart4);
subtest("test_clask_to_wstring", test_clask_to_wstring);
subtest("test_clask_request_uri_param", test_clask_request_uri_param);
return done_testing();
}