misc: support variadic URL with trailing / (#10813)

This commit is contained in:
Thomas NOËL 2016-05-03 16:59:27 +02:00
parent 334a4d3b34
commit 89924c1940
2 changed files with 23 additions and 9 deletions

View File

@ -9,6 +9,12 @@ def test_url_scheme():
{'https': 's'}) == 'https://www.example.net/foobar'
assert get_variadic_url('http[https]://www.example.net/foobar',
{'https': ''}) == 'http://www.example.net/foobar'
assert get_variadic_url('http[https]://www.example.net/foobar/',
{'https': ''}) == 'http://www.example.net/foobar/'
assert get_variadic_url('http[https]://www.example.net/foo/bar/',
{'https': ''}) == 'http://www.example.net/foo/bar/'
assert get_variadic_url('http[https]://www.example.net/foo/bar',
{'https': ''}) == 'http://www.example.net/foo/bar'
def test_url_netloc():
assert get_variadic_url('http://[hostname]/foobar',
@ -61,12 +67,19 @@ def test_url_whole():
{'url': 'http://www.example.net/foobar'}) == 'http://www.example.net/foobar'
def test_url_server():
assert get_variadic_url('[url]/foobar',
{'url': 'http://www.example.net'}) == 'http://www.example.net/foobar'
assert get_variadic_url('[url]/foobar',
{'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar'
assert get_variadic_url('[url]foobar',
{'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar'
for url in ('http://www.example.net', 'http://www.example.net/'):
assert get_variadic_url('[url]/foobar',
{'url': url}) == 'http://www.example.net/foobar'
assert get_variadic_url('[url]/foobar',
{'url': url}) == 'http://www.example.net/foobar'
assert get_variadic_url('[url]/foobar/',
{'url': url}) == 'http://www.example.net/foobar/'
assert get_variadic_url('[url]foo/bar/',
{'url': 'http://www.example.net/'}) == 'http://www.example.net/foo/bar/'
assert get_variadic_url('[url]foobar/',
{'url': 'http://www.example.net/'}) == 'http://www.example.net/foobar/'
assert get_variadic_url('[url]foo/bar',
{'url': 'http://www.example.net/'}) == 'http://www.example.net/foo/bar'
def test_url_server_qs():
assert get_variadic_url('[url]?foo=bar',

View File

@ -316,15 +316,16 @@ def get_variadic_url(url, variables, encode_query=True):
# (ex: http[https]://www.example.net) or because the value starts
# with a variable name (ex: [url]); in that situation we do not
# quote at all.
if path.count('/') >= 2:
if path.count('//') == 1:
# there were no / in the original path (the two / comes from
# the scheme/netloc separation, this means there is no path
# the scheme/netloc separation, this means there is no path)
before_path = ezt_substitute(path, variables)
p2 = urlparse.urlsplit(before_path)
scheme, netloc, path = p2.scheme, p2.netloc, p2.path
else:
# there is a path, we need to get back to the original URL and
# split it on the last /, to isolate the path part.
lastslash = '/' if path.endswith('/') else ''
if '/' in path:
before_path, path = path.rsplit('/', 1)
else:
@ -334,7 +335,7 @@ def get_variadic_url(url, variables, encode_query=True):
scheme, netloc = p2.scheme, p2.netloc
if p2.path:
if not path:
path, query2 = p2.path, p2.query
path, query2 = p2.path + lastslash, p2.query
else:
path, query2 = p2.path + '/' + path, p2.query
if query and query2: