From 401423df063e82bdac25c5b94ed4edc33e05de5e Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 23 Feb 2018 15:45:37 -0800 Subject: [PATCH] only strip one slash when registering blueprint add test and changelog --- CHANGES.rst | 3 +++ flask/blueprints.py | 6 +++--- tests/test_blueprints.py | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 6a363149..d33bac95 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 @@ -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 diff --git a/flask/blueprints.py b/flask/blueprints.py index 5d8bfaae..d633685f 100644 --- a/flask/blueprints.py +++ b/flask/blueprints.py @@ -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 @@ -54,6 +52,9 @@ class BlueprintSetupState(object): #: 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 @@ -68,7 +69,6 @@ class BlueprintSetupState(object): """ 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) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index ba69073c..7984a815 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -114,7 +114,20 @@ def test_blueprint_app_error_handling(app, client): 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})