forked from sanic-org/sanic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_exceptions_handler.py
49 lines (32 loc) · 1.27 KB
/
test_exceptions_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sanic import Sanic
from sanic.response import text
from sanic.exceptions import InvalidUsage, ServerError, NotFound
from sanic.utils import sanic_endpoint_test
exception_handler_app = Sanic('test_exception_handler')
@exception_handler_app.route('/1')
def handler_1(request):
raise InvalidUsage("OK")
@exception_handler_app.route('/2')
def handler_2(request):
raise ServerError("OK")
@exception_handler_app.route('/3')
def handler_3(request):
raise NotFound("OK")
@exception_handler_app.exception(NotFound, ServerError)
def handler_exception(request, exception):
return text("OK")
def test_invalid_usage_exception_handler():
request, response = sanic_endpoint_test(exception_handler_app, uri='/1')
assert response.status == 400
def test_server_error_exception_handler():
request, response = sanic_endpoint_test(exception_handler_app, uri='/2')
assert response.status == 200
assert response.text == 'OK'
def test_not_found_exception_handler():
request, response = sanic_endpoint_test(exception_handler_app, uri='/3')
assert response.status == 200
def test_text_exception__handler():
request, response = sanic_endpoint_test(
exception_handler_app, uri='/random')
assert response.status == 200
assert response.text == 'OK'