Merge pull request #1076 from channelcat/1074
fix strict_slashes bug when route has slash
This commit is contained in:
commit
ca0bc1cb7d
|
@ -129,18 +129,22 @@ class Router:
|
||||||
|
|
||||||
# Add versions with and without trailing /
|
# Add versions with and without trailing /
|
||||||
slashed_methods = self.routes_all.get(uri + '/', frozenset({}))
|
slashed_methods = self.routes_all.get(uri + '/', frozenset({}))
|
||||||
|
unslashed_methods = self.routes_all.get(uri[:-1], frozenset({}))
|
||||||
if isinstance(methods, Iterable):
|
if isinstance(methods, Iterable):
|
||||||
_slash_is_missing = all(method in slashed_methods for
|
_slash_is_missing = all(method in slashed_methods for
|
||||||
method in methods)
|
method in methods)
|
||||||
|
_without_slash_is_missing = all(method in unslashed_methods for
|
||||||
|
method in methods)
|
||||||
else:
|
else:
|
||||||
_slash_is_missing = methods in slashed_methods
|
_slash_is_missing = methods in slashed_methods
|
||||||
|
_without_slash_is_missing = methods in unslashed_methods
|
||||||
|
|
||||||
slash_is_missing = (
|
slash_is_missing = (
|
||||||
not uri[-1] == '/' and not _slash_is_missing
|
not uri[-1] == '/' and not _slash_is_missing
|
||||||
)
|
)
|
||||||
without_slash_is_missing = (
|
without_slash_is_missing = (
|
||||||
uri[-1] == '/' and not
|
uri[-1] == '/' and not
|
||||||
self.routes_all.get(uri[:-1], False) and not
|
_without_slash_is_missing and not
|
||||||
uri == '/'
|
uri == '/'
|
||||||
)
|
)
|
||||||
# add version with trailing slash
|
# add version with trailing slash
|
||||||
|
|
|
@ -129,6 +129,30 @@ def test_route_strict_slash_default_value_can_be_overwritten():
|
||||||
request, response = app.test_client.get('/get/')
|
request, response = app.test_client.get('/get/')
|
||||||
assert response.text == 'OK'
|
assert response.text == 'OK'
|
||||||
|
|
||||||
|
def test_route_slashes_overload():
|
||||||
|
app = Sanic('test_route_slashes_overload')
|
||||||
|
|
||||||
|
@app.get('/hello/')
|
||||||
|
def handler(request):
|
||||||
|
return text('OK')
|
||||||
|
|
||||||
|
@app.post('/hello/')
|
||||||
|
def handler(request):
|
||||||
|
return text('OK')
|
||||||
|
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/hello')
|
||||||
|
assert response.text == 'OK'
|
||||||
|
|
||||||
|
request, response = app.test_client.get('/hello/')
|
||||||
|
assert response.text == 'OK'
|
||||||
|
|
||||||
|
request, response = app.test_client.post('/hello')
|
||||||
|
assert response.text == 'OK'
|
||||||
|
|
||||||
|
request, response = app.test_client.post('/hello/')
|
||||||
|
assert response.text == 'OK'
|
||||||
|
|
||||||
def test_route_optional_slash():
|
def test_route_optional_slash():
|
||||||
app = Sanic('test_route_optional_slash')
|
app = Sanic('test_route_optional_slash')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user