Skip to content
Myr

Resolve state issue for url_for with forced scheme

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This fixes #1596

authored by Armin Ronacher · committed by Armin Ronacher · parent 883cb7c · Browse files · 2 files+21 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−2 removed

  • tests:+10 added−0 removed
  • other:+11 added−2 removed

2 files+21 added−2 removed

modified:flask/helpers.py+11 added-2 removed
298 unmodified lines
299 method = values.pop('_method', None)300 scheme = values.pop('_scheme', None)301 appctx.app.inject_url_defaults(endpoint, values)302303 # This is not the best way to deal with this but currently the304 # underlying Werkzeug router does not support overriding the scheme on305 # a per build call basis.306 old_scheme = None307 if scheme is not None:308 if not external:309 raise ValueError('When specifying _scheme, _external must be True')310 old_scheme = url_adapter.url_scheme311 url_adapter.url_scheme = scheme312313 try:314 try:309 rv = url_adapter.build(endpoint, values, method=method,310 force_external=external)315 rv = url_adapter.build(endpoint, values, method=method,316 force_external=external)317 finally:318 if old_scheme is not None:319 url_adapter.url_scheme = old_scheme320 except BuildError as error:321 # We need to inject the values again so that the app callback can322 # deal with that sort of stuff.323 values['_external'] = external
601 unmodified lines
modified:tests/test_helpers.py+10 added
625 unmodified lines
626 flask.url_for,627 'index',628 _scheme='https')629630 def test_url_for_with_alternating_schemes(self):631 app = flask.Flask(__name__)632 @app.route('/')633 def index():634 return '42'635 with app.test_request_context():636 assert flask.url_for('index', _external=True) == 'http://localhost/'637 assert flask.url_for('index', _external=True, _scheme='https') == 'https://localhost/'638 assert flask.url_for('index', _external=True) == 'http://localhost/'639640 def test_url_with_method(self):641 from flask.views import MethodView642 app = flask.Flask(__name__)643 class MyView(MethodView):
96 unmodified lines