Merge pull request #105 from channelcat/blueprint-ordering

Added blueprint order test and used deques to add blueprints
This commit is contained in:
Channel Cat 2016-10-24 02:45:31 -07:00 committed by GitHub
commit 984c086296
2 changed files with 44 additions and 3 deletions

View File

@ -1,4 +1,5 @@
from asyncio import get_event_loop
from collections import deque
from functools import partial
from inspect import isawaitable
from multiprocessing import Process, Event
@ -21,8 +22,8 @@ class Sanic:
self.router = router or Router()
self.error_handler = error_handler or Handler(self)
self.config = Config()
self.request_middleware = []
self.response_middleware = []
self.request_middleware = deque()
self.response_middleware = deque()
self.blueprints = {}
self._blueprint_order = []
self.loop = None
@ -74,7 +75,7 @@ class Sanic:
if attach_to == 'request':
self.request_middleware.append(middleware)
if attach_to == 'response':
self.response_middleware.insert(0, middleware)
self.response_middleware.appendleft(middleware)
return middleware
# Detect which way this was called, @middleware or @middleware('AT')

View File

@ -86,3 +86,43 @@ def test_middleware_override_response():
assert response.status == 200
assert response.text == 'OK'
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]