diff --git a/flask/app.py b/flask/app.py index e073798a..8a191d0a 100644 --- a/flask/app.py +++ b/flask/app.py @@ -1776,7 +1776,11 @@ class Flask(_PackageBoundObject): def test_request_context(self, *args, **kwargs): """Creates a WSGI environment from the given values (see :func:`werkzeug.test.EnvironBuilder` for more information, this - function accepts the same arguments). + function accepts the same arguments plus two additional). + + Additional arguments (might be used only if `base_url` is not specified): + :param subdomain: subdomain in case of testing requests handled by blueprint + :param url_scheme: a URL scheme (default scheme is http) """ from flask.testing import make_test_environ_builder builder = make_test_environ_builder(self, *args, **kwargs) diff --git a/flask/testing.py b/flask/testing.py index 6351462b..73043f1b 100644 --- a/flask/testing.py +++ b/flask/testing.py @@ -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: diff --git a/flask/testsuite/testing.py b/flask/testsuite/testing.py index 11ab763b..9d58fef4 100644 --- a/flask/testsuite/testing.py +++ b/flask/testsuite/testing.py @@ -45,6 +45,38 @@ class TestToolsTestCase(FlaskTestCase): rv = c.get('/') self.assert_equal(rv.data, b'http://localhost/') + def test_specify_url_scheme(self): + app = flask.Flask(__name__) + app.testing = True + @app.route('/') + def index(): + return flask.request.url + + ctx = app.test_request_context(url_scheme='https') + self.assert_equal(ctx.request.url, 'https://localhost/') + with app.test_client() as c: + rv = c.get('/', url_scheme='https') + self.assert_equal(rv.data, b'https://localhost/') + + def test_blueprint_with_subdomain(self): + app = flask.Flask(__name__) + app.testing = True + 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') + self.assert_equal(ctx.request.url, 'http://xxx.example.com:1234/foo/') + self.assert_equal(ctx.request.blueprint, bp.name) + with app.test_client() as c: + rv = c.get('/', subdomain='xxx') + self.assert_equal(rv.data, b'http://xxx.example.com:1234/foo/') + def test_redirect_keep_session(self): app = flask.Flask(__name__) app.secret_key = 'testing'