From a1e9ca7097f80b546ccf2e788841fcd33794605d Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 23 Feb 2016 01:17:31 +0200 Subject: Add tests for protocol-relative URLs generation. Close #29 --- tests/test_functional/test_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_functional/test_utils.py b/tests/test_functional/test_utils.py index 3f4dd47..e2a6a04 100644 --- a/tests/test_functional/test_utils.py +++ b/tests/test_functional/test_utils.py @@ -62,6 +62,8 @@ class TestUtils(unittest.TestCase): eq_('/content', urlobj()) eq_('https://www.test.com/viewpost', urlobj(controller='post', action='view', protocol='https')) eq_('http://www.test.org/content', urlobj(host='www.test.org')) + eq_('//www.test.com/viewpost', urlobj(controller='post', action='view', protocol='')) + eq_('//www.test.org/content', urlobj(host='www.test.org', protocol='')) def test_url_raises(self): con = self.con -- cgit v1.2.1 From d3021164cdbf996267dd049c25b2144cedb60d7a Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 23 Feb 2016 01:18:03 +0200 Subject: Add support for protocol-relative URLs generation. Close #29 --- routes/util.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/routes/util.py b/routes/util.py index baeeac7..24eb3e1 100644 --- a/routes/util.py +++ b/routes/util.py @@ -180,15 +180,19 @@ def url_for(*args, **kargs): """ anchor = kargs.get('anchor') host = kargs.get('host') - protocol = kargs.get('protocol') + protocol = kargs.pop('protocol', None) qualified = kargs.pop('qualified', None) # Remove special words from kargs, convert placeholders - for key in ['anchor', 'host', 'protocol']: + for key in ['anchor', 'host']: if kargs.get(key): del kargs[key] if key+'_' in kargs: kargs[key] = kargs.pop(key+'_') + + if 'protocol_' in kargs: + kargs['protocol_'] = protocol + config = request_config() route = None static = False @@ -250,21 +254,23 @@ def url_for(*args, **kargs): newargs = _screenargs(kargs, config.mapper, environ) anchor = newargs.pop('_anchor', None) or anchor host = newargs.pop('_host', None) or host - protocol = newargs.pop('_protocol', None) or protocol + protocol = newargs.pop('_protocol', protocol) url = config.mapper.generate(*route_args, **newargs) if anchor is not None: url += '#' + _url_quote(anchor, encoding) - if host or protocol or qualified: + if host or protocol is not None or qualified: if not host and not qualified: # Ensure we don't use a specific port, as changing the protocol # means that we most likely need a new port host = config.host.split(':')[0] elif not host: host = config.host - if not protocol: + if protocol is None: protocol = config.protocol + if protocol != '': + protocol += ':' if url is not None: - url = protocol + '://' + host + url + url = protocol + '//' + host + url if not ascii_characters(url) and url is not None: raise GenerationException("url_for can only return a string, got " @@ -324,16 +330,19 @@ class URLGenerator(object): """ anchor = kargs.get('anchor') host = kargs.get('host') - protocol = kargs.get('protocol') + protocol = kargs.pop('protocol', None) qualified = kargs.pop('qualified', None) # Remove special words from kargs, convert placeholders - for key in ['anchor', 'host', 'protocol']: + for key in ['anchor', 'host']: if kargs.get(key): del kargs[key] if key+'_' in kargs: kargs[key] = kargs.pop(key+'_') + if 'protocol_' in kargs: + kargs['protocol_'] = protocol + route = None use_current = '_use_current' in kargs and kargs.pop('_use_current') @@ -396,12 +405,13 @@ class URLGenerator(object): anchor = anchor or newargs.pop('_anchor', None) host = host or newargs.pop('_host', None) - protocol = protocol or newargs.pop('_protocol', None) + if protocol is None: + protocol = newargs.pop('_protocol', None) newargs['_environ'] = self.environ url = self.mapper.generate(*route_args, **newargs) if anchor is not None: url += '#' + _url_quote(anchor, encoding) - if host or protocol or qualified: + if host or protocol is not None or qualified: if 'routes.cached_hostinfo' not in self.environ: cache_hostinfo(self.environ) hostinfo = self.environ['routes.cached_hostinfo'] @@ -412,12 +422,14 @@ class URLGenerator(object): host = hostinfo['host'].split(':')[0] elif not host: host = hostinfo['host'] - if not protocol: + if protocol is None: protocol = hostinfo['protocol'] + if protocol != '': + protocol += ':' if url is not None: if host[-1] != '/': host += '/' - url = protocol + '://' + host + url.lstrip('/') + url = protocol + '//' + host + url.lstrip('/') if not ascii_characters(url) and url is not None: raise GenerationException("Can only return a string, got " -- cgit v1.2.1 From 4557f52729e4e7f5b03c876bcb476de3b8cdc706 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Tue, 23 Feb 2016 01:21:20 +0200 Subject: Document support for protocol-relative URLs generation. Close #29 --- CHANGELOG.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2a8a322..3b08d25 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,8 @@ Routes Changelog Release 2.3 (**dev**) ===================== +* Add support for protocol-relative URLs generation (i.e. starting with double + slash ``//``). PR #60. Patch by Sviatoslav Sydorenko. * Add support for the ``middleware`` extra requirement, making possible to depend on ``webob`` optionally. PR #59. Patch by Sviatoslav Sydorenko. * Fix matching of an empty string route, which led to exception in earlier -- cgit v1.2.1 From f4b2cb0297f4f07d27f142e9112bd5fbf6a0a2c6 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 28 Feb 2016 10:56:30 +0200 Subject: Wrap conditions with parentheses --- routes/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/util.py b/routes/util.py index 24eb3e1..f3d2dc4 100644 --- a/routes/util.py +++ b/routes/util.py @@ -258,7 +258,7 @@ def url_for(*args, **kargs): url = config.mapper.generate(*route_args, **newargs) if anchor is not None: url += '#' + _url_quote(anchor, encoding) - if host or protocol is not None or qualified: + if host or (protocol is not None) or qualified: if not host and not qualified: # Ensure we don't use a specific port, as changing the protocol # means that we most likely need a new port @@ -411,7 +411,7 @@ class URLGenerator(object): url = self.mapper.generate(*route_args, **newargs) if anchor is not None: url += '#' + _url_quote(anchor, encoding) - if host or protocol is not None or qualified: + if host or (protocol is not None) or qualified: if 'routes.cached_hostinfo' not in self.environ: cache_hostinfo(self.environ) hostinfo = self.environ['routes.cached_hostinfo'] -- cgit v1.2.1