Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
004c12b5
Unverified
Commit
004c12b5
authored
Jan 31, 2022
by
Kirill Efimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test for Swagger UI static file server
parent
bbcba746
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
0 deletions
+100
-0
tests/meson.build
tests/meson.build
+1
-0
tests/rest_swagger_server_test.cc
tests/rest_swagger_server_test.cc
+99
-0
No files found.
tests/meson.build
View file @
004c12b5
...
@@ -24,6 +24,7 @@ pistache_test_files = [
...
@@ -24,6 +24,7 @@ pistache_test_files = [
'request_size_test',
'request_size_test',
'streaming_test',
'streaming_test',
'rest_server_test',
'rest_server_test',
'rest_swagger_server_test',
'mailbox_test',
'mailbox_test',
'stream_test',
'stream_test',
'reactor_test',
'reactor_test',
...
...
tests/rest_swagger_server_test.cc
0 → 100644
View file @
004c12b5
#include <gtest/gtest.h>
#include <pistache/description.h>
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/peer.h>
#include <pistache/router.h>
#include <pistache/serializer/rapidjson.h>
#include <filesystem>
#include <httplib.h>
using
namespace
std
;
using
namespace
Pistache
;
class
SwaggerEndpoint
{
public:
SwaggerEndpoint
(
Address
addr
)
:
httpEndpoint
(
std
::
make_shared
<
Http
::
Endpoint
>
(
addr
))
,
desc
(
"SwaggerEndpoint API"
,
"1.0"
)
{
}
void
init
()
{
auto
opts
=
Http
::
Endpoint
::
options
().
threads
(
1
);
httpEndpoint
->
init
(
opts
);
}
void
start
()
{
router
.
initFromDescription
(
desc
);
Rest
::
Swagger
swagger
(
desc
);
swagger
.
uiPath
(
"/doc"
)
.
uiDirectory
(
filesystem
::
current_path
()
/
"assets"
)
.
apiPath
(
"/banker-api.json"
)
.
serializer
(
&
Rest
::
Serializer
::
rapidJson
)
.
install
(
router
);
httpEndpoint
->
setHandler
(
router
.
handler
());
httpEndpoint
->
serve
();
}
void
shutdown
()
{
httpEndpoint
->
shutdown
();
}
Port
getPort
()
const
{
return
httpEndpoint
->
getPort
();
}
private:
shared_ptr
<
Http
::
Endpoint
>
httpEndpoint
;
Rest
::
Description
desc
;
Rest
::
Router
router
;
};
TEST
(
rest_server_test
,
basic_test
)
{
filesystem
::
create_directory
(
"assets"
);
ofstream
goodFile
(
"assets/good.txt"
);
goodFile
<<
"good"
;
goodFile
.
close
();
ofstream
badFile
(
"bad.txt"
);
badFile
<<
"bad"
;
badFile
.
close
();
Address
addr
(
Ipv4
::
any
(),
Port
(
0
));
SwaggerEndpoint
swagger
(
addr
);
swagger
.
init
();
std
::
thread
t
(
std
::
bind
(
&
SwaggerEndpoint
::
start
,
swagger
));
while
(
swagger
.
getPort
()
==
0
);
Port
port
=
swagger
.
getPort
();
cout
<<
"Cores = "
<<
hardware_concurrency
()
<<
endl
;
cout
<<
"CWD = "
<<
filesystem
::
current_path
()
<<
endl
;
cout
<<
"Port = "
<<
port
<<
endl
;
httplib
::
Client
client
(
"localhost"
,
port
);
// Test if we have access to files inside the UI folder.
auto
goodRes
=
client
.
Get
(
"/doc/good.txt"
);
ASSERT_EQ
(
goodRes
->
status
,
200
);
ASSERT_EQ
(
goodRes
->
body
,
"good"
);
// Attempt to read file outside of the UI directory should fail even if
// the file exists.
auto
badRes
=
client
.
Get
(
"/doc/../bad.txt"
);
ASSERT_EQ
(
badRes
->
status
,
404
);
ASSERT_NE
(
badRes
->
body
,
"bad"
);
swagger
.
shutdown
();
t
.
join
();
filesystem
::
remove_all
(
"assets"
);
filesystem
::
remove_all
(
"bad.txt"
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment