Allow to specify subdomain and/or url_scheme in app.test_request_context()

This commit is contained in:
Ivan Velichko 2015-11-20 19:43:49 +03:00
parent 6a6a6d87c2
commit daa3f272da
3 changed files with 52 additions and 5 deletions

View file

@ -20,13 +20,24 @@ except ImportError:
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."""
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), \
'If "base_url" parameter is passed in, pass of ' \
'"subdomain" and/or "url_scheme" is meaningless.'
if base_url is None:
http_host = app.config.get('SERVER_NAME')
app_root = app.config.get('APPLICATION_ROOT')
if subdomain:
http_host = '%s.%s' % (subdomain, http_host)
if url_scheme is None:
url_scheme = 'http'
url = url_parse(path)
base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')
base_url = '%s://%s/' % (url_scheme, url.netloc or http_host or 'localhost')
if app_root:
base_url += app_root.lstrip('/')
if url.netloc: