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
|
2016-10-14 23:38:43 +01:00
|
|
|
from sanic.utils import sanic_endpoint_test
|
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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app)
|
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is Request
|
|
|
|
assert type(results[1]) is Request
|
|
|
|
assert issubclass(type(results[2]), HTTPResponse)
|
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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
response = sanic_endpoint_test(app, 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
|
|
|
|
|
|
|
|
2017-01-11 08:23:12 +00:00
|
|
|
def test_middleware_override_scope():
|
|
|
|
app = Sanic('test_middleware_override_scope')
|
|
|
|
|
|
|
|
@app.middleware
|
|
|
|
async def validate_request(request):
|
|
|
|
if not request.headers.get('X-REQUIRED-HEADER'):
|
|
|
|
return text('X-REQUIRED-HEADER is missing.', status=412)
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
response = sanic_endpoint_test(app, uri='/', gather_request=False)
|
|
|
|
assert response.status == 412
|
|
|
|
|
|
|
|
response = sanic_endpoint_test(app, uri='/nothing/', gather_request=False)
|
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app)
|
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')
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app)
|
|
|
|
|
|
|
|
assert response.status == 200
|
|
|
|
assert order == [1,2,3,4,5,6]
|