FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
expresscpp/test/layer.cpp at master · deimi/expresscpp · GitHub
deimi
/
expresscpp
Public
forked from
expresscpp/expresscpp
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
expresscpp
/
test
/
layer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
202 lines (185 loc) · 7.46 KB
Breadcrumbs
expresscpp
/
test
/
layer.cpp
Copy path
File metadata and controls
202 lines (185 loc) · 7.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
expresscpp/expresscpp.hpp
"
#
include
"
expresscpp/fetch.hpp
"
#
include
"
expresscpp/impl/routing_stack.hpp
"
#
include
"
gtest/gtest.h
"
#
include
"
nlohmann/json.hpp
"
using
namespace
expresscpp
;
using
namespace
std
::string_literals
;
TEST
(LayerTests, TestKeyFirst) {
auto
layer =
Layer
(
"
/:id
"
);
auto
req = std::make_shared<Request>(
"
/12
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(layer.
GetKeys
().
size
(),
1
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
name_
,
"
id
"
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
index_
,
0
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
id
"
),
"
12
"
);
}
TEST
(LayerTests, TestIncompleteKey) {
EXPECT_ANY_THROW
(
Layer
(
"
:
"
));
EXPECT_ANY_THROW
(
Layer
(
"
/:
"
));
EXPECT_ANY_THROW
(
Layer
(
"
/a/b/:/c
"
));
EXPECT_ANY_THROW
(
Layer
(
"
/a/b/c/:
"
));
EXPECT_ANY_THROW
(
Layer
(
"
/a/:/c/:
"
));
}
TEST
(LayerTests, TestWithSingleKey) {
auto
layer =
Layer
(
"
/:id/a
"
);
auto
req = std::make_shared<Request>(
"
/12/a
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(layer.
GetKeys
().
size
(),
1
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
name_
,
"
id
"
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
index_
,
0
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
id
"
),
"
12
"
);
}
TEST
(LayerTests, TestWithMultipleKeys) {
auto
layer =
Layer
(
"
/:id1/a/:id2
"
);
auto
req = std::make_shared<Request>(
"
/12/a/13
"
);
EXPECT_EQ
(layer.
GetKeys
().
size
(),
2
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
2
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
name_
,
"
id1
"
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
index_
,
0
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
id1
"
),
"
12
"
);
EXPECT_EQ
(layer.
GetKeys
()[
1
].
name_
,
"
id2
"
);
EXPECT_EQ
(layer.
GetKeys
()[
1
].
index_
,
1
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
id2
"
),
"
13
"
);
}
TEST
(LayerTests, TestWithRangesAtEnd) {
auto
layer =
Layer
(
"
/abc/:from-:to
"
);
auto
req = std::make_shared<Request>(
"
/abc/100-200
"
);
EXPECT_EQ
(layer.
GetKeys
().
size
(),
2
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
2
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
name_
,
"
from
"
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
index_
,
0
);
EXPECT_EQ
(layer.
GetKeys
()[
1
].
name_
,
"
to
"
);
EXPECT_EQ
(layer.
GetKeys
()[
1
].
index_
,
1
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
from
"
),
"
100
"
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
to
"
),
"
200
"
);
}
TEST
(LayerTests, TestWithRangesInBetween) {
auto
layer =
Layer
(
"
/abc/:from-:to/a
"
);
EXPECT_EQ
(layer.
GetKeys
().
size
(),
2
);
auto
req = std::make_shared<Request>(
"
/abc/100-200/a
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
2
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
name_
,
"
from
"
);
EXPECT_EQ
(layer.
GetKeys
()[
0
].
index_
,
0
);
EXPECT_EQ
(layer.
GetKeys
()[
1
].
name_
,
"
to
"
);
EXPECT_EQ
(layer.
GetKeys
()[
1
].
index_
,
1
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
from
"
),
"
100
"
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
to
"
),
"
200
"
);
}
TEST
(LayerTests, TestWithMultipleRanges) {
auto
layer =
Layer
(
"
/abc/:from1-:to1/a/:from2-:to2/:from3-:to3/test/:from4-:to4/:from5-:to5
"
);
EXPECT_EQ
(layer.
GetKeys
().
size
(),
10
);
auto
req = std::make_shared<Request>(
"
/abc/1-2/a/3-4/5-6/test/7-8/9-10
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
10
);
size_t
name_index =
1
;
for
(
size_t
index =
0
; index < req->
GetParams
().
size
();) {
const
auto
from =
"
from
"
+
std::to_string
(name_index);
const
auto
to =
"
to
"
+
std::to_string
(name_index);
EXPECT_EQ
(layer.
GetKeys
()[index].
name_
, from);
EXPECT_EQ
(layer.
GetKeys
()[index].
index_
, index);
EXPECT_EQ
(layer.
GetKeys
()[index +
1
].
name_
, to);
EXPECT_EQ
(layer.
GetKeys
()[index +
1
].
index_
, index +
1
);
EXPECT_EQ
(req->
GetParams
().
at
(from),
std::to_string
(index +
1
));
EXPECT_EQ
(req->
GetParams
().
at
(to),
std::to_string
(index +
2
));
index +=
2
;
++name_index;
}
}
TEST
(LayerTests, TestWithManyKeys) {
auto
layer =
Layer
(
"
/:id1/a/:id2/:id3/:id4/:id5/x/y/z/:id6
"
);
EXPECT_EQ
(layer.
GetKeys
().
size
(),
6
);
auto
req = std::make_shared<Request>(
"
/1/a/2/3/4/5/x/y/z/6
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
6
);
for
(
size_t
index =
0
; index <
6
; ++index) {
const
auto
name =
"
id
"
+
std::to_string
(index +
1
);
EXPECT_EQ
(layer.
GetKeys
()[index].
name_
, name);
EXPECT_EQ
(layer.
GetKeys
()[index].
index_
, index);
EXPECT_EQ
(req->
GetParams
().
at
(name),
std::to_string
(index +
1
));
}
}
TEST
(LayerTests, TestNonMatching) {
auto
layer =
Layer
(
"
/:id1/a/:id2/:id3/:id4/:id5/x/y/z/:id6
"
);
auto
req = std::make_shared<Request>(
"
/1/abc/2/3/4/5/x/y/z/6
"
);
EXPECT_FALSE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
0
);
}
TEST
(LayerTests, TestMultiMatchings) {
auto
layer =
Layer
(
"
/:id1/a/:id2/:id3/:id4/:id5/x/y/z/:id6
"
);
EXPECT_EQ
(layer.
GetKeys
().
size
(),
6
);
auto
req = std::make_shared<Request>(
"
/1/a/2/3/4/5/x/y/z/6
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetParams
().
size
(),
6
);
auto
req2 = std::make_shared<Request>(
"
/1/abc/2/3/4/5/x/y/z/6
"
);
EXPECT_FALSE
(layer.
Match
(req2));
EXPECT_EQ
(req2->
GetParams
().
size
(),
0
);
auto
req3 = std::make_shared<Request>(
"
/1/a/2/3/4/5/x/y/z/6
"
);
EXPECT_TRUE
(layer.
Match
(req3));
EXPECT_EQ
(layer.
GetKeys
().
size
(),
6
);
for
(
size_t
index =
0
; index <
6
; ++index) {
const
auto
name =
"
id
"
+
std::to_string
(index +
1
);
EXPECT_EQ
(layer.
GetKeys
()[index].
name_
, name);
EXPECT_EQ
(layer.
GetKeys
()[index].
index_
, index);
EXPECT_EQ
(req3->
GetParams
().
at
(name),
std::to_string
(index +
1
));
}
}
TEST
(LayerTests, TestQueryParams) {
auto
layer =
Layer
(
"
/abc
"
);
auto
req = std::make_shared<Request>(
"
/abc?key=value
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetQueryParams
().
size
(),
1
);
}
TEST
(LayerTests, TestWithMoreQueryParams) {
auto
layer =
Layer
(
"
/abc
"
);
auto
req = std::make_shared<Request>(
"
/abc?key1=value1&key2=value2&key3=value3
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetQueryParams
().
size
(),
3
);
}
TEST
(LayerTests, TestWithParamsAndQueryParams) {
auto
layer =
Layer
(
"
/abc/:id
"
);
auto
req = std::make_shared<Request>(
"
/abc/123?key1=value1&key2=value2&key3=value3
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetQueryParams
().
size
(),
3
);
EXPECT_EQ
(req->
GetParams
().
size
(),
1
);
for
(
size_t
index =
0
; index < req->
GetQueryParams
().
size
(); ++index) {
const
auto
key =
"
key
"
+
std::to_string
(index +
1
);
const
auto
value =
"
value
"
+
std::to_string
(index +
1
);
EXPECT_EQ
(req->
GetQueryParams
().
count
(key),
1
);
EXPECT_EQ
(req->
GetQueryParams
().
at
(key), value);
}
}
TEST
(LayerTests, TestWithInvalidQueryParams) {
auto
layer =
Layer
(
"
/abc
"
);
auto
req = std::make_shared<Request>(
"
/abc?
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetQueryParams
().
size
(),
0
);
EXPECT_EQ
(req->
GetParams
().
size
(),
0
);
auto
req1 = std::make_shared<Request>(
"
/abc??
"
);
EXPECT_TRUE
(layer.
Match
(req1));
EXPECT_EQ
(req1->
GetQueryParams
().
size
(),
0
);
EXPECT_EQ
(req1->
GetParams
().
size
(),
0
);
auto
req2 = std::make_shared<Request>(
"
/abc&&
"
);
EXPECT_FALSE
(layer.
Match
(req2));
EXPECT_EQ
(req2->
GetQueryParams
().
size
(),
0
);
EXPECT_EQ
(req2->
GetParams
().
size
(),
0
);
auto
req3 = std::make_shared<Request>(
"
/abc?&&&&&??
"
);
EXPECT_TRUE
(layer.
Match
(req3));
EXPECT_EQ
(req3->
GetQueryParams
().
size
(),
0
);
EXPECT_EQ
(req3->
GetParams
().
size
(),
0
);
}
TEST
(LayerTests, TestWithIncompleteQueryParams) {
auto
layer =
Layer
(
"
/abc/:id
"
);
auto
req = std::make_shared<Request>(
"
/abc/123?key1=value1&key2&key3=value3
"
);
EXPECT_TRUE
(layer.
Match
(req));
EXPECT_EQ
(req->
GetQueryParams
().
size
(),
1
);
for
(
size_t
index =
0
; index < req->
GetQueryParams
().
size
(); ++index) {
const
auto
key =
"
key
"
+
std::to_string
(index +
1
);
const
auto
value =
"
value
"
+
std::to_string
(index +
1
);
EXPECT_EQ
(req->
GetQueryParams
().
count
(key),
1
);
EXPECT_EQ
(req->
GetQueryParams
().
at
(key), value);
}
}
Back
|
FazBrowse Home
|
New Git URL