Permalink
Browse files

Trailing slash is optional for urls. #71

  • Loading branch information...
1 parent ac6b800 commit 81913bf168a6775e0238968eea0e9ddd5dd1eb4e @jbeluch committed Feb 13, 2013
Showing with 21 additions and 1 deletion.
  1. +16 −0 tests/test_urls.py
  2. +5 −1 xbmcswift2/urls.py
View
16 tests/test_urls.py
@@ -29,3 +29,19 @@ def view(video_id):
# allow ints
path_qs = rule.make_path_qs({'video_id': 24})
self.assertEqual(path_qs, '/videos?video_id=24')
+
+class TestUrlRule(unittest.TestCase):
+
+ def test_match_without_trailing_slash(self):
+ def view():
+ pass
+ rule = UrlRule('/videos/', view, view.__name__, {})
+ self.assertEqual((view, {}), rule.match('/videos'))
+
+ def test_match_with_trailing_slash(self):
+ def view():
+ pass
+ rule = UrlRule('/videos', view, view.__name__, {})
+ self.assertEqual((view, {}), rule.match('/videos/'))
+
+
View
6 xbmcswift2/urls.py
@@ -50,7 +50,11 @@ def __init__(self, url_rule, view_func, name, options):
self._url_format = self._url_rule.replace('<', '{').replace('>', '}')
# Make a regex pattern for matching incoming URLs
- p = self._url_rule.replace('<', '(?P<').replace('>', '>[^/]+?)')
+ rule = self._url_rule
+ if rule != '/':
+ # Except for a path of '/', the trailing slash is optional.
+ rule = self._url_rule.rstrip('/') + '/?'
+ p = rule.replace('<', '(?P<').replace('>', '>[^/]+?)')
try:
self._regex = re.compile('^' + p + '$')

0 comments on commit 81913bf

Please sign in to comment.