2016-10-14 11:23:48 +01:00
|
|
|
from json import loads as json_loads, dumps as json_dumps
|
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.request import Request
|
|
|
|
from sanic.response import json, text, HTTPResponse
|
2017-04-04 15:55:38 +01:00
|
|
|
from sanic.exceptions import NotFound
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
def test_middleware_request():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_middleware_request')
|
|
|
|
|
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.middleware
|
|
|
|
async def handler(request):
|
|
|
|
results.append(request)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is Request
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_middleware_response():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_middleware_response')
|
|
|
|
|
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.middleware('request')
|
|
|
|
async def process_response(request):
|
|
|
|
results.append(request)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.middleware('response')
|
|
|
|
async def process_response(request, response):
|
|
|
|
results.append(request)
|
|
|
|
results.append(response)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-04-04 15:55:38 +01:00
|
|
|
def test_middleware_response_exception():
|
|
|
|
app = Sanic('test_middleware_response_exception')
|
|
|
|
result = {'status_code': None}
|
|
|
|
|
|
|
|
@app.middleware('response')
|
2017-11-15 15:46:39 +00:00
|
|
|
async def process_response(request, response):
|
2017-04-04 15:55:38 +01:00
|
|
|
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
|
|
|
|
|
2017-12-26 15:47:13 +00:00
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
def test_middleware_override_request():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_middleware_override_request')
|
|
|
|
|
|
|
|
@app.middleware
|
|
|
|
async def halt_request(request):
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('FAIL')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +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
|
|
|
|
assert response.text == 'OK'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_middleware_override_response():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_middleware_override_response')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.middleware('response')
|
|
|
|
async def process_response(request, response):
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('FAIL')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +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
|
|
|
|
assert response.text == 'OK'
|
2016-10-24 10:09:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_middleware_order():
|
|
|
|
app = Sanic('test_middleware_order')
|
|
|
|
|
|
|
|
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('/')
|
2016-10-24 10:09:07 +01:00
|
|
|
|
|
|
|
assert response.status == 200
|
2017-12-26 15:47:13 +00:00
|
|
|
assert order == [1, 2, 3, 4, 5, 6]
|