Skip to content

Commit b16592a

Browse files
authored
Merge pull request pallets#2629 from zcchen/master
prevent double slash /a//b when blueprint prefix ends with slash
2 parents f808c20 + 401423d commit b16592a

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ unreleased
143143
:data:`SERVER_NAME` does not implicily enable it. It can be enabled by
144144
passing ``subdomain_matching=True`` to the ``Flask`` constructor.
145145
(`#2635`_)
146+
- A single trailing slash is stripped from the blueprint ``url_prefix``
147+
when it is registered with the app. (`#2629`_)
146148

147149
.. _pallets/meta#24: https://github.com/pallets/meta/issues/24
148150
.. _#1421: https://github.com/pallets/flask/issues/1421
@@ -186,6 +188,7 @@ unreleased
186188
.. _#2607: https://github.com/pallets/flask/pull/2607
187189
.. _#2636: https://github.com/pallets/flask/pull/2636
188190
.. _#2635: https://github.com/pallets/flask/pull/2635
191+
.. _#2629: https://github.com/pallets/flask/pull/2629
189192

190193

191194
Version 0.12.2

flask/blueprints.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
:copyright: © 2010 by the Pallets team.
1010
:license: BSD, see LICENSE for more details.
1111
"""
12-
1312
from functools import update_wrapper
1413

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

5453
#: The prefix that should be used for all URLs defined on the
5554
#: blueprint.
55+
if url_prefix and url_prefix[-1] == '/':
56+
url_prefix = url_prefix[:-1]
57+
5658
self.url_prefix = url_prefix
5759

5860
#: A dictionary with URL defaults that is added to each and every

tests/test_blueprints.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,20 @@ def bp_forbidden():
114114
assert client.get('/nope').data == b'you shall not pass'
115115

116116

117-
def test_blueprint_url_definitions(app, client):
117+
def test_blueprint_prefix_slash(app, client):
118+
bp = flask.Blueprint('test', __name__, url_prefix='/bar/')
119+
120+
@bp.route('/foo')
121+
def foo():
122+
return '', 204
123+
124+
app.register_blueprint(bp)
125+
app.register_blueprint(bp, url_prefix='/spam/')
126+
assert client.get('/bar/foo').status_code == 204
127+
assert client.get('/spam/foo').status_code == 204
128+
129+
130+
def test_blueprint_url_defaults(app, client):
118131
bp = flask.Blueprint('test', __name__)
119132

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

0 commit comments

Comments
 (0)