Support negative numbers for int path components (Fixes #137)

This commit is contained in:
Miguel Grinberg
2023-05-21 23:21:47 +01:00
parent a80841f464
commit a0dd7c8ab6
2 changed files with 5 additions and 3 deletions

View File

@@ -736,7 +736,7 @@ class URLPattern():
if type_ == 'string':
pattern = '[^/]+'
elif type_ == 'int':
pattern = '\\d+'
pattern = '-?\\d+'
elif type_ == 'path':
pattern = '.+'
elif type_.startswith('re:'):

View File

@@ -56,10 +56,12 @@ class TestURLPattern(unittest.TestCase):
p = URLPattern('/users/<int:id>/<int:id2>/')
self.assertEqual(p.match('/users/123/456/'), {'id': 123, 'id2': 456})
self.assertEqual(p.match('/users/123/-456/'), {'id': 123, 'id2': -456})
self.assertIsNone(p.match('/users/'))
self.assertIsNone(p.match('/users/123/456'))
self.assertIsNone(p.match('/users/123/-456'))
self.assertIsNone(p.match('/users/123/abc/'))
self.assertIsNone(p.match('/users/123/456/abc'))
self.assertIsNone(p.match('/users/123/-456/abc'))
self.assertIsNone(p.match('/users/--123/456/'))
def test_path_argument(self):
p = URLPattern('/users/<path:path>')