Allow early Blueprint registrations to still apply later added objects (#2260)

This commit is contained in:
Adam Hopkins
2021-11-17 17:29:41 +02:00
committed by GitHub
parent b731a6b48c
commit 85e7b712b9
5 changed files with 129 additions and 37 deletions

View File

@@ -1,6 +1,4 @@
from copy import deepcopy
from sanic import Blueprint, Sanic, blueprints, response
from sanic import Blueprint, Sanic
from sanic.response import text

View File

@@ -1088,3 +1088,31 @@ def test_bp_set_attribute_warning():
"and will be removed in version 21.12. You should change your "
"Blueprint instance to use instance.ctx.foo instead."
)
def test_early_registration(app):
assert len(app.router.routes) == 0
bp = Blueprint("bp")
@bp.get("/one")
async def one(_):
return text("one")
app.blueprint(bp)
assert len(app.router.routes) == 1
@bp.get("/two")
async def two(_):
return text("two")
@bp.get("/three")
async def three(_):
return text("three")
assert len(app.router.routes) == 3
for path in ("one", "two", "three"):
_, response = app.test_client.get(f"/{path}")
assert response.text == path