Skip to content

Commit

Permalink
only strip one slash when registering blueprint
Browse files Browse the repository at this point in the history
add test and changelog
  • Loading branch information
davidism committed Feb 23, 2018
1 parent 0887245 commit 401423d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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
6 changes: 3 additions & 3 deletions flask/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
:copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details.
"""

import re
from functools import update_wrapper

from .helpers import _PackageBoundObject, _endpoint_from_view_func
Expand Down Expand Up @@ -54,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 All @@ -68,7 +69,6 @@ def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
"""
if self.url_prefix:
rule = self.url_prefix + rule
rule = re.sub('/+', '/', rule)
options.setdefault('subdomain', self.subdomain)
if endpoint is None:
endpoint = _endpoint_from_view_func(view_func)
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

0 comments on commit 401423d

Please sign in to comment.