Browse Source

support passing environ to test client (#2412)

closes #2411
pull/2383/merge
David Lord 8 years ago committed by GitHub
parent
commit
59f7966e31
  1. 3
      CHANGES
  2. 40
      flask/testing.py
  3. 18
      tests/test_testing.py

3
CHANGES

@ -97,6 +97,8 @@ Major release, unreleased
- ``Request.module`` - use ``Request.blueprint`` instead. - ``Request.module`` - use ``Request.blueprint`` instead.
- The ``request.json`` property is no longer deprecated. (`#1421`_) - The ``request.json`` property is no longer deprecated. (`#1421`_)
- Support passing an existing ``EnvironBuilder`` or ``dict`` to
``test_client.open``. (`#2412`_)
.. _#1421: https://github.com/pallets/flask/issues/1421 .. _#1421: https://github.com/pallets/flask/issues/1421
.. _#1489: https://github.com/pallets/flask/pull/1489 .. _#1489: https://github.com/pallets/flask/pull/1489
@ -124,6 +126,7 @@ Major release, unreleased
.. _#2374: https://github.com/pallets/flask/pull/2374 .. _#2374: https://github.com/pallets/flask/pull/2374
.. _#2373: https://github.com/pallets/flask/pull/2373 .. _#2373: https://github.com/pallets/flask/pull/2373
.. _#2385: https://github.com/pallets/flask/issues/2385 .. _#2385: https://github.com/pallets/flask/issues/2385
.. _#2412: https://github.com/pallets/flask/pull/2412
Version 0.12.2 Version 0.12.2
-------------- --------------

40
flask/testing.py

@ -144,19 +144,41 @@ class FlaskClient(Client):
self.cookie_jar.extract_wsgi(c.request.environ, headers) self.cookie_jar.extract_wsgi(c.request.environ, headers)
def open(self, *args, **kwargs): def open(self, *args, **kwargs):
kwargs.setdefault('environ_overrides', {}) \
['flask._preserve_context'] = self.preserve_context
kwargs.setdefault('environ_base', self.environ_base)
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)
return Client.open(self, builder, if (
as_tuple=as_tuple, not kwargs and len(args) == 1
buffered=buffered, and isinstance(args[0], (EnvironBuilder, dict))
follow_redirects=follow_redirects) ):
environ = self.environ_base.copy()
if isinstance(args[0], EnvironBuilder):
environ.update(args[0].get_environ())
else:
environ.update(args[0])
environ['flask._preserve_context'] = self.preserve_context
else:
kwargs.setdefault('environ_overrides', {}) \
['flask._preserve_context'] = self.preserve_context
kwargs.setdefault('environ_base', self.environ_base)
builder = make_test_environ_builder(
self.application, *args, **kwargs
)
try:
environ = builder.get_environ()
finally:
builder.close()
return Client.open(
self, environ,
as_tuple=as_tuple,
buffered=buffered,
follow_redirects=follow_redirects
)
def __enter__(self): def __enter__(self):
if self.preserve_context: if self.preserve_context:

18
tests/test_testing.py

@ -15,6 +15,7 @@ import werkzeug
from flask._compat import text_type from flask._compat import text_type
from flask.json import jsonify from flask.json import jsonify
from flask.testing import make_test_environ_builder
def test_environ_defaults_from_config(app, client): def test_environ_defaults_from_config(app, client):
@ -74,6 +75,23 @@ def test_environ_base_modified(app, client, app_ctx):
assert flask.g.user_agent == 'Bar' assert flask.g.user_agent == 'Bar'
def test_client_open_environ(app, client, request):
@app.route('/index')
def index():
return flask.request.remote_addr
builder = make_test_environ_builder(app, path='/index', method='GET')
request.addfinalizer(builder.close)
rv = client.open(builder)
assert rv.data == b'127.0.0.1'
environ = builder.get_environ()
client.environ_base['REMOTE_ADDR'] = '127.0.0.2'
rv = client.open(environ)
assert rv.data == b'127.0.0.2'
def test_specify_url_scheme(app, client): def test_specify_url_scheme(app, client):
@app.route('/') @app.route('/')
def index(): def index():

Loading…
Cancel
Save