Skip to content

Commit

Permalink
support passing environ to test client (#2412)
Browse files Browse the repository at this point in the history
closes #2411
  • Loading branch information
davidism authored Jul 13, 2017
1 parent 817903c commit 59f7966
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Major release, unreleased
- ``Request.module`` - use ``Request.blueprint`` instead.

- 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
.. _#1489: https://github.com/pallets/flask/pull/1489
Expand Down Expand Up @@ -124,6 +126,7 @@ Major release, unreleased
.. _#2374: https://github.com/pallets/flask/pull/2374
.. _#2373: https://github.com/pallets/flask/pull/2373
.. _#2385: https://github.com/pallets/flask/issues/2385
.. _#2412: https://github.com/pallets/flask/pull/2412

Version 0.12.2
--------------
Expand Down
40 changes: 31 additions & 9 deletions flask/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,41 @@ def session_transaction(self, *args, **kwargs):
self.cookie_jar.extract_wsgi(c.request.environ, headers)

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)
buffered = kwargs.pop('buffered', False)
follow_redirects = kwargs.pop('follow_redirects', False)
builder = make_test_environ_builder(self.application, *args, **kwargs)

return Client.open(self, builder,
as_tuple=as_tuple,
buffered=buffered,
follow_redirects=follow_redirects)
if (
not kwargs and len(args) == 1
and isinstance(args[0], (EnvironBuilder, dict))
):
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):
if self.preserve_context:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from flask._compat import text_type
from flask.json import jsonify
from flask.testing import make_test_environ_builder


def test_environ_defaults_from_config(app, client):
Expand Down Expand Up @@ -74,6 +75,23 @@ def index():
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):
@app.route('/')
def index():
Expand Down

0 comments on commit 59f7966

Please sign in to comment.