Skip to content

Commit

Permalink
Polish quoting (#2668)
Browse files Browse the repository at this point in the history
* Poslish quoting

* Add CHANGE note

* More tests
  • Loading branch information
asvetlov authored Jan 17, 2018
1 parent 79b0f88 commit e55251c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/2668.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do percent encoding for `.url_for()` parameters
1 change: 1 addition & 0 deletions CHANGES/2668.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Forbid non-strings in `resource.url_for()` parameters.
6 changes: 3 additions & 3 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,7 @@ def _match(self, path):
return None
else:
return {key: URL.build(path=value, encoded=True).path
for key, value in
match.groupdict().items()}
for key, value in match.groupdict().items()}

def raw_match(self, path):
return self._formatter == path
Expand All @@ -416,7 +415,8 @@ def get_info(self):
'pattern': self._pattern}

def url_for(self, **parts):
url = self._formatter.format_map(parts)
url = self._formatter.format_map({k: URL.build(path=v).raw_path
for k, v in parts.items()})
return URL.build(path=url)

def __repr__(self):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,18 @@ def test_route_dynamic_with_regex(router):
handler = make_handler()
route = router.add_route('GET', r'/{one}/{two:.+}', handler)

url = route.url_for(one=1, two=2)
url = route.url_for(one='1', two='2')
assert '/1/2' == str(url)


def test_route_dynamic_quoting(router):
handler = make_handler()
route = router.add_route('GET', r'/{arg}', handler)

url = route.url_for(arg='1 2/текст')
assert '/1%202/%D1%82%D0%B5%D0%BA%D1%81%D1%82' == str(url)


async def test_regular_match_info(router):
handler = make_handler()
router.add_route('GET', '/get/{name}', handler)
Expand Down

0 comments on commit e55251c

Please sign in to comment.