Add tests (#1433)
* Add tests for remove_route() * Add tests for sanic/router.py * Add tests for sanic/cookies.py * Disable reset logging in test_logging.py * Add tests for sanic/request.py * Add tests for ContentRangeHandler * Add tests for exception at response middleware * Fix cached_handlers for ErrorHandler.lookup() * Add test for websocket request timeout * Add tests for getting cookies of StreamResponse, Remove some unused variables in tests/test_cookies.py * Add tests for nested error handle
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
from sanic.request import Request
|
||||
from sanic.response import text, HTTPResponse
|
||||
from sanic.exceptions import NotFound
|
||||
import logging
|
||||
from asyncio import CancelledError
|
||||
|
||||
from sanic.exceptions import NotFound
|
||||
from sanic.request import Request
|
||||
from sanic.response import HTTPResponse, text
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# GET
|
||||
@@ -69,6 +71,49 @@ def test_middleware_response_exception(app):
|
||||
assert result['status_code'] == 404
|
||||
|
||||
|
||||
def test_middleware_response_raise_cancelled_error(app, caplog):
|
||||
|
||||
@app.middleware('response')
|
||||
async def process_response(request, response):
|
||||
raise CancelledError('CancelledError at response middleware')
|
||||
|
||||
@app.get('/')
|
||||
def handler(request):
|
||||
return text('OK')
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
reqrequest, response = app.test_client.get('/')
|
||||
|
||||
assert response.status == 503
|
||||
assert caplog.record_tuples[0] == (
|
||||
'sanic.root',
|
||||
logging.ERROR,
|
||||
'Exception occurred while handling uri: \'http://127.0.0.1:42101/\''
|
||||
)
|
||||
|
||||
|
||||
def test_middleware_response_raise_exception(app, caplog):
|
||||
|
||||
@app.middleware('response')
|
||||
async def process_response(request, response):
|
||||
raise Exception('Exception at response middleware')
|
||||
|
||||
with caplog.at_level(logging.ERROR):
|
||||
reqrequest, response = app.test_client.get('/')
|
||||
|
||||
assert response.status == 404
|
||||
assert caplog.record_tuples[0] == (
|
||||
'sanic.root',
|
||||
logging.ERROR,
|
||||
'Exception occurred while handling uri: \'http://127.0.0.1:42101/\''
|
||||
)
|
||||
assert caplog.record_tuples[1] == (
|
||||
'sanic.error',
|
||||
logging.ERROR,
|
||||
'Exception occurred in one of response middleware handlers'
|
||||
)
|
||||
|
||||
|
||||
def test_middleware_override_request(app):
|
||||
|
||||
@app.middleware
|
||||
|
||||
Reference in New Issue
Block a user