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
Provenance
- Commit
a37f675- Tree
1320f14- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
aeb82a4- Parent 2
c72e7c7
3 files changed+70 added−11 removed
modified:flask/app.py+10 added-1 removed
2015 unmodified lines2016 2017 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).2021+2022+ Additional arguments (only if ``base_url`` is not specified):2023+2024+ :param subdomain: subdomain to use for route matching2025+ :param url_scheme: scheme for the request, default2026+ ``PREFERRED_URL_SCHEME`` or ``http``.2027 """2028+2029 from flask.testing import make_test_environ_builder2030+2031 builder = make_test_environ_builder(self, *args, **kwargs)2032+2033 try:2034 return self.request_context(builder.get_environ())2035 finally:2036 builder.close()53 unmodified linesmodified:flask/testing.py+28 added-10 removed
19 unmodified lines20 except ImportError:21 from urlparse import urlsplit as url_parse22 23 24-def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):24+def 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')29+30+ 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".'34+35 if base_url is None:36+ http_host = app.config.get('SERVER_NAME') or 'localhost'37+ app_root = app.config.get('APPLICATION_ROOT') or '/'38+39+ if subdomain:40+ http_host = '{0}.{1}'.format(subdomain, http_host)41+42+ if url_scheme is None:43+ url_scheme = app.config.get('PREFERRED_URL_SCHEME') or 'http'44+45 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.path50+51+ if url.query:52+ sep = b'?' if isinstance(url.query, bytes) else '?'53+ path += sep + url.query54+55 return EnvironBuilder(path, base_url, *args, **kwargs)56 57 58 class FlaskClient(Client):103 unmodified linesmodified:tests/test_testing.py+32 added
71 unmodified lines72 assert rv.data == b'0.0.0.1'73 assert flask.g.user_agent == 'Bar'74 75 76+def test_specify_url_scheme(app, client):77+ @app.route('/')78+ def index():79+ return flask.request.url80+81+ ctx = app.test_request_context(url_scheme='https')82+ assert ctx.request.url == 'https://localhost/'83+84+ rv = client.get('/', url_scheme='https')85+ assert rv.data == b'https://localhost/'86+87+88+def test_blueprint_with_subdomain(app, client):89+ app.config['SERVER_NAME'] = 'example.com:1234'90+ app.config['APPLICATION_ROOT'] = '/foo'91+92+ bp = flask.Blueprint('company', __name__, subdomain='xxx')93+94+ @bp.route('/')95+ def index():96+ return flask.request.url97+98+ app.register_blueprint(bp)99+100+ ctx = app.test_request_context('/', subdomain='xxx')101+ assert ctx.request.url == 'http://xxx.example.com:1234/foo/'102+ assert ctx.request.blueprint == bp.name103+104+ rv = client.get('/', subdomain='xxx')105+ assert rv.data == b'http://xxx.example.com:1234/foo/'106+107+108 def test_redirect_keep_session(app, client, app_ctx):109 app.secret_key = 'testing'110 111 @app.route('/', methods=['GET', 'POST'])186 unmodified lines