The test client and test_request_context are now both using the same logic internally for creating the environ. Also they use APPLICATION_ROOT now.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 1ea3d4e · Browse files · 5 files·+38 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+38 added−29 removed
- tests:+15 added−0 removed
- other:+23 added−29 removed
Provenance
- Commit
e853a0f- Tree
5f10e80- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
1ea3d4e
5 files changed+38 added−29 removed
modified:docs/api.rst+1 added-1 removed
221 unmodified lines222 -----------223 224 .. currentmodule:: flask.testing225 226-.. autoclass:: TestClient226+.. autoclass:: FlaskClient227 :members:228 229 230 Application Globals273 unmodified linesmodified:flask/app.py+7 added-14 removed
704 unmodified lines705 with app.test_client() as c:706 rv = c.get('/?vodka=42')707 assert request.args['vodka'] == '42'708 709- See :class:`~flask.testing.TestClient` for more information.709+ See :class:`~flask.testing.FlaskClient` for more information.710 711 .. versionchanged:: 0.4712 added support for `with` block usage for the client.713 766 unmodified lines1480 """Creates a WSGI environment from the given values (see1481 :func:`werkzeug.test.EnvironBuilder` for more information, this1482 function accepts the same arguments).1483 """1484- from werkzeug.test import create_environ1485- environ_overrides = kwargs.setdefault('environ_overrides', {})1486- if self.config.get('SERVER_NAME'):1487- server_name = self.config.get('SERVER_NAME')1488- if ':' not in server_name:1489- http_host, http_port = server_name, '80'1490- else:1491- http_host, http_port = server_name.split(':', 1)1484+ from flask.testing import make_test_environ_builder1485+ builder = make_test_environ_builder(self, *args, **kwargs)1486+ try:1487+ return self.request_context(builder.get_environ())1488+ finally:1489+ builder.close()1490 1493- environ_overrides.setdefault('SERVER_NAME', server_name)1494- environ_overrides.setdefault('HTTP_HOST', server_name)1495- environ_overrides.setdefault('SERVER_PORT', http_port)1496- return self.request_context(create_environ(*args, **kwargs))1497-1491 def wsgi_app(self, environ, start_response):1492 """The actual WSGI application. This is not implemented in1493 `__call__` so that middlewares can be applied without losing a1494 reference to the class. So instead of doing this::38 unmodified linesmodified:flask/testing.py+12 added-14 removed
13 unmodified lines14 from werkzeug.test import Client, EnvironBuilder15 from flask import _request_ctx_stack16 17 18+def make_test_environ_builder(app, path='/', base_url=None, *args, **kwargs):19+ """Creates a new test builder with some application defaults thrown in."""20+ http_host = app.config.get('SERVER_NAME')21+ app_root = app.config.get('APPLICATION_ROOT')22+ if base_url is None:23+ base_url = 'http://%s/' % (http_host or 'localhost')24+ if app_root:25+ base_url += app_root.lstrip('/')26+ return EnvironBuilder(path, base_url, *args, **kwargs)27+28+29 class FlaskClient(Client):30 """Works like a regular Werkzeug test client but has some knowledge about31 how Flask works to defer the cleanup of the request context stack to the32 end of a with body when used in a with statement. For general information60 unmodified lines93 94 as_tuple = kwargs.pop('as_tuple', False)95 buffered = kwargs.pop('buffered', False)96 follow_redirects = kwargs.pop('follow_redirects', False)97+ builder = make_test_environ_builder(self.application, *args, **kwargs)98 87- builder = EnvironBuilder(*args, **kwargs)88-89- if self.application.config.get('SERVER_NAME'):90- server_name = self.application.config.get('SERVER_NAME')91- if ':' not in server_name:92- http_host, http_port = server_name, None93- else:94- http_host, http_port = server_name.split(':', 1)95- if builder.base_url == 'http://localhost/':96- # Default Generated Base URL97- if http_port != None:98- builder.host = http_host + ':' + http_port99- else:100- builder.host = http_host99 old = _request_ctx_stack.top100 try:101 return Client.open(self, builder,102 as_tuple=as_tuple,15 unmodified linesmodified:tests/flask_tests.py+15 added
1040 unmodified lines1041 1042 1043 class TestToolsTestCase(FlaskTestCase):1044 1045+ def test_environ_defaults_from_config(self):1046+ app = flask.Flask(__name__)1047+ app.testing = True1048+ app.config['SERVER_NAME'] = 'example.com:1234'1049+ app.config['APPLICATION_ROOT'] = '/foo'1050+ @app.route('/')1051+ def index():1052+ return flask.request.url1053+1054+ ctx = app.test_request_context()1055+ self.assertEqual(ctx.request.url, 'http://example.com:1234/foo/')1056+ with app.test_client() as c:1057+ rv = c.get('/')1058+ self.assertEqual(rv.data, 'http://example.com:1234/foo/')1059+1060 def test_session_transactions(self):1061 app = flask.Flask(__name__)1062 app.testing = True1063 app.secret_key = 'testing'1232 unmodified linesmodified:CHANGES+3 added
33 unmodified lines34 see :ref:`instance-folders`.35 - Added the ``APPLICATION_ROOT`` configuration variable.36 - Implemented :meth:`~flask.testing.TestClient.session_transaction` to37 easily modify sessions from the test environment.38+- Refactored test client internally. The ``APPLICATION_ROOT`` configuration39+ variable as well as ``SERVER_NAME`` are now properly used by the test client40+ as defaults.41 42 Version 0.7.343 -------------44 238 unmodified lines