Browse Source

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

refactor make_test_environ_builder
pull/2343/head
David Lord 7 years ago
parent
commit
a37f675ccb
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
  1. 11
      flask/app.py
  2. 38
      flask/testing.py
  3. 32
      tests/test_testing.py

11
flask/app.py

@ -2017,10 +2017,19 @@ class Flask(_PackageBoundObject):
def test_request_context(self, *args, **kwargs): def test_request_context(self, *args, **kwargs):
"""Creates a WSGI environment from the given values (see """Creates a WSGI environment from the given values (see
:class:`werkzeug.test.EnvironBuilder` for more information, this :class:`werkzeug.test.EnvironBuilder` for more information, this
function accepts the same arguments). function accepts the same arguments plus two additional).
Additional arguments (only if ``base_url`` is not specified):
:param subdomain: subdomain to use for route matching
:param url_scheme: scheme for the request, default
``PREFERRED_URL_SCHEME`` or ``http``.
""" """
from flask.testing import make_test_environ_builder from flask.testing import make_test_environ_builder
builder = make_test_environ_builder(self, *args, **kwargs) builder = make_test_environ_builder(self, *args, **kwargs)
try: try:
return self.request_context(builder.get_environ()) return self.request_context(builder.get_environ())
finally: finally:

38
flask/testing.py

@ -21,19 +21,37 @@ except ImportError:
from urlparse import urlsplit as url_parse from urlparse import urlsplit as url_parse
def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs): def make_test_environ_builder(
app, path='/', base_url=None, subdomain=None, url_scheme=None,
*args, **kwargs
):
"""Creates a new test builder with some application defaults thrown in.""" """Creates a new test builder with some application defaults thrown in."""
http_host = app.config.get('SERVER_NAME')
app_root = app.config.get('APPLICATION_ROOT') assert (
not (base_url or subdomain or url_scheme)
or (base_url is not None) != bool(subdomain or url_scheme)
), 'Cannot pass "subdomain" or "url_scheme" with "base_url".'
if base_url is None: if base_url is None:
http_host = app.config.get('SERVER_NAME') or 'localhost'
app_root = app.config.get('APPLICATION_ROOT') or '/'
if subdomain:
http_host = '{0}.{1}'.format(subdomain, http_host)
if url_scheme is None:
url_scheme = app.config.get('PREFERRED_URL_SCHEME') or 'http'
url = url_parse(path) url = url_parse(path)
base_url = 'http://%s/' % (url.netloc or http_host or 'localhost') base_url = '{0}://{1}/{2}'.format(
if app_root: url_scheme, url.netloc or http_host, app_root.lstrip('/')
base_url += app_root.lstrip('/') )
if url.netloc: path = url.path
path = url.path
if url.query: if url.query:
path += '?' + url.query sep = b'?' if isinstance(url.query, bytes) else '?'
path += sep + url.query
return EnvironBuilder(path, base_url, *args, **kwargs) return EnvironBuilder(path, base_url, *args, **kwargs)

32
tests/test_testing.py

@ -73,6 +73,38 @@ def test_environ_base_modified(app, client, app_ctx):
assert flask.g.user_agent == 'Bar' assert flask.g.user_agent == 'Bar'
def test_specify_url_scheme(app, client):
@app.route('/')
def index():
return flask.request.url
ctx = app.test_request_context(url_scheme='https')
assert ctx.request.url == 'https://localhost/'
rv = client.get('/', url_scheme='https')
assert rv.data == b'https://localhost/'
def test_blueprint_with_subdomain(app, client):
app.config['SERVER_NAME'] = 'example.com:1234'
app.config['APPLICATION_ROOT'] = '/foo'
bp = flask.Blueprint('company', __name__, subdomain='xxx')
@bp.route('/')
def index():
return flask.request.url
app.register_blueprint(bp)
ctx = app.test_request_context('/', subdomain='xxx')
assert ctx.request.url == 'http://xxx.example.com:1234/foo/'
assert ctx.request.blueprint == bp.name
rv = client.get('/', subdomain='xxx')
assert rv.data == b'http://xxx.example.com:1234/foo/'
def test_redirect_keep_session(app, client, app_ctx): def test_redirect_keep_session(app, client, app_ctx):
app.secret_key = 'testing' app.secret_key = 'testing'

Loading…
Cancel
Save