Split up the exceptions tests
This commit is contained in:
parent
4684083f2f
commit
c9d63f4e31
|
@ -1,72 +1,53 @@
|
||||||
from json import loads as json_loads, dumps as json_dumps
|
|
||||||
from sanic import Sanic
|
from sanic import Sanic
|
||||||
from sanic.response import json, text
|
from sanic.response import text
|
||||||
from sanic.exceptions import InvalidUsage, ServerError, NotFound
|
from sanic.exceptions import InvalidUsage, ServerError, NotFound
|
||||||
from helpers import sanic_endpoint_test
|
from sanic.utils import sanic_endpoint_test
|
||||||
|
|
||||||
# ------------------------------------------------------------ #
|
# ------------------------------------------------------------ #
|
||||||
# GET
|
# GET
|
||||||
# ------------------------------------------------------------ #
|
# ------------------------------------------------------------ #
|
||||||
|
|
||||||
def test_exception_response():
|
exception_app = Sanic('test_exceptions')
|
||||||
app = Sanic('test_text')
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
def handler(request):
|
|
||||||
return text('OK')
|
|
||||||
|
|
||||||
@app.route('/error')
|
@exception_app.route('/')
|
||||||
def handler_error(request):
|
def handler(request):
|
||||||
raise ServerError("OK")
|
return text('OK')
|
||||||
|
|
||||||
@app.route('/404')
|
|
||||||
def handler_404(request):
|
|
||||||
raise NotFound("OK")
|
|
||||||
|
|
||||||
@app.route('/invalid')
|
@exception_app.route('/error')
|
||||||
def handler_invalid(request):
|
def handler_error(request):
|
||||||
raise InvalidUsage("OK")
|
raise ServerError("OK")
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app)
|
|
||||||
assert response.status == 200
|
|
||||||
assert response.text == 'OK'
|
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/error')
|
@exception_app.route('/404')
|
||||||
assert response.status == 500
|
def handler_404(request):
|
||||||
|
raise NotFound("OK")
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/invalid')
|
|
||||||
assert response.status == 400
|
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/404')
|
@exception_app.route('/invalid')
|
||||||
assert response.status == 404
|
def handler_invalid(request):
|
||||||
|
raise InvalidUsage("OK")
|
||||||
|
|
||||||
def test_exception_handler():
|
|
||||||
app = Sanic('test_text')
|
|
||||||
|
|
||||||
@app.route('/1')
|
def test_no_exception():
|
||||||
def handler_1(request):
|
request, response = sanic_endpoint_test(exception_app)
|
||||||
raise InvalidUsage("OK")
|
assert response.status == 200
|
||||||
@app.route('/2')
|
assert response.text == 'OK'
|
||||||
def handler_2(request):
|
|
||||||
raise ServerError("OK")
|
|
||||||
@app.route('/3')
|
|
||||||
def handler_3(request):
|
|
||||||
raise NotFound("OK")
|
|
||||||
|
|
||||||
@app.exception(NotFound, ServerError)
|
|
||||||
def handler_exception(request, exception):
|
|
||||||
return text("OK")
|
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/1')
|
def test_server_error_exception():
|
||||||
assert response.status == 400
|
request, response = sanic_endpoint_test(exception_app, uri='/error')
|
||||||
|
assert response.status == 500
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/2')
|
|
||||||
assert response.status == 200
|
|
||||||
assert response.text == 'OK'
|
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/3')
|
def test_invalid_usage_exception():
|
||||||
assert response.status == 200
|
request, response = sanic_endpoint_test(exception_app, uri='/invalid')
|
||||||
|
assert response.status == 400
|
||||||
|
|
||||||
|
|
||||||
|
def test_not_found_exception():
|
||||||
|
request, response = sanic_endpoint_test(exception_app, uri='/404')
|
||||||
|
assert response.status == 404
|
||||||
|
|
||||||
|
|
||||||
request, response = sanic_endpoint_test(app, uri='/random')
|
|
||||||
assert response.status == 200
|
|
||||||
assert response.text == 'OK'
|
|
49
tests/test_exceptions_handler.py
Normal file
49
tests/test_exceptions_handler.py
Normal file
|
@ -0,0 +1,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'
|
Loading…
Reference in New Issue
Block a user