Skip to content

Commit f570f0a

Browse files
+ test_match
1 parent 63046aa commit f570f0a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

hyper2web/router.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ def _match(cls, route, path):
5757
return False, None
5858
else:
5959
# todo: implement it
60-
pass
60+
parameters = {}
61+
for r, p in zip(route, path):
62+
if r[0] == '{' and r[-1] == '}':
63+
parameters[r[1:-1]] = p
64+
elif r != p:
65+
return False, None
66+
return True, parameters
6167

6268
# async
6369
async def handle_route(self, http: HTTP, stream: Stream):

test/test_router.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def test_handle_existing_route_only(self):
1818
coroutine = router.handle_route(None, stream)
1919
run(coroutine)
2020

21+
# todo: this test has some problem
2122
def test_get_existing_route(self):
2223
router = Router(None, None)
2324
stream = Stream(1, {':path': 'x', ':method': 'GET'})
@@ -40,12 +41,15 @@ async def f(http, stream):
4041
coroutine = router.handle_route(None, stream)
4142
run(coroutine)
4243

43-
def test_parameterized_route(self):
44-
router = Router(None, None)
45-
the_route = 'user/{user}'
46-
router._route('GET', the_route, None)
47-
matched, parameters = router.match('/user/abc') # todo: to be implemented
48-
self.assertEqual(the_route, matched)
49-
self.assertEqual(parameters['user'], 'abc')
44+
def test_match(self):
45+
# match true
46+
matched, parameters = Router._match('user/{userId}/name/{name}', 'user/123/name/John')
47+
self.assertTrue(matched)
48+
self.assertEqual(parameters['userId'], '123')
49+
self.assertEqual(parameters['name'], 'John')
50+
51+
# match false
52+
matched, parameters = Router._match('user/{userId}/name/{name}', 'user/123/nam/John')
53+
self.assertFalse(matched)
5054

5155
# will want to test with unicode

0 commit comments

Comments
 (0)