FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
expresscpp/test/static_file_middleware.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
/
static_file_middleware.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (103 loc) · 4.04 KB
Breadcrumbs
expresscpp
/
test
/
static_file_middleware.cpp
Copy path
File metadata and controls
127 lines (103 loc) · 4.04 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
#
include
<
algorithm
>
#
include
<
fstream
>
#
include
<
iostream
>
#
include
<
string
>
#
include
"
boost/uuid/uuid_generators.hpp
"
#
include
"
boost/uuid/uuid_io.hpp
"
#
include
"
expresscpp/console.hpp
"
#
include
"
expresscpp/expresscpp.hpp
"
#
include
"
expresscpp/fetch.hpp
"
#
include
"
expresscpp/middleware/serve_static_provider.hpp
"
#
include
"
gtest/gtest.h
"
#
include
"
nlohmann/json.hpp
"
using
namespace
expresscpp
;
constexpr
uint16_t
port =
8081u
;
TEST
(StaticFileMiddleware, ServeIndexHtml) {
auto
expresscpp = std::make_shared<ExpressCpp>();
//
create temp folder for html files
const
auto
uuid_ =
boost::uuids::random_generator
()();
const
auto
tmp_folder =
std::filesystem::temp_directory_path
();
const
std::string doc_root = tmp_folder.
string
() +
"
/www-
"
+
boostUUIDToString
(uuid_);
std::filesystem::create_directory
(doc_root);
std::string index_html_content =
R"(
<!doctype html>
<html>
<head>
<title>This is the title of the webpage!</title>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
)"
;
std::string index_doc_content =
R"(
{"status": "ok"}
)"
;
//
create html files
{
std::filesystem::path path_to_index_html = doc_root +
"
/index.html
"
;
std::ofstream
index_html_file
(path_to_index_html);
index_html_file << index_html_content << std::endl;
index_html_file.
close
();
assert
(
std::filesystem::exists
(path_to_index_html));
}
//
create other doc files such as json
{
std::filesystem::path path_to_doc = doc_root +
"
/doc.json
"
;
std::ofstream
index_doc_file
(path_to_doc);
index_doc_file << index_doc_content << std::endl;
index_doc_file.
close
();
assert
(
std::filesystem::exists
(path_to_doc));
}
StaticFileProvider
static_file_provider
(doc_root);
expresscpp->
Use
(static_file_provider);
expresscpp->
Listen
(port, [=](
auto
ec) {
EXPECT_FALSE
(ec);
{
//
should get the index.html file
auto
index_html_contect_response =
fetch
(
fmt::format
(
"
localhost:{}/
"
, port), { HttpMethod::Get});
index_html_contect_response.
erase
(
std::remove
(index_html_contect_response.
begin
(), index_html_contect_response.
end
(),
'
\n
'
),
index_html_contect_response.
end
());
std::string index_html_content_expected = index_html_content;
index_html_content_expected.
erase
(
std::remove
(index_html_content_expected.
begin
(), index_html_content_expected.
end
(),
'
\n
'
),
index_html_content_expected.
end
());
EXPECT_EQ
(index_html_contect_response, index_html_content_expected);
}
{
//
should get the doc.json file
auto
json_content_response =
fetch
(
fmt::format
(
"
localhost:{}/doc.json
"
, port));
const
auto
result =
nlohmann::json::parse
(json_content_response);
const
auto
expected =
nlohmann::json::parse
(index_doc_content);
EXPECT_EQ
(expected[
"
status
"
], expected[
"
status
"
]);
}
});
}
TEST
(StaticFileMiddleware, ServeIndexHtmlWithQuery) {
auto
expresscpp = std::make_shared<ExpressCpp>();
//
create temp folder for html files
const
auto
uuid_ =
boost::uuids::random_generator
()();
const
auto
tmp_folder =
std::filesystem::temp_directory_path
();
const
std::string doc_root = tmp_folder.
string
() +
"
/www-
"
+
boostUUIDToString
(uuid_);
std::filesystem::create_directory
(doc_root);
std::string index_doc_content =
R"(
{"status": "ok"}
)"
;
//
create other doc files such as json
{
std::filesystem::path path_to_doc = doc_root +
"
/doc.json
"
;
std::ofstream
index_doc_file
(path_to_doc);
index_doc_file << index_doc_content << std::endl;
index_doc_file.
close
();
assert
(
std::filesystem::exists
(path_to_doc));
}
StaticFileProvider
static_file_provider
(doc_root);
expresscpp->
Use
(static_file_provider);
expresscpp->
Listen
(port, [=](
auto
ec) {
EXPECT_FALSE
(ec);
{
//
should get the doc.json file
auto
json_content_response =
fetch
(
fmt::format
(
"
localhost:{}/doc.json?v=444
"
, port));
const
auto
result =
nlohmann::json::parse
(json_content_response);
const
auto
expected =
nlohmann::json::parse
(index_doc_content);
EXPECT_EQ
(expected[
"
status
"
], expected[
"
status
"
]);
}
});
}
Back
|
FazBrowse Home
|
New Git URL