diff options
author | Ben Bangert <ben@groovie.org> | 2015-01-17 13:23:39 -0800 |
---|---|---|
committer | Ben Bangert <ben@groovie.org> | 2015-01-17 13:23:39 -0800 |
commit | c896e82611f9f4ae7962de7dfa4734d7c4342be6 (patch) | |
tree | 18b575db69a9d6c72ea0ecedbf129b9d50a764ef | |
parent | da4f235ce6e48046bf95bef03cffe23fcdf907a1 (diff) | |
download | routes-c896e82611f9f4ae7962de7dfa4734d7c4342be6.tar.gz |
* URL generation with/without SCRIPT_NAME was resulting in the URL cache
failing to return the appropriate cached URL generation. The URL cache
should always include the SCRIPT_NAME, even if its empty, in the cache
to avoid this, and now does. Fixes #6.
-rw-r--r-- | CHANGELOG.rst | 4 | ||||
-rw-r--r-- | routes/mapper.py | 26 | ||||
-rw-r--r-- | tests/test_functional/test_generation.py | 7 |
3 files changed, 24 insertions, 13 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 5606bce..e883078 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,10 @@ Routes Changelog Release 2.1 (**dev**) ===================== +* URL generation with/without SCRIPT_NAME was resulting in the URL cache + failing to return the appropriate cached URL generation. The URL cache + should always include the SCRIPT_NAME, even if its empty, in the cache + to avoid this, and now does. Fixes #6. * Extract Route creation into separate method in Mapper. Subclasses of Route can be created by Mappers now. * Use the first X_FORWARDED_FOR value if there are multiple proxies in the diff --git a/routes/mapper.py b/routes/mapper.py index fcb272e..8979253 100644 --- a/routes/mapper.py +++ b/routes/mapper.py @@ -744,7 +744,13 @@ class Mapper(SubMapperParent): if 'action' not in kargs: kargs['action'] = 'index' - environ = kargs.pop('_environ', self.environ) + environ = kargs.pop('_environ', self.environ) or {} + if 'SCRIPT_NAME' in environ: + script_name = environ['SCRIPT_NAME'] + elif self.environ and 'SCRIPT_NAME' in self.environ: + script_name = self.environ['SCRIPT_NAME'] + else: + script_name = "" controller = kargs.get('controller', None) action = kargs.get('action', None) @@ -755,17 +761,12 @@ class Mapper(SubMapperParent): unicode(kargs).encode('utf8') if self.urlcache is not None: - if self.environ: - cache_key_script_name = '%s:%s' % ( - environ.get('SCRIPT_NAME', ''), cache_key) - else: - cache_key_script_name = cache_key + cache_key_script_name = '%s:%s' % (script_name, cache_key) # Check the url cache to see if it exists, use it if it does - for key in [cache_key, cache_key_script_name]: - val = self.urlcache.get(key, self) - if val != self: - return val + val = self.urlcache.get(cache_key_script_name, self) + if val != self: + return val controller = as_unicode(controller, self.encoding) action = as_unicode(action, self.encoding) @@ -869,9 +870,8 @@ class Mapper(SubMapperParent): if self.prefix: path = self.prefix + path external_static = route.static and route.external - if environ and environ.get('SCRIPT_NAME', '') != ''\ - and not route.absolute and not external_static: - path = environ['SCRIPT_NAME'] + path + if not route.absolute and not external_static: + path = script_name + path key = cache_key_script_name else: key = cache_key diff --git a/tests/test_functional/test_generation.py b/tests/test_functional/test_generation.py index 1f66188..5e6d51e 100644 --- a/tests/test_functional/test_generation.py +++ b/tests/test_functional/test_generation.py @@ -584,6 +584,13 @@ class TestGeneration(unittest.TestCase): eq_('/notblog/content', m.generate(controller='content')) eq_('/notblog/admin/comments', m.generate(controller='admin/comments')) + def test_url_with_environ_and_caching(self): + m = Mapper() + m.connect("foo", "/", controller="main", action="index") + + eq_('/', m.generate(controller='main', action='index')) + eq_('/bar/', m.generate(controller='main', action='index', _environ=dict(SCRIPT_NAME='/bar'))) + eq_('/', m.generate(controller='main', action='index')) def test_url_with_environ_and_absolute(self): m = Mapper(explicit=False) |