From d0a4cf8fa7dfb1da7466157b18d3329a8cf9a5df Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Wed, 6 Mar 2024 20:34:00 +0000 Subject: [PATCH] Handle 0 as an integer argument (Fixes #212) --- src/microdot/microdot.py | 4 ++-- tests/test_url_pattern.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index e2f2e41..4715594 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -862,8 +862,6 @@ class URLPattern(): if arg is None: return if 'name' in segment: - if not arg: - return args[segment['name']] = arg if path is not None: return @@ -879,6 +877,8 @@ class URLPattern(): def _string_segment(self, value): s = value.split('/', 1) + if len(s[0]) == 0: + return None, None return s[0], s[1] if len(s) > 1 else None def _int_segment(self, value): diff --git a/tests/test_url_pattern.py b/tests/test_url_pattern.py index 0cdde9e..e9b4a43 100644 --- a/tests/test_url_pattern.py +++ b/tests/test_url_pattern.py @@ -26,10 +26,14 @@ class TestURLPattern(unittest.TestCase): p = URLPattern('/') self.assertEqual(p.match('/foo'), {'arg': 'foo'}) self.assertIsNone(p.match('/')) + self.assertIsNone(p.match('//')) self.assertIsNone(p.match('')) self.assertIsNone(p.match('foo/')) self.assertIsNone(p.match('/foo/')) + self.assertIsNone(p.match('//foo/')) + self.assertIsNone(p.match('/foo//')) self.assertIsNone(p.match('/foo/bar')) + self.assertIsNone(p.match('/foo//bar')) p = URLPattern('//') self.assertEqual(p.match('/foo/'), {'arg': 'foo'}) @@ -64,6 +68,8 @@ class TestURLPattern(unittest.TestCase): def test_int_argument(self): p = URLPattern('/users/') self.assertEqual(p.match('/users/123'), {'id': 123}) + self.assertEqual(p.match('/users/-123'), {'id': -123}) + self.assertEqual(p.match('/users/0'), {'id': 0}) self.assertIsNone(p.match('/users/')) self.assertIsNone(p.match('/users/abc')) self.assertIsNone(p.match('/users/123abc'))