FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
expresscpp/test/middleware.cpp at master · expresscpp/expresscpp · GitHub
expresscpp
/
expresscpp
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
96
Code
Issues
5
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
expresscpp
/
test
/
middleware.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
156 lines (137 loc) · 4.78 KB
Breadcrumbs
expresscpp
/
test
/
middleware.cpp
Copy path
File metadata and controls
156 lines (137 loc) · 4.78 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
#
include
"
expresscpp/console.hpp
"
#
include
"
expresscpp/date.hpp
"
#
include
"
expresscpp/expresscpp.hpp
"
#
include
"
expresscpp/fetch.hpp
"
#
include
"
expresscpp/router.hpp
"
#
include
"
gtest/gtest.h
"
#
include
"
test_utils.hpp
"
using
namespace
expresscpp
;
constexpr
uint16_t
port =
8081u
;
TEST
(MiddlewareTests, LoggerLikeMiddleware) {
ExpressCpp app;
constexpr
std::string_view message =
"
hello world
"
;
bool
logger_called =
false
;
bool
route_called =
false
;
auto
LoggerMiddleware = [&](
auto
req,
auto
/*
res
*/
,
auto
next) {
const
auto
n = req->
getTimeStamp
();
Console::Debug
(
fmt::format
(
"
time: {}, method:
\"
{}
\"
, path:
\"
{}
\"
"
, n,
getHttpMethodName
(req->
getMethod
()), req->
getPath
()));
logger_called =
true
;
next
();
};
app.
Use
(LoggerMiddleware);
app.
Get
(
"
/a
"
, [&](
auto
/*
req
*/
,
auto
res,
auto
/*
next
*/
) {
route_called =
true
;
res->
Send
(message.
data
());
});
auto
stack = app.
Stack
();
app.
Listen
(port, [&](
auto
ec) {
EXPECT_FALSE
(ec);
EXPECT_EQ
(logger_called,
false
);
const
auto
get_response =
fetch
(
fmt::format
(
"
http://localhost:{}/a
"
, port));
EXPECT_EQ
(route_called,
true
);
EXPECT_EQ
(get_response, message);
EXPECT_EQ
(logger_called,
true
);
});
}
void
auth_like_middleware
() {
TestCallSleeper
sleeper
(
2
);
ExpressCpp app;
constexpr
std::string_view error_message =
"
Access Denied
"
;
constexpr
std::string_view success_message =
"
hello world
"
;
bool
authorized =
false
;
auto
AuthMiddleware = [&](
auto
req,
auto
res,
auto
next) {
sleeper.
Call
();
auto
headers = req->
getHeaders
();
if
(headers.
find
(
"
Authorization
"
) == headers.
end
()) {
authorized =
false
;
res->
SetStatus
(
401
);
res->
Send
(error_message.
data
());
}
else
{
if
(req->
getHeaders
()[
"
Authorization
"
] ==
"
secret_token
"
) {
authorized =
true
;
next
();
}
}
};
app.
Use
(AuthMiddleware);
app.
Get
(
"
/secret
"
, [&](
auto
/*
req
*/
,
auto
res,
auto
/*
next
*/
) { res->
Send
(success_message.
data
()); });
app.
Listen
(port, [&](
auto
ec) {
EXPECT_FALSE
(ec);
EXPECT_EQ
(authorized,
false
);
{
const
auto
get_response =
fetch
(
fmt::format
(
"
http://localhost:{}/secret
"
, port), { HttpMethod::Get});
EXPECT_EQ
(get_response, error_message);
EXPECT_EQ
(authorized,
false
);
}
{
std::map<std::string, std::string> headers{{
"
Authorization
"
,
"
secret_token
"
}};
const
auto
get_response =
fetch
(
fmt::format
(
"
http://localhost:{}/secret
"
, port), { HttpMethod::Get, headers});
EXPECT_EQ
(get_response, success_message);
EXPECT_EQ
(authorized,
true
);
}
});
EXPECT_TRUE
(sleeper.
Wait
());
}
TEST
(MiddlewareTests, AuthLikeMiddleware) {
auth_like_middleware
();
}
TEST
(MiddlewareTests, AuthLikeMiddlewareStress) {
for
(
int
i =
0
; i <
1000
; ++i) {
auth_like_middleware
();
}
}
TEST
(MiddlewareTests, DISABLED_SpecialAuthLikeMiddleware) {
{
ExpressCpp app;
constexpr
std::string_view secret_message =
"
secret token
"
;
constexpr
std::string_view not_secret_message =
"
hello world
"
;
constexpr
std::string_view error_message =
"
Access Denied
"
;
bool
auth_called =
false
;
bool
authorized =
false
;
auto
AuthMiddleware = [&](
auto
req,
auto
res,
auto
next) {
auth_called =
true
;
if
(req->
getHeaders
().
find
(
"
Authorization
"
) != req->
getHeaders
().
end
()) {
if
(req->
getHeaders
()[
"
Authorization
"
] ==
"
secret_token
"
) {
authorized =
true
;
next
();
}
}
else
{
authorized =
false
;
res->
SetStatus
(
401
);
res->
Send
(error_message.
data
());
}
};
app.
Use
(
"
/secret
"
, AuthMiddleware);
app.
Use
(
"
/not_secret
"
).
Get
([&](
auto
/*
req
*/
,
auto
res,
auto
) { res->
Send
(not_secret_message.
data
()); });
app.
Use
(
"
/secret
"
).
Get
([&](
auto
/*
req
*/
,
auto
res,
auto
) { res->
Send
(secret_message.
data
()); });
app.
Listen
(port, [&](
auto
ec) {
EXPECT_FALSE
(ec);
EXPECT_EQ
(auth_called,
false
);
EXPECT_EQ
(authorized,
false
);
{
const
auto
get_response =
fetch
(
fmt::format
(
"
http://localhost:{}/not_secret
"
, port), { HttpMethod::Get});
EXPECT_EQ
(get_response, not_secret_message);
EXPECT_EQ
(auth_called,
false
);
EXPECT_EQ
(authorized,
false
);
}
{
const
auto
get_response =
fetch
(
fmt::format
(
"
http://localhost:{}/secret
"
, port), { HttpMethod::Get});
EXPECT_EQ
(get_response, error_message);
EXPECT_EQ
(auth_called,
true
);
EXPECT_EQ
(authorized,
false
);
}
{
std::map<std::string, std::string> headers;
headers[
"
Authorization
"
] =
"
secret_token
"
;
const
auto
get_response =
fetch
(
fmt::format
(
"
http://localhost:{}/secret
"
, port), { HttpMethod::Get, headers});
EXPECT_EQ
(get_response, secret_message);
EXPECT_EQ
(auth_called,
true
);
EXPECT_EQ
(authorized,
true
);
}
});
}
}
Back
|
FazBrowse Home
|
New Git URL