FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
expresscpp/test/hello_world.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
/
hello_world.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (97 loc) · 3.37 KB
Breadcrumbs
expresscpp
/
test
/
hello_world.cpp
Copy path
File metadata and controls
113 lines (97 loc) · 3.37 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
#
include
"
expresscpp/console.hpp
"
#
include
"
expresscpp/expresscpp.hpp
"
#
include
"
expresscpp/fetch.hpp
"
#
include
"
expresscpp/types.hpp
"
#
include
"
gtest/gtest.h
"
#
include
"
nlohmann/json.hpp
"
#
include
"
test_utils.hpp
"
using
namespace
std
::literals
;
using
namespace
expresscpp
;
constexpr
uint16_t
port =
8081u
;
TEST
(HelloWorld, TemplateHandler) {
ExpressCpp app;
app.
Get
(
"
/a
"
, [&](
auto
/*
req
*/
,
auto
res,
auto
/*
next
*/
) {
Console::Debug
(
"
/a called
"
);
res->
Json
(
R"(
{"status":"ok"}
)"
);
});
app.
Get
(
"
/b
"
, [&](
auto
/*
req
*/
,
auto
res) {
Console::Debug
(
"
/b called
"
);
res->
Json
(
R"(
{"status":"ok"}
)"
);
});
app.
Listen
(port, [&](
auto
ec) {
ASSERT_FALSE
(ec);
const
auto
a =
fetch
(
fmt::format
(
"
localhost:{}/a
"
, port));
EXPECT_EQ
(a,
R"(
{"status":"ok"}
)"
);
const
auto
b =
fetch
(
fmt::format
(
"
localhost:{}/b
"
, port));
});
}
TEST
(HelloWorld, UseRouter) {
TestCallSleeper
sleeper
(
2
);
ExpressCpp app;
app.
Get
(
"
/
"
, [&](
auto
/*
req
*/
,
auto
res,
auto
/*
next
*/
) {
Console::Debug
(
"
/ called
"
);
res->
Json
(
R"(
{"status":"ok"}
)"
);
sleeper.
Call
();
});
const
auto
json_response =
R"(
{"users":
[
{"id": "39811e5d-cb4b-4dcf-a857-df3813fa89f3"},
{"id": "4edfc753-b817-42d5-9266-d045f8945e2e"}
]
}
)"
sv;
app.
Get
(
"
/api/v0/users
"
, [&](
auto
/*
req
*/
,
auto
res,
auto
/*
next
*/
) {
Console::Debug
(
"
/api/v0/users called
"
);
res->
Json
(json_response);
sleeper.
Call
();
});
app.
Listen
(port, [&](
auto
ec) {
ASSERT_FALSE
(ec);
const
auto
s =
fetch
(
fmt::format
(
"
localhost:{}/
"
, port), { HttpMethod::Get});
EXPECT_EQ
(s,
R"(
{"status":"ok"}
)"
);
const
auto
ss =
fetch
(
fmt::format
(
"
localhost:{}/api/v0/users
"
, port), { HttpMethod::Get});
const
auto
expected =
nlohmann::json::parse
(json_response);
const
auto
received =
nlohmann::json::parse
(ss);
const
std::string expected_string = expected.
dump
();
const
std::string received_string = received.
dump
();
EXPECT_EQ
(expected_string, received_string);
});
EXPECT_TRUE
(sleeper.
Wait
());
}
TEST
(HelloWorld, UseRouterWithParams) {
TestCallSleeper
sleeper
(
1
);
ExpressCpp app;
app.
Get
(
"
/things/:id
"
, [&](
auto
req,
auto
res,
auto
/*
next
*/
) {
Console::Debug
(
"
/ called
"
);
EXPECT_EQ
(req->
GetParams
().
size
(),
1
);
EXPECT_EQ
(req->
GetParams
().
at
(
"
id
"
),
"
198
"
);
res->
Json
(
R"(
{"status":"ok"}
)"
);
sleeper.
Call
();
});
constexpr
uint16_t
port =
8081u
;
app.
Listen
(port, [&](
auto
ec) {
EXPECT_FALSE
(ec);
const
auto
ss =
fetch
(
fmt::format
(
"
localhost:{}/things/198
"
, port), { HttpMethod::Get});
EXPECT_EQ
(ss,
R"(
{"status":"ok"}
)"
);
});
EXPECT_TRUE
(sleeper.
Wait
());
}
TEST
(HelloWorld, UseRouterWithQueryParams) {
TestCallSleeper
sleeper
(
1
);
ExpressCpp app;
app.
Get
(
"
/things
"
, [&](
auto
req,
auto
res,
auto
/*
next
*/
) {
Console::Debug
(
"
/ called
"
);
EXPECT_EQ
(req->
GetParams
().
size
(),
0
);
EXPECT_EQ
(req->
GetQueryParams
().
size
(),
2
);
EXPECT_EQ
(req->
GetQueryParams
().
at
(
"
id
"
),
"
198
"
);
EXPECT_EQ
(req->
GetQueryParams
().
at
(
"
key
"
),
"
value
"
);
res->
Json
(
R"(
{"status":"ok"}
)"
);
sleeper.
Call
();
});
constexpr
uint16_t
port =
8081u
;
app.
Listen
(port, [&](
auto
ec) {
EXPECT_FALSE
(ec);
const
auto
ss =
fetch
(
fmt::format
(
"
localhost:{}/things?id=198&key=value
"
, port), { HttpMethod::Get});
EXPECT_EQ
(ss,
R"(
{"status":"ok"}
)"
);
});
EXPECT_TRUE
(sleeper.
Wait
());
}
Back
|
FazBrowse Home
|
New Git URL