Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement for normalize_path_middleware #1995

Merged
merged 2 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changes
2.3.0 (2017-xx-xx)
------------------

-
- Improvement for `normalize_path_middleware`. Added possibility to handle
URLs with query string. #1995

-

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Vladimir Kozlovski
Vladimir Rutsky
Vladimir Shulyak
Vladimir Zakharov
Vladyslav Bondar
W. Trevor King
Will McGugan
Willem de Groot
Expand Down
11 changes: 9 additions & 2 deletions aiohttp/web_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ def middleware(request):

if isinstance(request.match_info.route, SystemRoute):
paths_to_check = []
path = request.raw_path
if '?' in request.raw_path:
path, query = request.raw_path.split('?', 1)
if query:
query = '?' + query
else:
query = ''
path = request.raw_path

if merge_slashes:
paths_to_check.append(re.sub('//+', '/', path))
if append_slash and not request.path.endswith('/'):
Expand All @@ -67,7 +74,7 @@ def middleware(request):
resolves, request = yield from _check_request_resolves(
request, path)
if resolves:
return redirect_class(request.path)
return redirect_class(request.path + query)

return (yield from handler(request))

Expand Down
37 changes: 33 additions & 4 deletions tests/test_web_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ class TestNormalizePathMiddleware:
('/resource1', 200),
('/resource1/', 404),
('/resource2', 200),
('/resource2/', 200)
('/resource2/', 200),
('/resource1?p1=1&p2=2', 200),
('/resource1/?p1=1&p2=2', 404),
('/resource2?p1=1&p2=2', 200),
('/resource2/?p1=1&p2=2', 200)
])
def test_add_trailing_when_necessary(
self, path, status, cli):
Expand All @@ -134,7 +138,11 @@ def test_add_trailing_when_necessary(
('/resource1', 200),
('/resource1/', 404),
('/resource2', 404),
('/resource2/', 200)
('/resource2/', 200),
('/resource1?p1=1&p2=2', 200),
('/resource1/?p1=1&p2=2', 404),
('/resource2?p1=1&p2=2', 404),
('/resource2/?p1=1&p2=2', 200)
])
def test_no_trailing_slash_when_disabled(
self, path, status, cli):
Expand All @@ -153,7 +161,13 @@ def test_no_trailing_slash_when_disabled(
('//resource1//a//b/', 404),
('///resource1//a//b', 200),
('/////resource1/a///b', 200),
('/////resource1/a//b/', 404)
('/////resource1/a//b/', 404),
('/resource1/a/b?p=1', 200),
('//resource1//a//b?p=1', 200),
('//resource1//a//b/?p=1', 404),
('///resource1//a//b?p=1', 200),
('/////resource1/a///b?p=1', 200),
('/////resource1/a//b/?p=1', 404),
])
def test_merge_slash(self, path, status, cli):
extra_middlewares = [
Expand All @@ -179,7 +193,22 @@ def test_merge_slash(self, path, status, cli):
('///resource2//a//b', 200),
('///resource2//a//b/', 200),
('/////resource2/a///b', 200),
('/////resource2/a///b/', 200)
('/////resource2/a///b/', 200),
('/resource1/a/b?p=1', 200),
('/resource1/a/b/?p=1', 404),
('//resource2//a//b?p=1', 200),
('//resource2//a//b/?p=1', 200),
('///resource1//a//b?p=1', 200),
('///resource1//a//b/?p=1', 404),
('/////resource1/a///b?p=1', 200),
('/////resource1/a///b/?p=1', 404),
('/resource2/a/b?p=1', 200),
('//resource2//a//b?p=1', 200),
('//resource2//a//b/?p=1', 200),
('///resource2//a//b?p=1', 200),
('///resource2//a//b/?p=1', 200),
('/////resource2/a///b?p=1', 200),
('/////resource2/a///b/?p=1', 200)
])
def test_append_and_merge_slash(self, path, status, cli):
extra_middlewares = [
Expand Down