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

prevent double slash /a//b when blueprint prefix ends with slash #2629

Merged
merged 2 commits into from
Feb 24, 2018
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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ unreleased
:data:`SERVER_NAME` does not implicily enable it. It can be enabled by
passing ``subdomain_matching=True`` to the ``Flask`` constructor.
(`#2635`_)
- A single trailing slash is stripped from the blueprint ``url_prefix``
when it is registered with the app. (`#2629`_)

.. _pallets/meta#24: https://github.com/pallets/meta/issues/24
.. _#1421: https://github.com/pallets/flask/issues/1421
Expand Down Expand Up @@ -186,6 +188,7 @@ unreleased
.. _#2607: https://github.com/pallets/flask/pull/2607
.. _#2636: https://github.com/pallets/flask/pull/2636
.. _#2635: https://github.com/pallets/flask/pull/2635
.. _#2629: https://github.com/pallets/flask/pull/2629


Version 0.12.2
Expand Down
4 changes: 3 additions & 1 deletion flask/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
:copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details.
"""

from functools import update_wrapper

from .helpers import _PackageBoundObject, _endpoint_from_view_func
Expand Down Expand Up @@ -53,6 +52,9 @@ def __init__(self, blueprint, app, options, first_registration):

#: The prefix that should be used for all URLs defined on the
#: blueprint.
if url_prefix and url_prefix[-1] == '/':
url_prefix = url_prefix[:-1]

self.url_prefix = url_prefix

#: A dictionary with URL defaults that is added to each and every
Expand Down
15 changes: 14 additions & 1 deletion tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,20 @@ def bp_forbidden():
assert client.get('/nope').data == b'you shall not pass'


def test_blueprint_url_definitions(app, client):
def test_blueprint_prefix_slash(app, client):
bp = flask.Blueprint('test', __name__, url_prefix='/bar/')

@bp.route('/foo')
def foo():
return '', 204

app.register_blueprint(bp)
app.register_blueprint(bp, url_prefix='/spam/')
assert client.get('/bar/foo').status_code == 204
assert client.get('/spam/foo').status_code == 204


def test_blueprint_url_defaults(app, client):
bp = flask.Blueprint('test', __name__)

@bp.route('/foo', defaults={'baz': 42})
Expand Down