1
|
|
|
from mount_api.core import exceptions |
2
|
|
|
from mount_api.endpoints import AbstractEndpoint |
3
|
|
|
from mount_api.routing import AbstractRouter, Route, Router |
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestEndpoint(AbstractEndpoint): |
9
|
|
|
def get(self): |
10
|
|
|
return {'message': 'Test response.'} |
11
|
|
|
|
12
|
|
|
def post(self, name: str): |
13
|
|
|
return {'message': 'Hello {}.'.format(name)} |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
routes = [ |
17
|
|
|
Route('/welcome', TestEndpoint()), |
18
|
|
|
] |
19
|
|
|
|
20
|
|
|
router = Router(routes=routes) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def test_router_valid_get_dispatch_for_valid_path(): |
24
|
|
|
assert router.dispatch( |
25
|
|
|
path=routes[0].rule, method='GET' |
26
|
|
|
) == routes[0].endpoint.get |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
def test_router_valid_post_dispatch_for_valid_path(): |
30
|
|
|
assert router.dispatch( |
31
|
|
|
path=routes[0].rule, method='POST' |
32
|
|
|
) == routes[0].endpoint.post |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_router_raises_not_found_for_invalid_path(): |
36
|
|
|
with pytest.raises(exceptions.NotFound): |
37
|
|
|
router.dispatch(path='/foo', method='GET') |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
def test_router_raises_not_found_for_invalid_method(): |
41
|
|
|
with pytest.raises(exceptions.MethodNotAllowed): |
42
|
|
|
router.dispatch(path=routes[0].rule, method='DELETE') |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_abstract_router_dispatch_raises_not_implemented(): |
46
|
|
|
with pytest.raises(NotImplementedError): |
47
|
|
|
AbstractRouter.dispatch(router, path=routes[0].rule, method='GET') |
48
|
|
|
|