Skip to content
Myr

Merge remote-tracking branch 'origin/0.10-maintenance'

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

refactor make_test_environ_builder

authored by David Lord · committed by David Lord · parents aeb82a4, c72e7c7 · Browse files · 3 files+70 added−11 removed
This commit

State

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

Diff stats

3 files+70 added−11 removed

  • tests:+32 added−0 removed
  • other:+38 added−11 removed

3 files+70 added−11 removed

modified:flask/app.py+10 added-1 removed
2015 unmodified lines
20162017 def test_request_context(self, *args, **kwargs):2018 """Creates a WSGI environment from the given values (see2019 :class:`werkzeug.test.EnvironBuilder` for more information, this2020 function accepts the same arguments).2020 function accepts the same arguments plus two additional).20212022 Additional arguments (only if ``base_url`` is not specified):20232024 :param subdomain: subdomain to use for route matching2025 :param url_scheme: scheme for the request, default2026 ``PREFERRED_URL_SCHEME`` or ``http``.2027 """20282029 from flask.testing import make_test_environ_builder20302031 builder = make_test_environ_builder(self, *args, **kwargs)20322033 try:2034 return self.request_context(builder.get_environ())2035 finally:2036 builder.close()
53 unmodified lines
modified:flask/testing.py+28 added-10 removed
19 unmodified lines
20except ImportError:21 from urlparse import urlsplit as url_parse222324def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):24def make_test_environ_builder(25 app, path='/', base_url=None, subdomain=None, url_scheme=None,26 *args, **kwargs27):28 """Creates a new test builder with some application defaults thrown in."""26 http_host = app.config.get('SERVER_NAME')27 app_root = app.config.get('APPLICATION_ROOT')2930 assert (31 not (base_url or subdomain or url_scheme)32 or (base_url is not None) != bool(subdomain or url_scheme)33 ), 'Cannot pass "subdomain" or "url_scheme" with "base_url".'3435 if base_url is None:36 http_host = app.config.get('SERVER_NAME') or 'localhost'37 app_root = app.config.get('APPLICATION_ROOT') or '/'3839 if subdomain:40 http_host = '{0}.{1}'.format(subdomain, http_host)4142 if url_scheme is None:43 url_scheme = app.config.get('PREFERRED_URL_SCHEME') or 'http'4445 url = url_parse(path)30 base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')31 if app_root:32 base_url += app_root.lstrip('/')33 if url.netloc:34 path = url.path35 if url.query:36 path += '?' + url.query46 base_url = '{0}://{1}/{2}'.format(47 url_scheme, url.netloc or http_host, app_root.lstrip('/')48 )49 path = url.path5051 if url.query:52 sep = b'?' if isinstance(url.query, bytes) else '?'53 path += sep + url.query5455 return EnvironBuilder(path, base_url, *args, **kwargs)565758class FlaskClient(Client):
103 unmodified lines
modified:tests/test_testing.py+32 added
71 unmodified lines
72 assert rv.data == b'0.0.0.1'73 assert flask.g.user_agent == 'Bar'747576def test_specify_url_scheme(app, client):77 @app.route('/')78 def index():79 return flask.request.url8081 ctx = app.test_request_context(url_scheme='https')82 assert ctx.request.url == 'https://localhost/'8384 rv = client.get('/', url_scheme='https')85 assert rv.data == b'https://localhost/'868788def test_blueprint_with_subdomain(app, client):89 app.config['SERVER_NAME'] = 'example.com:1234'90 app.config['APPLICATION_ROOT'] = '/foo'9192 bp = flask.Blueprint('company', __name__, subdomain='xxx')9394 @bp.route('/')95 def index():96 return flask.request.url9798 app.register_blueprint(bp)99100 ctx = app.test_request_context('/', subdomain='xxx')101 assert ctx.request.url == 'http://xxx.example.com:1234/foo/'102 assert ctx.request.blueprint == bp.name103104 rv = client.get('/', subdomain='xxx')105 assert rv.data == b'http://xxx.example.com:1234/foo/'106107108def test_redirect_keep_session(app, client, app_ctx):109 app.secret_key = 'testing'110111 @app.route('/', methods=['GET', 'POST'])
186 unmodified lines