Skip to content

Commit eaae95c

Browse files
+ test_parameterized_route
1 parent f570f0a commit eaae95c

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

hyper2web/router.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,16 @@ def match(self, path: str):
3939
# todo: now the problem is how to implement it
4040
# todo: pattern matching should be independent from :method,
4141
# todo: but the current implementation doesn't support it. Should improve it later.
42-
# GET
43-
for route in self._routes['GET'].keys():
44-
matched, parameters = self._match(route, path)
45-
if matched:
46-
return route, parameters
47-
48-
# POST
49-
pass
42+
for routes_of_this_method in self._routes.values():
43+
for route in routes_of_this_method:
44+
matched, parameters = self._match(route, path)
45+
if matched:
46+
return route, parameters
5047

5148
@classmethod
5249
def _match(cls, route, path):
5350
# todo: it seems like that regular expression is not necessary
51+
# note: Could it be simpler? Could regex help?
5452
route = route.split('/')
5553
path = path.split('/')
5654
if len(route) != len(path):
@@ -69,8 +67,11 @@ def _match(cls, route, path):
6967
async def handle_route(self, http: HTTP, stream: Stream):
7068
print('app.App.handle_route')
7169

72-
route = stream.headers[':path'].lstrip('/')
70+
path = stream.headers[':path'].lstrip('/')
7371
method = stream.headers[':method']
7472

73+
route, parameters = self.match(path)
74+
7575
handler = self._routes[method].get(route, self.method_default[method])
76-
await handler(http, stream)
76+
if handler is not None:
77+
await handler(http, stream, parameters=parameters)

test/test_router.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import unittest
22

3-
from curio import run
3+
from curio import Kernel
44

55
from hyper2web.http import Stream
66
from hyper2web.router import Router
77

88

9+
k = Kernel()
10+
911
class TestRouter(unittest.TestCase):
1012

11-
def test_handle_existing_route_only(self):
13+
def test_raise_error_on_non_existing_route(self):
1214
"""If a route doesn't exist, should raise error"""
1315
router = Router(None, None)
1416
stream = Stream(1, {':path': 'x', ':method': 'GET'})
1517

1618
# should raise a more specific error in the future
1719
with self.assertRaises(Exception):
1820
coroutine = router.handle_route(None, stream)
19-
run(coroutine)
21+
k.run(coroutine)
2022

21-
# todo: this test has some problem
2223
def test_get_existing_route(self):
2324
router = Router(None, None)
2425
stream = Stream(1, {':path': 'x', ':method': 'GET'})
@@ -28,7 +29,7 @@ async def f(http, stream):
2829
assert stream.headers[':path'] == 'x'
2930
router.get('x', f)
3031
coroutine = router.handle_route(None, stream)
31-
run(coroutine)
32+
k.run(coroutine)
3233

3334
def test_post_existing_route(self):
3435
router = Router(None, None)
@@ -39,7 +40,7 @@ async def f(http, stream):
3940
assert stream.headers[':path'] == 'x'
4041
router.post('x', f)
4142
coroutine = router.handle_route(None, stream)
42-
run(coroutine)
43+
k.run(coroutine)
4344

4445
def test_match(self):
4546
# match true
@@ -52,4 +53,14 @@ def test_match(self):
5253
matched, parameters = Router._match('user/{userId}/name/{name}', 'user/123/nam/John')
5354
self.assertFalse(matched)
5455

56+
def test_parameterized_route(self):
57+
router = Router(None, None)
58+
async def f(http, stream, parameters):
59+
self.assertIsNone(http)
60+
self.assertEqual(parameters['userId'], '123')
61+
self.assertEqual(parameters['name'], 'John')
62+
router.get('user/{userId}/name/{name}', f)
63+
c = router.handle_route(None, Stream(1, {':path': 'user/123/name/John', ':method': 'GET'}))
64+
k.run(c)
65+
5566
# will want to test with unicode

0 commit comments

Comments
 (0)