From a8665d74dc7684929d823edadb1d5c6e04b18f80 Mon Sep 17 00:00:00 2001 From: bbangert Date: Fri, 17 Mar 2006 13:09:20 -0800 Subject: [svn] Added mapper support for append_slash which is used to determine if a slash will be appended during URL generation. --HG-- branch : trunk --- routes/base.py | 1 + routes/util.py | 2 ++ tests/test_utils.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/routes/base.py b/routes/base.py index 7d8834f..814a99e 100644 --- a/routes/base.py +++ b/routes/base.py @@ -444,6 +444,7 @@ class Mapper(object): self._regprefix = None self._routenames = {} self.debug = False + self.append_slash = False if register: config = request_config() config.mapper = self diff --git a/routes/util.py b/routes/util.py index 6f81ec2..2cb80be 100644 --- a/routes/util.py +++ b/routes/util.py @@ -115,6 +115,8 @@ def url_for(*args, **kargs): else: newargs = _screenargs(kargs) url = config.mapper.generate(**newargs) + if config.mapper.append_slash and not url.endswith('/'): + url += '/' if anchor: url += '#' + _url_quote(anchor) if host or protocol: if not host: host = config.host diff --git a/tests/test_utils.py b/tests/test_utils.py index c1fcbab..e514015 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -176,6 +176,22 @@ class TestUtils(unittest.TestCase): self.assertEqual('http://www.google.com/search?q=routes', url_for('http://www.google.com/search', q='routes')) self.assertEqual('/delicious.jpg', url_for('/delicious.jpg')) self.assertEqual('/delicious/search?v=routes', url_for('/delicious/search', v='routes')) + + def test_append_slash(self): + m = self.con.mapper + self.con.mapper_dict = {} + m.append_slash = True + self.con.environ = dict(SCRIPT_NAME='', SERVER_NAME='example.com') + m.connect(':controller/:action/:id') + m.connect('home', 'http://www.groovie.org/', _static=True) + m.connect('space', '/nasa/images', _static=True) + m.create_regs(['content', 'blog']) + + self.assertEqual('http://www.google.com/search', url_for('http://www.google.com/search')) + self.assertEqual('http://www.google.com/search?q=routes', url_for('http://www.google.com/search', q='routes')) + self.assertEqual('/delicious.jpg', url_for('/delicious.jpg')) + self.assertEqual('/delicious/search?v=routes', url_for('/delicious/search', v='routes')) + self.assertEqual('/content/list/', url_for(controller='/content', action='list')) def test_no_named_path_with_script(self): m = self.con.mapper -- cgit v1.2.1