Browse Source

merge slashes between blueprint prefix and rule

pull/2738/head
David Lord 7 years ago
parent
commit
4d0cdf95e6
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 8
      flask/blueprints.py
  2. 21
      tests/test_blueprints.py

8
flask/blueprints.py

@ -49,12 +49,10 @@ class BlueprintSetupState(object):
url_prefix = self.options.get('url_prefix') url_prefix = self.options.get('url_prefix')
if url_prefix is None: if url_prefix is None:
url_prefix = self.blueprint.url_prefix url_prefix = self.blueprint.url_prefix
if url_prefix:
url_prefix = url_prefix.rstrip('/')
#: The prefix that should be used for all URLs defined on the #: The prefix that should be used for all URLs defined on the
#: blueprint. #: blueprint.
if url_prefix and url_prefix[-1] == '/':
url_prefix = url_prefix[:-1]
self.url_prefix = url_prefix self.url_prefix = url_prefix
#: A dictionary with URL defaults that is added to each and every #: A dictionary with URL defaults that is added to each and every
@ -68,7 +66,7 @@ class BlueprintSetupState(object):
blueprint's name. blueprint's name.
""" """
if self.url_prefix: if self.url_prefix:
rule = self.url_prefix + rule rule = '/'.join((self.url_prefix, rule.lstrip('/')))
options.setdefault('subdomain', self.subdomain) options.setdefault('subdomain', self.subdomain)
if endpoint is None: if endpoint is None:
endpoint = _endpoint_from_view_func(view_func) endpoint = _endpoint_from_view_func(view_func)

21
tests/test_blueprints.py

@ -115,17 +115,22 @@ def test_blueprint_app_error_handling(app, client):
assert client.get('/nope').data == b'you shall not pass' assert client.get('/nope').data == b'you shall not pass'
def test_blueprint_prefix_slash(app, client): @pytest.mark.parametrize(('prefix', 'rule', 'url'), (
bp = flask.Blueprint('test', __name__, url_prefix='/bar/') ('/foo/', '/bar', '/foo/bar'),
('/foo/', 'bar', '/foo/bar'),
@bp.route('/foo') ('/foo', '/bar', '/foo/bar'),
def foo(): ('/foo/', '//bar', '/foo/bar'),
('/foo//', '/bar', '/foo/bar'),
))
def test_blueprint_prefix_slash(app, client, prefix, rule, url):
bp = flask.Blueprint('test', __name__, url_prefix=prefix)
@bp.route(rule)
def index():
return '', 204 return '', 204
app.register_blueprint(bp) app.register_blueprint(bp)
app.register_blueprint(bp, url_prefix='/spam/') assert client.get(url).status_code == 204
assert client.get('/bar/foo').status_code == 204
assert client.get('/spam/foo').status_code == 204
def test_blueprint_url_defaults(app, client): def test_blueprint_url_defaults(app, client):

Loading…
Cancel
Save