Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that blueprints with no slash is maintained when applied #2085

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def __init__(
self.routes: List[Route] = []
self.statics: List[RouteHandler] = []
self.strict_slashes = strict_slashes
self.url_prefix = url_prefix
self.url_prefix = (
url_prefix[:-1]
if url_prefix and url_prefix.endswith("/")
else url_prefix
)
self.version = version
self.websocket_routes: List[Route] = []

Expand Down
2 changes: 1 addition & 1 deletion sanic/mixins/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def route(

# Fix case where the user did not prefix the URL with a /
# and will probably get confused as to why it's not working
if not uri.startswith("/"):
if not uri.startswith("/") and (uri or hasattr(self, "router")):
uri = "/" + uri

if strict_slashes is None:
Expand Down
58 changes: 57 additions & 1 deletion tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from sanic_testing.testing import SanicTestClient

from sanic import Blueprint, Sanic
from sanic import Blueprint, Sanic, response
ahopkins marked this conversation as resolved.
Show resolved Hide resolved
from sanic.constants import HTTP_METHODS
from sanic.exceptions import NotFound, SanicException
from sanic.request import Request
Expand Down Expand Up @@ -1175,3 +1175,59 @@ async def handler(request):

with pytest.raises(SanicException):
app.router.finalize()


def test_routes_with_and_without_slash_definitions(app):
bar = Blueprint("bar", url_prefix="bar")
baz = Blueprint("baz", url_prefix="/baz")
fizz = Blueprint("fizz", url_prefix="fizz/")
buzz = Blueprint("buzz", url_prefix="/buzz/")

instances = (
(app, "foo"),
(bar, "bar"),
(baz, "baz"),
(fizz, "fizz"),
(buzz, "buzz"),
)

for instance, term in instances:
route = f"/{term}" if isinstance(instance, Sanic) else ""

@instance.get(route, strict_slashes=True)
def get_without(request):
return text(f"{term}_without")

@instance.get(f"{route}/", strict_slashes=True)
def get_with(request):
return text(f"{term}_with")

@instance.post(route, strict_slashes=True)
def post_without(request):
return text(f"{term}_without")

@instance.post(f"{route}/", strict_slashes=True)
def post_with(request):
return text(f"{term}_with")

app.blueprint(bar)
app.blueprint(baz)
app.blueprint(fizz)
app.blueprint(buzz)

for _, term in instances:
_, response = app.test_client.get(f"/{term}")
assert response.status == 200
assert response.text == f"{term}_without"

_, response = app.test_client.get(f"/{term}/")
assert response.status == 200
assert response.text == f"{term}_with"

_, response = app.test_client.post(f"/{term}")
assert response.status == 200
assert response.text == f"{term}_without"

_, response = app.test_client.post(f"/{term}/")
assert response.status == 200
assert response.text == f"{term}_with"