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

Fix static routes registration on a blueprint #1954

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,10 @@ def static(
:param strict_slashes: Instruct :class:`Sanic` to check if the request
URLs need to terminate with a */*
:param content_type: user defined content type for header
:return: None
:return: routes registered on the router
:rtype: List[sanic.router.Route]
"""
static_register(
return static_register(
self,
uri,
file_or_directory,
Expand Down
19 changes: 11 additions & 8 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,18 @@ def register(self, app, options):
if _routes:
routes += _routes

# Static Files
for future in self.statics:
# Prepend the blueprint URI prefix if available
uri = url_prefix + future.uri if url_prefix else future.uri
_routes = app.static(
uri, future.file_or_directory, *future.args, **future.kwargs
)
if _routes:
routes += _routes

route_names = [route.name for route in routes if route]

# Middleware
for future in self.middlewares:
if future.args or future.kwargs:
Expand All @@ -160,14 +171,6 @@ def register(self, app, options):
for future in self.exceptions:
app.exception(*future.args, **future.kwargs)(future.handler)

# Static Files
for future in self.statics:
# Prepend the blueprint URI prefix if available
uri = url_prefix + future.uri if url_prefix else future.uri
app.static(
uri, future.file_or_directory, *future.args, **future.kwargs
)

# Event listeners
for event, listeners in self.listeners.items():
for listener in listeners:
Expand Down
5 changes: 4 additions & 1 deletion sanic/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ def register(
threshold size to switch to file_stream()
:param name: user defined name used for url_for
:param content_type: user defined content type for header
:return: registered static routes
:rtype: List[sanic.router.Route]
"""
# If we're not trying to match a file directly,
# serve from the folder
Expand All @@ -155,10 +157,11 @@ def register(
)
)

app.route(
_routes, _ = app.route(
uri,
methods=["GET", "HEAD"],
name=name,
host=host,
strict_slashes=strict_slashes,
)(_handler)
return _routes
Copy link

@digitalkaoz digitalkaoz Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this change is not necessary

30 changes: 30 additions & 0 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,36 @@ def test_static_blueprint_name(app: Sanic, static_file_directory, file_name):
_, response = app.test_client.get("/static/test.file/")
assert response.status == 200

@pytest.mark.parametrize("file_name", ["test.file"])
def test_static_blueprintp_mw(app: Sanic, static_file_directory, file_name):
current_file = inspect.getfile(inspect.currentframe())
with open(current_file, "rb") as file:
file.read()

triggered = False

bp = Blueprint(name="test_mw", url_prefix="")

@bp.middleware('request')
def bp_mw1(request):
nonlocal triggered
triggered = True

bp.static(
"/test.file",
get_file_path(static_file_directory, file_name),
strict_slashes=True,
name="static"
)

app.blueprint(bp)

uri = app.url_for("test_mw.static")
assert uri == "/test.file"

_, response = app.test_client.get("/test.file")
assert triggered is True


def test_route_handler_add(app: Sanic):
view = CompositionView()
Expand Down