Skip to content
Myr

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

5 files+38 added−29 removed

modified:docs/api.rst+1 added-1 removed
221 unmodified lines
222-----------223224.. currentmodule:: flask.testing225226.. autoclass:: TestClient226.. autoclass:: FlaskClient227 :members:228229230Application Globals
273 unmodified lines
modified:flask/app.py+7 added-14 removed
704 unmodified lines
705 with app.test_client() as c:706 rv = c.get('/?vodka=42')707 assert request.args['vodka'] == '42'708709 See :class:`~flask.testing.TestClient` for more information.709 See :class:`~flask.testing.FlaskClient` for more information.710711 .. versionchanged:: 0.4712 added support for `with` block usage for the client.713
766 unmodified lines
1480 """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()14901493 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))14971491 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 lines
modified:flask/testing.py+12 added-14 removed
13 unmodified lines
14from werkzeug.test import Client, EnvironBuilder15from flask import _request_ctx_stack161718def 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)272829class 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 information
60 unmodified lines
9394 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)9887 builder = EnvironBuilder(*args, **kwargs)8889 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 lines
modified:tests/flask_tests.py+15 added
1040 unmodified lines
104110421043class TestToolsTestCase(FlaskTestCase):10441045 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.url10531054 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/')10591060 def test_session_transactions(self):1061 app = flask.Flask(__name__)1062 app.testing = True1063 app.secret_key = 'testing'
1232 unmodified lines
modified:CHANGES+3 added
33 unmodified lines
34 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.4142Version 0.7.343-------------44
238 unmodified lines