Add signals before and after handler execution (#2540)

This commit is contained in:
Adam Hopkins
2022-09-15 15:49:21 +03:00
committed by GitHub
parent e5010286b4
commit d352a4155e
4 changed files with 52 additions and 0 deletions

View File

@@ -531,6 +531,8 @@ async def test_signals_triggered(app):
"http.lifecycle.handle",
"http.routing.before",
"http.routing.after",
"http.handler.before",
"http.handler.after",
"http.lifecycle.response",
# "http.lifecycle.send",
# "http.lifecycle.complete",

36
tests/test_handler.py Normal file
View File

@@ -0,0 +1,36 @@
from sanic.app import Sanic
from sanic.response import empty
from sanic.signals import Event
def test_handler_operation_order(app: Sanic):
operations = []
@app.on_request
async def on_request(_):
nonlocal operations
operations.append(1)
@app.on_response
async def on_response(*_):
nonlocal operations
operations.append(5)
@app.get("/")
async def handler(_):
nonlocal operations
operations.append(3)
return empty()
@app.signal(Event.HTTP_HANDLER_BEFORE)
async def handler_before(**_):
nonlocal operations
operations.append(2)
@app.signal(Event.HTTP_HANDLER_AFTER)
async def handler_after(**_):
nonlocal operations
operations.append(4)
app.test_client.get("/")
assert operations == [1, 2, 3, 4, 5]