Merge pull request #1621 from Ostrovski/0.10-maintenance
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Allow to specify subdomain and/or url_scheme in app.test_request_cont…
authored by Kenneth Reitz · committed by GitHub · parents 08f843f, daa3f27 · 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
Provenance
- Commit
c72e7c7- Tree
b350e01- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
GitHub <noreply@github.com>- Parent 1
08f843f- Parent 2
daa3f27
3 files changed+52 added−5 removed
modified:flask/testsuite/testing.py+32 added
43 unmodified lines44 with app.test_client() as c:45 rv = c.get('/')46 self.assert_equal(rv.data, b'http://localhost/')47 48+ def test_specify_url_scheme(self):49+ app = flask.Flask(__name__)50+ app.testing = True51+ @app.route('/')52+ def index():53+ return flask.request.url54+55+ 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/')60+61+ 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'66+67+ bp = flask.Blueprint('company', __name__, subdomain='xxx')68+ @bp.route('/')69+ def index():70+ return flask.request.url71+ app.register_blueprint(bp)72+73+ 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/')79+80 def test_redirect_keep_session(self):81 app = flask.Flask(__name__)82 app.secret_key = 'testing'83 205 unmodified linesmodified:flask/app.py+5 added-1 removed
1774 unmodified lines1775 1776 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).1780+1781+ 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 linesmodified:flask/testing.py+15 added-4 removed
18 unmodified lines19 except ImportError:20 from urlparse import urlsplit as url_parse21 22 23-def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):23+def 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.'30+31 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'38+39 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.path93 unmodified lines