Skip to content

Commit

Permalink
Improvement for normalize_path_middleware (#1995)
Browse files Browse the repository at this point in the history
* Improvement for normalize_path_middleware. Now it could handle URLs with query string

* Pull request number.
  • Loading branch information
lemurchik authored and asvetlov committed Jun 21, 2017
1 parent 44ebcac commit 6f2adac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
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

0 comments on commit 6f2adac

Please sign in to comment.