forked from sanic-org/sanic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add signals before and after handler execution (sanic-org#2540)
- Loading branch information
Showing
4 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |