-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience for dynamic changes to routing (#2704)
- Loading branch information
Showing
2 changed files
with
96 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pytest | ||
|
||
from sanic import Sanic, text | ||
|
||
|
||
@pytest.fixture | ||
def late_app(app: Sanic): | ||
app.config.TOUCHUP = False | ||
app.get("/")(lambda _: text("")) | ||
return app | ||
|
||
|
||
def test_late_route(late_app: Sanic): | ||
@late_app.before_server_start | ||
async def late(app: Sanic): | ||
@app.get("/late") | ||
def handler(_): | ||
return text("late") | ||
|
||
_, response = late_app.test_client.get("/late") | ||
assert response.status_code == 200 | ||
assert response.text == "late" | ||
|
||
|
||
def test_late_middleware(late_app: Sanic): | ||
@late_app.get("/late") | ||
def handler(request): | ||
return text(request.ctx.late) | ||
|
||
@late_app.before_server_start | ||
async def late(app: Sanic): | ||
@app.on_request | ||
def handler(request): | ||
request.ctx.late = "late" | ||
|
||
_, response = late_app.test_client.get("/late") | ||
assert response.status_code == 200 | ||
assert response.text == "late" | ||
|
||
|
||
def test_late_signal(late_app: Sanic): | ||
@late_app.get("/late") | ||
def handler(request): | ||
return text(request.ctx.late) | ||
|
||
@late_app.before_server_start | ||
async def late(app: Sanic): | ||
@app.signal("http.lifecycle.request") | ||
def handler(request): | ||
request.ctx.late = "late" | ||
|
||
_, response = late_app.test_client.get("/late") | ||
assert response.status_code == 200 | ||
assert response.text == "late" |