GIT-2045: enable versioning and strict slash on BlueprintGroup (#2047)
* GIT-2045: enable versioning and strict slash on BlueprintGroup * GIT-2045: convert named tuple into typed format + unit tests * GIT-2045: add example code for versioned bpg * GIT-2045: None value for strict slashes check * GIT-2045: refactor handler types and add benchmark for urlparse * GIT-2045: reduce urlparse benchmark iterations * GIT-2045: add unit test and url merge behavior * GIT-2045: cleanup example code and remove print * GIT-2045: add test for slash duplication avoidence * GIT-2045: fix issue with tailing / getting appended * GIT-2045: use Optional instead of Union for Typing * GIT-2045: use string for version arg * GIT-2045: combine optional with union
This commit is contained in:
@@ -9,7 +9,6 @@ from typing import Tuple
|
||||
import pytest
|
||||
|
||||
from sanic_routing.exceptions import RouteExists
|
||||
from sanic_testing import TestManager
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.constants import HTTP_METHODS
|
||||
|
||||
@@ -2,6 +2,7 @@ from pytest import raises
|
||||
|
||||
from sanic.app import Sanic
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.blueprint_group import BlueprintGroup
|
||||
from sanic.request import Request
|
||||
from sanic.response import HTTPResponse, text
|
||||
|
||||
@@ -200,3 +201,20 @@ def test_bp_group_as_nested_group():
|
||||
Blueprint.group(blueprint_1, blueprint_2)
|
||||
)
|
||||
assert len(blueprint_group_1) == 2
|
||||
|
||||
|
||||
def test_blueprint_group_insert():
|
||||
blueprint_1 = Blueprint(
|
||||
"blueprint_1", url_prefix="/bp1", strict_slashes=True, version=1
|
||||
)
|
||||
blueprint_2 = Blueprint("blueprint_2", url_prefix="/bp2")
|
||||
blueprint_3 = Blueprint("blueprint_3", url_prefix=None)
|
||||
group = BlueprintGroup(
|
||||
url_prefix="/test", version=1.3, strict_slashes=False
|
||||
)
|
||||
group.insert(0, blueprint_1)
|
||||
group.insert(0, blueprint_2)
|
||||
group.insert(0, blueprint_3)
|
||||
assert group.blueprints[1].strict_slashes is False
|
||||
assert group.blueprints[2].strict_slashes is True
|
||||
assert group.blueprints[0].url_prefix == "/test"
|
||||
|
||||
@@ -893,3 +893,98 @@ def test_strict_slashes_behavior_adoption():
|
||||
|
||||
assert app.test_client.get("/f1")[1].status == 200
|
||||
assert app.test_client.get("/f1/")[1].status == 200
|
||||
|
||||
|
||||
def test_blueprint_group_versioning():
|
||||
app = Sanic(name="blueprint-group-test")
|
||||
|
||||
bp1 = Blueprint(name="bp1", url_prefix="/bp1")
|
||||
bp2 = Blueprint(name="bp2", url_prefix="/bp2", version=2)
|
||||
|
||||
bp3 = Blueprint(name="bp3", url_prefix="/bp3")
|
||||
|
||||
@bp3.get("/r1")
|
||||
async def bp3_r1(request):
|
||||
return json({"from": "bp3/r1"})
|
||||
|
||||
@bp1.get("/pre-group")
|
||||
async def pre_group(request):
|
||||
return json({"from": "bp1/pre-group"})
|
||||
|
||||
group = Blueprint.group([bp1, bp2], url_prefix="/group1", version=1)
|
||||
|
||||
group2 = Blueprint.group([bp3])
|
||||
|
||||
@bp1.get("/r1")
|
||||
async def r1(request):
|
||||
return json({"from": "bp1/r1"})
|
||||
|
||||
@bp2.get("/r2")
|
||||
async def r2(request):
|
||||
return json({"from": "bp2/r2"})
|
||||
|
||||
@bp2.get("/r3", version=3)
|
||||
async def r3(request):
|
||||
return json({"from": "bp2/r3"})
|
||||
|
||||
app.blueprint([group, group2])
|
||||
|
||||
assert app.test_client.get("/v1/group1/bp1/r1/")[1].status == 200
|
||||
assert app.test_client.get("/v2/group1/bp2/r2")[1].status == 200
|
||||
assert app.test_client.get("/v1/group1/bp1/pre-group")[1].status == 200
|
||||
assert app.test_client.get("/v3/group1/bp2/r3")[1].status == 200
|
||||
assert app.test_client.get("/bp3/r1")[1].status == 200
|
||||
|
||||
assert group.version == 1
|
||||
assert group2.strict_slashes is None
|
||||
|
||||
|
||||
def test_blueprint_group_strict_slashes():
|
||||
app = Sanic(name="blueprint-group-test")
|
||||
bp1 = Blueprint(name="bp1", url_prefix=None, strict_slashes=False)
|
||||
|
||||
bp2 = Blueprint(
|
||||
name="bp2", version=3, url_prefix="/bp2", strict_slashes=None
|
||||
)
|
||||
|
||||
bp3 = Blueprint(
|
||||
name="bp3", version=None, url_prefix="/bp3/", strict_slashes=None
|
||||
)
|
||||
|
||||
@bp1.get("/r1")
|
||||
async def bp1_r1(request):
|
||||
return json({"from": "bp1/r1"})
|
||||
|
||||
@bp2.get("/r1")
|
||||
async def bp2_r1(request):
|
||||
return json({"from": "bp2/r1"})
|
||||
|
||||
@bp2.get("/r2/")
|
||||
async def bp2_r2(request):
|
||||
return json({"from": "bp2/r2"})
|
||||
|
||||
@bp3.get("/r1")
|
||||
async def bp3_r1(request):
|
||||
return json({"from": "bp3/r1"})
|
||||
|
||||
group = Blueprint.group(
|
||||
[bp1, bp2],
|
||||
url_prefix="/slash-check/",
|
||||
version=1.3,
|
||||
strict_slashes=True,
|
||||
)
|
||||
|
||||
group2 = Blueprint.group(
|
||||
[bp3], url_prefix="/other-prefix/", version="v2", strict_slashes=False
|
||||
)
|
||||
|
||||
app.blueprint(group)
|
||||
app.blueprint(group2)
|
||||
|
||||
assert app.test_client.get("/v1.3/slash-check/r1")[1].status == 200
|
||||
assert app.test_client.get("/v1.3/slash-check/r1/")[1].status == 200
|
||||
assert app.test_client.get("/v3/slash-check/bp2/r1")[1].status == 200
|
||||
assert app.test_client.get("/v3/slash-check/bp2/r1/")[1].status == 404
|
||||
assert app.test_client.get("/v3/slash-check/bp2/r2")[1].status == 404
|
||||
assert app.test_client.get("/v3/slash-check/bp2/r2/")[1].status == 200
|
||||
assert app.test_client.get("/v2/other-prefix/bp3/r1")[1].status == 200
|
||||
|
||||
Reference in New Issue
Block a user