sanic/tests/test_middleware.py

186 lines
4.6 KiB
Python
Raw Normal View History

import logging
from asyncio import CancelledError
from sanic.exceptions import NotFound
from sanic.request import Request
from sanic.response import HTTPResponse, text
# ------------------------------------------------------------ #
# GET
# ------------------------------------------------------------ #
2018-08-26 15:43:14 +01:00
def test_middleware_request(app):
results = []
@app.middleware
2018-10-22 21:25:38 +01:00
async def handler1(request):
results.append(request)
@app.route('/')
2018-10-22 21:25:38 +01:00
async def handler2(request):
return text('OK')
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/')
assert response.text == 'OK'
assert type(results[0]) is Request
2018-08-26 15:43:14 +01:00
def test_middleware_response(app):
results = []
@app.middleware('request')
2018-10-22 21:25:38 +01:00
async def process_request(request):
results.append(request)
@app.middleware('response')
async def process_response(request, response):
results.append(request)
results.append(response)
@app.route('/')
async def handler(request):
return text('OK')
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/')
assert response.text == 'OK'
assert type(results[0]) is Request
assert type(results[1]) is Request
assert isinstance(results[2], HTTPResponse)
2018-08-26 15:43:14 +01:00
def test_middleware_response_exception(app):
result = {'status_code': None}
@app.middleware('response')
2017-11-15 15:46:39 +00:00
async def process_response(request, response):
result['status_code'] = response.status
return response
@app.exception(NotFound)
async def error_handler(request, exception):
return text('OK', exception.status_code)
@app.route('/')
async def handler(request):
return text('FAIL')
request, response = app.test_client.get('/page_not_found')
assert response.text == 'OK'
assert result['status_code'] == 404
2018-10-22 21:25:38 +01:00
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')
caplog.set_level(logging.ERROR, logger="sanic.root")
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')
caplog.set_level(logging.ERROR, logger="sanic.root")
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'
)
2018-08-26 15:43:14 +01:00
def test_middleware_override_request(app):
@app.middleware
async def halt_request(request):
return text('OK')
@app.route('/')
async def handler(request):
return text('FAIL')
2017-02-14 19:51:20 +00:00
response = app.test_client.get('/', gather_request=False)
assert response.status == 200
assert response.text == 'OK'
2018-08-26 15:43:14 +01:00
def test_middleware_override_response(app):
@app.middleware('response')
async def process_response(request, response):
return text('OK')
@app.route('/')
async def handler(request):
return text('FAIL')
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/')
assert response.status == 200
assert response.text == 'OK'
2018-08-26 15:43:14 +01:00
def test_middleware_order(app):
order = []
@app.middleware('request')
async def request1(request):
order.append(1)
@app.middleware('request')
async def request2(request):
order.append(2)
@app.middleware('request')
async def request3(request):
order.append(3)
@app.middleware('response')
async def response1(request, response):
order.append(6)
@app.middleware('response')
async def response2(request, response):
order.append(5)
@app.middleware('response')
async def response3(request, response):
order.append(4)
@app.route('/')
async def handler(request):
return text('OK')
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/')
assert response.status == 200
2018-10-22 21:25:38 +01:00
assert order == [1, 2, 3, 4, 5, 6]