Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Ivan Velichko · committed by Ivan Velichko · parent 6a6a6d8 · Browse files · 3 files+52 added−5 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+52 added−5 removed

  • other:+52 added−5 removed

3 files+52 added−5 removed

modified:flask/testsuite/testing.py+32 added
43 unmodified lines
44 with app.test_client() as c:45 rv = c.get('/')46 self.assert_equal(rv.data, b'http://localhost/')4748 def test_specify_url_scheme(self):49 app = flask.Flask(__name__)50 app.testing = True51 @app.route('/')52 def index():53 return flask.request.url5455 ctx = app.test_request_context(url_scheme='https')56 self.assert_equal(ctx.request.url, 'https://localhost/')57 with app.test_client() as c:58 rv = c.get('/', url_scheme='https')59 self.assert_equal(rv.data, b'https://localhost/')6061 def test_blueprint_with_subdomain(self):62 app = flask.Flask(__name__)63 app.testing = True64 app.config['SERVER_NAME'] = 'example.com:1234'65 app.config['APPLICATION_ROOT'] = '/foo'6667 bp = flask.Blueprint('company', __name__, subdomain='xxx')68 @bp.route('/')69 def index():70 return flask.request.url71 app.register_blueprint(bp)7273 ctx = app.test_request_context('/', subdomain='xxx')74 self.assert_equal(ctx.request.url, 'http://xxx.example.com:1234/foo/')75 self.assert_equal(ctx.request.blueprint, bp.name)76 with app.test_client() as c:77 rv = c.get('/', subdomain='xxx')78 self.assert_equal(rv.data, b'http://xxx.example.com:1234/foo/')7980 def test_redirect_keep_session(self):81 app = flask.Flask(__name__)82 app.secret_key = 'testing'83
205 unmodified lines
modified:flask/app.py+5 added-1 removed
1774 unmodified lines
17751776 def test_request_context(self, *args, **kwargs):1777 """Creates a WSGI environment from the given values (see1778 :func:`werkzeug.test.EnvironBuilder` for more information, this1779 function accepts the same arguments).1779 function accepts the same arguments plus two additional).17801781 Additional arguments (might be used only if `base_url` is not specified):1782 :param subdomain: subdomain in case of testing requests handled by blueprint1783 :param url_scheme: a URL scheme (default scheme is http)1784 """1785 from flask.testing import make_test_environ_builder1786 builder = make_test_environ_builder(self, *args, **kwargs)1787 try:
60 unmodified lines
modified:flask/testing.py+15 added-4 removed
18 unmodified lines
19except ImportError:20 from urlparse import urlsplit as url_parse212223def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):23def make_test_environ_builder(app, path='/', base_url=None, subdomain=None,24 url_scheme=None, *args, **kwargs):25 """Creates a new test builder with some application defaults thrown in."""25 http_host = app.config.get('SERVER_NAME')26 app_root = app.config.get('APPLICATION_ROOT')26 assert not (base_url or subdomain or url_scheme) \27 or (base_url is not None) != bool(subdomain or url_scheme), \28 'If "base_url" parameter is passed in, pass of ' \29 '"subdomain" and/or "url_scheme" is meaningless.'3031 if base_url is None:32 http_host = app.config.get('SERVER_NAME')33 app_root = app.config.get('APPLICATION_ROOT')34 if subdomain:35 http_host = '%s.%s' % (subdomain, http_host)36 if url_scheme is None:37 url_scheme = 'http'3839 url = url_parse(path)29 base_url = 'http://%s/' % (url.netloc or http_host or 'localhost')40 base_url = '%s://%s/' % (url_scheme, url.netloc or http_host or 'localhost')41 if app_root:42 base_url += app_root.lstrip('/')43 if url.netloc:44 path = url.path
93 unmodified lines