From 21fb1dff7e93f35698ad46cc7f432972e3e63934 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Mon, 27 Feb 2017 19:54:58 -0800 Subject: [PATCH] fix routing issue with slashes --- sanic/router.py | 13 +++++++++---- tests/test_routes.py | 13 +++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/sanic/router.py b/sanic/router.py index 263f9d82..f17ff23d 100644 --- a/sanic/router.py +++ b/sanic/router.py @@ -98,10 +98,15 @@ class Router: def add(self, uri, methods, handler, host=None): # add regular version self._add(uri, methods, handler, host) - slash_is_missing = (not uri[-1].endswith('/') - and not self.routes_all.get(uri + '/', False)) - without_slash_is_missing = (not self.routes_all.get(uri[:-1], False) - and uri is not '/') + slash_is_missing = ( + not uri[-1] == '/' + and not self.routes_all.get(uri + '/', False) + ) + without_slash_is_missing = ( + uri[-1] == '/' + and not self.routes_all.get(uri[:-1], False) + and not uri == '/' + ) # add version with trailing slash if slash_is_missing: self._add(uri + '/', methods, handler, host) diff --git a/tests/test_routes.py b/tests/test_routes.py index d9e4532f..ca2173f0 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -498,6 +498,19 @@ def test_remove_inexistent_route(): with pytest.raises(RouteDoesNotExist): app.remove_route('/test') +def test_removing_slash(): + app = Sanic(__name__) + + @app.get('/rest/') + def get(_): + pass + + @app.post('/rest/') + def post(_): + pass + + assert len(app.router.routes_all.keys()) == 2 + def test_remove_unhashable_route(): app = Sanic('test_remove_unhashable_route')