Browse Source

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.

pull/308/merge
Armin Ronacher 14 years ago
parent
commit
e853a0f739
  1. 3
      CHANGES
  2. 2
      docs/api.rst
  3. 21
      flask/app.py
  4. 26
      flask/testing.py
  5. 15
      tests/flask_tests.py

3
CHANGES

@ -35,6 +35,9 @@ Relase date to be decided, codename to be chosen.
- Added the ``APPLICATION_ROOT`` configuration variable. - Added the ``APPLICATION_ROOT`` configuration variable.
- Implemented :meth:`~flask.testing.TestClient.session_transaction` to - Implemented :meth:`~flask.testing.TestClient.session_transaction` to
easily modify sessions from the test environment. easily modify sessions from the test environment.
- Refactored test client internally. The ``APPLICATION_ROOT`` configuration
variable as well as ``SERVER_NAME`` are now properly used by the test client
as defaults.
Version 0.7.3 Version 0.7.3
------------- -------------

2
docs/api.rst

@ -223,7 +223,7 @@ Test Client
.. currentmodule:: flask.testing .. currentmodule:: flask.testing
.. autoclass:: TestClient .. autoclass:: FlaskClient
:members: :members:

21
flask/app.py

@ -706,7 +706,7 @@ class Flask(_PackageBoundObject):
rv = c.get('/?vodka=42') rv = c.get('/?vodka=42')
assert request.args['vodka'] == '42' assert request.args['vodka'] == '42'
See :class:`~flask.testing.TestClient` for more information. See :class:`~flask.testing.FlaskClient` for more information.
.. versionchanged:: 0.4 .. versionchanged:: 0.4
added support for `with` block usage for the client. added support for `with` block usage for the client.
@ -1481,19 +1481,12 @@ class Flask(_PackageBoundObject):
:func:`werkzeug.test.EnvironBuilder` for more information, this :func:`werkzeug.test.EnvironBuilder` for more information, this
function accepts the same arguments). function accepts the same arguments).
""" """
from werkzeug.test import create_environ from flask.testing import make_test_environ_builder
environ_overrides = kwargs.setdefault('environ_overrides', {}) builder = make_test_environ_builder(self, *args, **kwargs)
if self.config.get('SERVER_NAME'): try:
server_name = self.config.get('SERVER_NAME') return self.request_context(builder.get_environ())
if ':' not in server_name: finally:
http_host, http_port = server_name, '80' builder.close()
else:
http_host, http_port = server_name.split(':', 1)
environ_overrides.setdefault('SERVER_NAME', server_name)
environ_overrides.setdefault('HTTP_HOST', server_name)
environ_overrides.setdefault('SERVER_PORT', http_port)
return self.request_context(create_environ(*args, **kwargs))
def wsgi_app(self, environ, start_response): def wsgi_app(self, environ, start_response):
"""The actual WSGI application. This is not implemented in """The actual WSGI application. This is not implemented in

26
flask/testing.py

@ -15,6 +15,17 @@ from werkzeug.test import Client, EnvironBuilder
from flask import _request_ctx_stack from flask import _request_ctx_stack
def make_test_environ_builder(app, path='/', base_url=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')
if base_url is None:
base_url = 'http://%s/' % (http_host or 'localhost')
if app_root:
base_url += app_root.lstrip('/')
return EnvironBuilder(path, base_url, *args, **kwargs)
class FlaskClient(Client): class FlaskClient(Client):
"""Works like a regular Werkzeug test client but has some knowledge about """Works like a regular Werkzeug test client but has some knowledge about
how Flask works to defer the cleanup of the request context stack to the how Flask works to defer the cleanup of the request context stack to the
@ -83,21 +94,8 @@ class FlaskClient(Client):
as_tuple = kwargs.pop('as_tuple', False) as_tuple = kwargs.pop('as_tuple', False)
buffered = kwargs.pop('buffered', False) buffered = kwargs.pop('buffered', False)
follow_redirects = kwargs.pop('follow_redirects', False) follow_redirects = kwargs.pop('follow_redirects', False)
builder = make_test_environ_builder(self.application, *args, **kwargs)
builder = EnvironBuilder(*args, **kwargs)
if self.application.config.get('SERVER_NAME'):
server_name = self.application.config.get('SERVER_NAME')
if ':' not in server_name:
http_host, http_port = server_name, None
else:
http_host, http_port = server_name.split(':', 1)
if builder.base_url == 'http://localhost/':
# Default Generated Base URL
if http_port != None:
builder.host = http_host + ':' + http_port
else:
builder.host = http_host
old = _request_ctx_stack.top old = _request_ctx_stack.top
try: try:
return Client.open(self, builder, return Client.open(self, builder,

15
tests/flask_tests.py

@ -1042,6 +1042,21 @@ class BasicFunctionalityTestCase(FlaskTestCase):
class TestToolsTestCase(FlaskTestCase): class TestToolsTestCase(FlaskTestCase):
def test_environ_defaults_from_config(self):
app = flask.Flask(__name__)
app.testing = True
app.config['SERVER_NAME'] = 'example.com:1234'
app.config['APPLICATION_ROOT'] = '/foo'
@app.route('/')
def index():
return flask.request.url
ctx = app.test_request_context()
self.assertEqual(ctx.request.url, 'http://example.com:1234/foo/')
with app.test_client() as c:
rv = c.get('/')
self.assertEqual(rv.data, 'http://example.com:1234/foo/')
def test_session_transactions(self): def test_session_transactions(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.testing = True app.testing = True

Loading…
Cancel
Save