2018-12-22 15:21:45 +00:00
|
|
|
import logging
|
|
|
|
from asyncio import CancelledError
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-22 15:21:45 +00:00
|
|
|
from sanic.exceptions import NotFound
|
|
|
|
from sanic.request import Request
|
|
|
|
from sanic.response import HTTPResponse, text
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_middleware_request(app):
|
2016-10-15 03:53:49 +01:00
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.middleware
|
2018-10-22 21:25:38 +01:00
|
|
|
async def handler1(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
results.append(request)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2018-10-22 21:25:38 +01:00
|
|
|
async def handler2(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2016-10-15 03:53:49 +01:00
|
|
|
assert type(results[0]) is Request
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_middleware_response(app):
|
2016-10-15 03:53:49 +01:00
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("request")
|
2018-10-22 21:25:38 +01:00
|
|
|
async def process_request(request):
|
2016-10-15 03:53:49 +01:00
|
|
|
results.append(request)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2016-10-15 03:53:49 +01:00
|
|
|
async def process_response(request, response):
|
|
|
|
results.append(request)
|
|
|
|
results.append(response)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-10-15 03:53:49 +01:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2016-10-15 03:53:49 +01:00
|
|
|
assert type(results[0]) is Request
|
|
|
|
assert type(results[1]) is Request
|
2017-01-27 10:11:29 +00:00
|
|
|
assert isinstance(results[2], HTTPResponse)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_middleware_response_exception(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
result = {"status_code": None}
|
2017-04-04 15:55:38 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2017-11-15 15:46:39 +00:00
|
|
|
async def process_response(request, response):
|
2018-12-30 11:18:06 +00:00
|
|
|
result["status_code"] = response.status
|
2017-04-04 15:55:38 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
@app.exception(NotFound)
|
|
|
|
async def error_handler(request, exception):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK", exception.status_code)
|
2017-04-04 15:55:38 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2017-04-04 15:55:38 +01:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("FAIL")
|
2017-04-04 15:55:38 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/page_not_found")
|
|
|
|
assert response.text == "OK"
|
|
|
|
assert result["status_code"] == 404
|
2017-04-04 15:55:38 +01:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2018-12-22 15:21:45 +00:00
|
|
|
def test_middleware_response_raise_cancelled_error(app, caplog):
|
2018-12-30 18:37:30 +00:00
|
|
|
app.config.RESPONSE_TIMEOUT = 1
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2018-12-22 15:21:45 +00:00
|
|
|
async def process_response(request, response):
|
2018-12-30 11:18:06 +00:00
|
|
|
raise CancelledError("CancelledError at response middleware")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2018-12-30 11:18:06 +00:00
|
|
|
reqrequest, response = app.test_client.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 503
|
|
|
|
assert caplog.record_tuples[0] == (
|
2018-12-30 11:18:06 +00:00
|
|
|
"sanic.root",
|
2018-12-22 15:21:45 +00:00
|
|
|
logging.ERROR,
|
2018-12-30 11:18:06 +00:00
|
|
|
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
2018-12-22 15:21:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_middleware_response_raise_exception(app, caplog):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2018-12-22 15:21:45 +00:00
|
|
|
async def process_response(request, response):
|
2018-12-30 11:18:06 +00:00
|
|
|
raise Exception("Exception at response middleware")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
with caplog.at_level(logging.ERROR):
|
2018-12-30 11:18:06 +00:00
|
|
|
reqrequest, response = app.test_client.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
assert response.status == 404
|
|
|
|
assert caplog.record_tuples[0] == (
|
2018-12-30 11:18:06 +00:00
|
|
|
"sanic.root",
|
2018-12-22 15:21:45 +00:00
|
|
|
logging.ERROR,
|
2018-12-30 11:18:06 +00:00
|
|
|
"Exception occurred while handling uri: 'http://127.0.0.1:42101/'",
|
2018-12-22 15:21:45 +00:00
|
|
|
)
|
|
|
|
assert caplog.record_tuples[1] == (
|
2018-12-30 11:18:06 +00:00
|
|
|
"sanic.error",
|
2018-12-22 15:21:45 +00:00
|
|
|
logging.ERROR,
|
2018-12-30 11:18:06 +00:00
|
|
|
"Exception occurred in one of response middleware handlers",
|
2018-12-22 15:21:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_middleware_override_request(app):
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.middleware
|
|
|
|
async def halt_request(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-10-15 03:53:49 +01:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("FAIL")
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
response = app.test_client.get("/", gather_request=False)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_middleware_override_response(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2016-10-15 03:53:49 +01:00
|
|
|
async def process_response(request, response):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-10-15 03:53:49 +01:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("FAIL")
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "OK"
|
2016-10-24 10:09:07 +01:00
|
|
|
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_middleware_order(app):
|
2016-10-24 10:09:07 +01:00
|
|
|
order = []
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("request")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def request1(request):
|
|
|
|
order.append(1)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("request")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def request2(request):
|
|
|
|
order.append(2)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("request")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def request3(request):
|
|
|
|
order.append(3)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def response1(request, response):
|
|
|
|
order.append(6)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def response2(request, response):
|
|
|
|
order.append(5)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.middleware("response")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def response3(request, response):
|
|
|
|
order.append(4)
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-10-24 10:09:07 +01:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return text("OK")
|
2016-10-24 10:09:07 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-24 10:09:07 +01:00
|
|
|
|
|
|
|
assert response.status == 200
|
2018-10-22 21:25:38 +01:00
|
|
|
assert order == [1, 2, 3, 4, 5, 6]
|