1
1
import unittest
2
2
3
- from curio import run
3
+ from curio import Kernel
4
4
5
5
from hyper2web .http import Stream
6
6
from hyper2web .router import Router
7
7
8
8
9
+ k = Kernel ()
10
+
9
11
class TestRouter (unittest .TestCase ):
10
12
11
- def test_handle_existing_route_only (self ):
13
+ def test_raise_error_on_non_existing_route (self ):
12
14
"""If a route doesn't exist, should raise error"""
13
15
router = Router (None , None )
14
16
stream = Stream (1 , {':path' : 'x' , ':method' : 'GET' })
15
17
16
18
# should raise a more specific error in the future
17
19
with self .assertRaises (Exception ):
18
20
coroutine = router .handle_route (None , stream )
19
- run (coroutine )
21
+ k . run (coroutine )
20
22
21
- # todo: this test has some problem
22
23
def test_get_existing_route (self ):
23
24
router = Router (None , None )
24
25
stream = Stream (1 , {':path' : 'x' , ':method' : 'GET' })
@@ -28,7 +29,7 @@ async def f(http, stream):
28
29
assert stream .headers [':path' ] == 'x'
29
30
router .get ('x' , f )
30
31
coroutine = router .handle_route (None , stream )
31
- run (coroutine )
32
+ k . run (coroutine )
32
33
33
34
def test_post_existing_route (self ):
34
35
router = Router (None , None )
@@ -39,7 +40,7 @@ async def f(http, stream):
39
40
assert stream .headers [':path' ] == 'x'
40
41
router .post ('x' , f )
41
42
coroutine = router .handle_route (None , stream )
42
- run (coroutine )
43
+ k . run (coroutine )
43
44
44
45
def test_match (self ):
45
46
# match true
@@ -52,4 +53,14 @@ def test_match(self):
52
53
matched , parameters = Router ._match ('user/{userId}/name/{name}' , 'user/123/nam/John' )
53
54
self .assertFalse (matched )
54
55
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
+
55
66
# will want to test with unicode
0 commit comments