Merge pull request #667 from maxcountryman/secure-url-for
Before adoption: this commit came in with the history the project adopted, so no landing records it.
adding `_scheme` parameter to `url_for`
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 6fa449d, b5069d0 · Browse files · 2 files·+34 added−0 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+34 added−0 removed
- other:+34 added−0 removed
Provenance
- Commit
b975dd4- Tree
07b57c8- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
6fa449d- Parent 2
b5069d0
2 files changed+34 added−0 removed
modified:flask/testsuite/helpers.py+22 added
395 unmodified lines396 with app.test_request_context():397 self.assert_equal(flask.url_for('index', _anchor='x y'),398 '/#x%20y')399 400+ def test_url_for_with_scheme(self):401+ app = flask.Flask(__name__)402+ @app.route('/')403+ def index():404+ return '42'405+ with app.test_request_context():406+ self.assert_equal(flask.url_for('index',407+ _external=True,408+ _scheme='https'),409+ 'https://localhost/')410+411+ def test_url_for_with_scheme_not_external(self):412+ app = flask.Flask(__name__)413+ @app.route('/')414+ def index():415+ return '42'416+ with app.test_request_context():417+ self.assert_raises(ValueError,418+ flask.url_for,419+ 'index',420+ _scheme='https')421+422 def test_url_with_method(self):423 from flask.views import MethodView424 app = flask.Flask(__name__)425 class MyView(MethodView):108 unmodified linesmodified:flask/helpers.py+12 added
227 unmodified lines228 `endpoint` and `**values` are the arguments passed into `url_for`. Note229 that this is for building URLs outside the current application, and not for230 handling 404 NotFound errors.231 232+ .. versionadded:: 0.10233+ The `_scheme` parameter was added.234+235 .. versionadded:: 0.9236 The `_anchor` and `_method` parameters were added.237 238 .. versionadded:: 0.94 unmodified lines243 :param values: the variable arguments of the URL rule244 :param _external: if set to `True`, an absolute URL is generated. Server245 address can be changed via `SERVER_NAME` configuration variable which246 defaults to `localhost`.247+ :param _scheme: a string specifying the desired URL scheme. The `_external`248+ parameter must be set to `True` or a `ValueError` is raised.249 :param _anchor: if provided this is added as anchor to the URL.250 :param _method: if provided this explicitly specifies an HTTP method.251 """252 appctx = _app_ctx_stack.top34 unmodified lines287 external = values.pop('_external', True)288 289 anchor = values.pop('_anchor', None)290 method = values.pop('_method', None)291+ scheme = values.pop('_scheme', None)292 appctx.app.inject_url_defaults(endpoint, values)293+294+ if scheme is not None:295+ if not external:296+ raise ValueError('When specifying _scheme, _external must be True')297+ url_adapter.url_scheme = scheme298+299 try:300 rv = url_adapter.build(endpoint, values, method=method,301 force_external=external)302 except BuildError, error:539 unmodified lines