Skip to content

Commit a37f675

Browse files
committed
Merge remote-tracking branch 'origin/0.10-maintenance'
refactor make_test_environ_builder
2 parents aeb82a4 + c72e7c7 commit a37f675

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

flask/app.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -2017,10 +2017,19 @@ def request_context(self, environ):
20172017
def test_request_context(self, *args, **kwargs):
20182018
"""Creates a WSGI environment from the given values (see
20192019
:class:`werkzeug.test.EnvironBuilder` for more information, this
2020-
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 matching
2025+
:param url_scheme: scheme for the request, default
2026+
``PREFERRED_URL_SCHEME`` or ``http``.
20212027
"""
2028+
20222029
from flask.testing import make_test_environ_builder
2030+
20232031
builder = make_test_environ_builder(self, *args, **kwargs)
2032+
20242033
try:
20252034
return self.request_context(builder.get_environ())
20262035
finally:

flask/testing.py

+28-10
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,37 @@
2121
from urlparse import urlsplit as url_parse
2222

2323

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, **kwargs
27+
):
2528
"""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+
2835
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+
2945
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.path
35-
if url.query:
36-
path += '?' + url.query
46+
base_url = '{0}://{1}/{2}'.format(
47+
url_scheme, url.netloc or http_host, app_root.lstrip('/')
48+
)
49+
path = url.path
50+
51+
if url.query:
52+
sep = b'?' if isinstance(url.query, bytes) else '?'
53+
path += sep + url.query
54+
3755
return EnvironBuilder(path, base_url, *args, **kwargs)
3856

3957

tests/test_testing.py

+32
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,38 @@ def index():
7373
assert flask.g.user_agent == 'Bar'
7474

7575

76+
def test_specify_url_scheme(app, client):
77+
@app.route('/')
78+
def index():
79+
return flask.request.url
80+
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.url
97+
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.name
103+
104+
rv = client.get('/', subdomain='xxx')
105+
assert rv.data == b'http://xxx.example.com:1234/foo/'
106+
107+
76108
def test_redirect_keep_session(app, client, app_ctx):
77109
app.secret_key = 'testing'
78110

0 commit comments

Comments
 (0)