Merge pull request #105 from channelcat/blueprint-ordering
Added blueprint order test and used deques to add blueprints
This commit is contained in:
commit
984c086296
|
@ -1,4 +1,5 @@
|
||||||
from asyncio import get_event_loop
|
from asyncio import get_event_loop
|
||||||
|
from collections import deque
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from inspect import isawaitable
|
from inspect import isawaitable
|
||||||
from multiprocessing import Process, Event
|
from multiprocessing import Process, Event
|
||||||
|
@ -21,8 +22,8 @@ class Sanic:
|
||||||
self.router = router or Router()
|
self.router = router or Router()
|
||||||
self.error_handler = error_handler or Handler(self)
|
self.error_handler = error_handler or Handler(self)
|
||||||
self.config = Config()
|
self.config = Config()
|
||||||
self.request_middleware = []
|
self.request_middleware = deque()
|
||||||
self.response_middleware = []
|
self.response_middleware = deque()
|
||||||
self.blueprints = {}
|
self.blueprints = {}
|
||||||
self._blueprint_order = []
|
self._blueprint_order = []
|
||||||
self.loop = None
|
self.loop = None
|
||||||
|
@ -74,7 +75,7 @@ class Sanic:
|
||||||
if attach_to == 'request':
|
if attach_to == 'request':
|
||||||
self.request_middleware.append(middleware)
|
self.request_middleware.append(middleware)
|
||||||
if attach_to == 'response':
|
if attach_to == 'response':
|
||||||
self.response_middleware.insert(0, middleware)
|
self.response_middleware.appendleft(middleware)
|
||||||
return middleware
|
return middleware
|
||||||
|
|
||||||
# Detect which way this was called, @middleware or @middleware('AT')
|
# Detect which way this was called, @middleware or @middleware('AT')
|
||||||
|
|
|
@ -86,3 +86,43 @@ def test_middleware_override_response():
|
||||||
|
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert response.text == 'OK'
|
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]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user