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
552a51b3
Commit
552a51b3
authored
Feb 17, 2019
by
knowledge4igor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add static file send test + more logs in tests
parent
267a718a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
86 additions
and
5 deletions
+86
-5
tests/http_server_test.cc
tests/http_server_test.cc
+86
-5
No files found.
tests/http_server_test.cc
View file @
552a51b3
#include <pistache/async.h>
#include <pistache/http.h>
#include <pistache/http.h>
#include <pistache/client.h>
#include <pistache/client.h>
#include <pistache/endpoint.h>
#include <pistache/endpoint.h>
...
@@ -6,6 +7,8 @@
...
@@ -6,6 +7,8 @@
#include <chrono>
#include <chrono>
#include <future>
#include <future>
#include <fstream>
#include <string>
using
namespace
Pistache
;
using
namespace
Pistache
;
...
@@ -44,8 +47,29 @@ struct SlowHandlerOnSpecialPage : public Http::Handler {
...
@@ -44,8 +47,29 @@ struct SlowHandlerOnSpecialPage : public Http::Handler {
int
delay_
;
int
delay_
;
};
};
struct
FileHandler
:
public
Http
::
Handler
{
HTTP_PROTOTYPE
(
FileHandler
)
int
clientLogicFunc
(
int
response_size
,
const
std
::
string
&
server_page
,
int
wait_seconds
)
{
explicit
FileHandler
(
const
std
::
string
&
fileName
)
:
fileName_
(
fileName
)
{
}
void
onRequest
(
const
Http
::
Request
&
/*request*/
,
Http
::
ResponseWriter
writer
)
override
{
Http
::
serveFile
(
writer
,
fileName_
).
then
([
this
](
ssize_t
bytes
)
{
std
::
cout
<<
"Sent "
<<
bytes
<<
" bytes from "
<<
fileName_
<<
" file"
<<
std
::
endl
;
},
Async
::
IgnoreException
);
}
private:
std
::
string
fileName_
;
};
int
clientLogicFunc
(
int
response_size
,
const
std
::
string
&
server_page
,
int
wait_seconds
)
{
Http
::
Client
client
;
Http
::
Client
client
;
client
.
init
();
client
.
init
();
...
@@ -55,10 +79,15 @@ int clientLogicFunc(int response_size, const std::string& server_page, int wait_
...
@@ -55,10 +79,15 @@ int clientLogicFunc(int response_size, const std::string& server_page, int wait_
for
(
int
i
=
0
;
i
<
response_size
;
++
i
)
for
(
int
i
=
0
;
i
<
response_size
;
++
i
)
{
{
auto
response
=
rb
.
send
();
auto
response
=
rb
.
send
();
response
.
then
([
&
](
Http
::
Response
rsp
)
{
response
.
then
([
&
counter
](
Http
::
Response
resp
)
if
(
rsp
.
code
()
==
Http
::
Code
::
Ok
)
{
++
counter
;
std
::
cout
<<
"Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
},
Async
::
IgnoreException
);
if
(
resp
.
code
()
==
Http
::
Code
::
Ok
)
{
++
counter
;
}
},
Async
::
IgnoreException
);
responses
.
push_back
(
std
::
move
(
response
));
responses
.
push_back
(
std
::
move
(
response
));
}
}
...
@@ -190,3 +219,55 @@ TEST(http_server_test, multiple_client_with_different_requests_to_multithreaded_
...
@@ -190,3 +219,55 @@ TEST(http_server_test, multiple_client_with_different_requests_to_multithreaded_
ASSERT_TRUE
(
true
);
ASSERT_TRUE
(
true
);
}
}
}
}
TEST
(
http_server_test
,
server_with_static_file
)
{
const
std
::
string
data
(
"Hello, World!"
);
char
fileName
[
PATH_MAX
]
=
"/tmp/pistacheioXXXXXX"
;
mkstemp
(
fileName
);
std
::
cout
<<
"Creating temporary file: "
<<
fileName
<<
'\n'
;
std
::
ofstream
tmpFile
;
tmpFile
.
open
(
fileName
);
tmpFile
<<
data
;
tmpFile
.
close
();
const
Pistache
::
Address
address
(
"localhost"
,
Pistache
::
Port
(
0
));
Http
::
Endpoint
server
(
address
);
auto
flags
=
Tcp
::
Options
::
InstallSignalHandler
|
Tcp
::
Options
::
ReuseAddr
;
auto
server_opts
=
Http
::
Endpoint
::
options
().
flags
(
flags
);
server
.
init
(
server_opts
);
server
.
setHandler
(
Http
::
make_handler
<
FileHandler
>
(
fileName
));
server
.
serveThreaded
();
const
std
::
string
server_address
=
"localhost:"
+
server
.
getPort
().
toString
();
std
::
cout
<<
"Server address: "
<<
server_address
<<
"
\n
"
;
Http
::
Client
client
;
client
.
init
();
auto
rb
=
client
.
get
(
server_address
);
auto
response
=
rb
.
send
();
std
::
string
resultData
;
response
.
then
([
&
resultData
](
Http
::
Response
resp
)
{
std
::
cout
<<
"Response code is "
<<
resp
.
code
()
<<
std
::
endl
;
if
(
resp
.
code
()
==
Http
::
Code
::
Ok
)
{
resultData
=
resp
.
body
();
}
},
Async
::
Throw
);
const
int
WAIT_TIME
=
2
;
Async
::
Barrier
<
Http
::
Response
>
barrier
(
response
);
barrier
.
wait_for
(
std
::
chrono
::
seconds
(
WAIT_TIME
));
client
.
shutdown
();
server
.
shutdown
();
std
::
cout
<<
"Deleting file "
<<
fileName
<<
std
::
endl
;
unlink
(
fileName
);
ASSERT_EQ
(
data
,
resultData
);
}
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