From e8ad768da5e1e9ee01157843f9b33434778c83ac Mon Sep 17 00:00:00 2001 From: Lennart Paasse Date: Tue, 27 Oct 2015 11:41:59 +0100 Subject: [PATCH] Added test & fix to prevent url_for from changing default _scheme in url_adapter --- flask/helpers.py | 2 ++ tests/test_helpers.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/flask/helpers.py b/flask/helpers.py index 861b21ab..d11d91c3 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -303,6 +303,8 @@ def url_for(endpoint, **values): if not external: raise ValueError('When specifying _scheme, _external must be True') url_adapter.url_scheme = scheme + else: + url_adapter.url_scheme = current_app.config['PREFERRED_URL_SCHEME'] try: rv = url_adapter.build(endpoint, values, method=method, diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 8d09327f..2a5a6cbb 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -578,6 +578,16 @@ class TestLogging(object): 'index', _scheme='https') + def test_url_for_with_alternating_schemes(self): + app = flask.Flask(__name__) + @app.route('/') + def index(): + return '42' + with app.test_request_context(): + assert flask.url_for('index', _external=True) == 'http://localhost/' + assert flask.url_for('index', _external=True, _scheme='https') == 'https://localhost/' + assert flask.url_for('index', _external=True) == 'http://localhost/' + def test_url_with_method(self): from flask.views import MethodView app = flask.Flask(__name__)