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
6e432b3f
Commit
6e432b3f
authored
Dec 31, 2018
by
Dennis Jenkins
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Resolves bug #323: double invocation of router's notFound handler.
parent
5f62bfd7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
14 deletions
+66
-14
src/server/router.cc
src/server/router.cc
+6
-14
tests/router_test.cc
tests/router_test.cc
+60
-0
No files found.
src/server/router.cc
View file @
6e432b3f
...
@@ -321,19 +321,7 @@ RouterHandler::onRequest(
...
@@ -321,19 +321,7 @@ RouterHandler::onRequest(
Http
::
ResponseWriter
response
)
Http
::
ResponseWriter
response
)
{
{
auto
resp
=
response
.
clone
();
auto
resp
=
response
.
clone
();
auto
result
=
router
->
route
(
req
,
std
::
move
(
resp
));
router
->
route
(
req
,
std
::
move
(
resp
));
/* @Feature: add support for a custom NotFound handler */
if
(
result
==
Route
::
Status
::
NotFound
)
{
if
(
router
->
hasNotFoundHandler
())
{
auto
resp2
=
response
.
clone
();
router
->
invokeNotFoundHandler
(
req
,
std
::
move
(
resp2
));
}
else
response
.
send
(
Http
::
Code
::
Not_Found
,
"Could not find a matching route"
);
}
}
}
}
// namespace Private
}
// namespace Private
...
@@ -453,7 +441,11 @@ Router::route(const Http::Request& req, Http::ResponseWriter response) {
...
@@ -453,7 +441,11 @@ Router::route(const Http::Request& req, Http::ResponseWriter response) {
if
(
handler1
==
Route
::
Result
::
Ok
)
return
Route
::
Status
::
Match
;
if
(
handler1
==
Route
::
Result
::
Ok
)
return
Route
::
Status
::
Match
;
}
}
if
(
hasNotFoundHandler
())
invokeNotFoundHandler
(
req
,
std
::
move
(
response
));
if
(
hasNotFoundHandler
())
{
invokeNotFoundHandler
(
req
,
std
::
move
(
response
));
}
else
{
response
.
send
(
Http
::
Code
::
Not_Found
,
"Could not find a matching route"
);
}
return
Route
::
Status
::
NotFound
;
return
Route
::
Status
::
NotFound
;
}
}
...
...
tests/router_test.cc
View file @
6e432b3f
...
@@ -8,8 +8,13 @@
...
@@ -8,8 +8,13 @@
#include "gtest/gtest.h"
#include "gtest/gtest.h"
#include <algorithm>
#include <algorithm>
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/router.h>
#include <pistache/router.h>
#include "httplib.h"
using
namespace
Pistache
;
using
namespace
Pistache
::
Rest
;
using
namespace
Pistache
::
Rest
;
bool
match
(
const
SegmentTreeNode
&
routes
,
const
std
::
string
&
req
)
{
bool
match
(
const
SegmentTreeNode
&
routes
,
const
std
::
string
&
req
)
{
...
@@ -144,3 +149,58 @@ TEST(router_test, test_mixed) {
...
@@ -144,3 +149,58 @@ TEST(router_test, test_mixed) {
ASSERT_FALSE
(
matchSplat
(
routes
,
"/hello"
,
{
"hello"
}));
ASSERT_FALSE
(
matchSplat
(
routes
,
"/hello"
,
{
"hello"
}));
ASSERT_TRUE
(
matchSplat
(
routes
,
"/hi"
,
{
"hi"
}));
ASSERT_TRUE
(
matchSplat
(
routes
,
"/hi"
,
{
"hi"
}));
}
}
TEST
(
router_test
,
test_notfound_exactly_once
)
{
Address
addr
(
Ipv4
::
any
(),
0
);
auto
endpoint
=
std
::
make_shared
<
Http
::
Endpoint
>
(
addr
);
auto
opts
=
Http
::
Endpoint
::
options
().
threads
(
1
).
maxPayload
(
4096
);
endpoint
->
init
(
opts
);
int
count_found
=
0
;
int
count_not_found
=
0
;
Rest
::
Router
router
;
Routes
::
NotFound
(
router
,
[
&
count_not_found
](
const
Pistache
::
Rest
::
Request
&
request
,
Pistache
::
Http
::
ResponseWriter
response
)
{
count_not_found
++
;
std
::
string
err
{
"Couldn't find route:
\"
"
+
request
.
resource
()
+
"
\"\n
"
};
response
.
send
(
Pistache
::
Http
::
Code
::
Not_Found
,
err
);
return
Pistache
::
Rest
::
Route
::
Result
::
Ok
;
});
Routes
::
Get
(
router
,
"/moogle"
,
[
&
count_found
](
const
Pistache
::
Rest
::
Request
&
,
Pistache
::
Http
::
ResponseWriter
response
)
{
count_found
++
;
response
.
send
(
Pistache
::
Http
::
Code
::
Ok
,
"kupo!
\n
"
);
return
Pistache
::
Rest
::
Route
::
Result
::
Ok
;
});
endpoint
->
setHandler
(
router
.
handler
());
endpoint
->
serveThreaded
();
const
auto
bound_port
=
endpoint
->
getPort
();
httplib
::
Client
client
(
"localhost"
,
bound_port
);
// Verify that the notFound handler is NOT called when route is found.
count_not_found
=
count_found
=
0
;
client
.
Get
(
"/moogle"
);
ASSERT_EQ
(
count_found
,
1
);
ASSERT_EQ
(
count_not_found
,
0
);
// Verify simple solution to bug #323 (one bad url triggered 2 routes).
count_not_found
=
count_found
=
0
;
client
.
Get
(
"/kefka"
);
ASSERT_EQ
(
count_found
,
0
);
ASSERT_EQ
(
count_not_found
,
1
);
// Anal test, 2 calls = 2 route hits.
count_not_found
=
count_found
=
0
;
client
.
Get
(
"/vicks"
);
client
.
Get
(
"/wedge"
);
ASSERT_EQ
(
count_found
,
0
);
ASSERT_EQ
(
count_not_found
,
2
);
endpoint
->
shutdown
();
}
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