Skip to content
Myr

support passing environ to test client (#2412)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #2411

authored by David Lord · committed by GitHub · parent 817903c · Browse files · 3 files+52 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+52 added−9 removed

  • tests:+18 added−0 removed
  • other:+34 added−9 removed

3 files+52 added−9 removed

modified:flask/testing.py+31 added-9 removed
142 unmodified lines
143 headers = resp.get_wsgi_headers(c.request.environ)144 self.cookie_jar.extract_wsgi(c.request.environ, headers)145146 def open(self, *args, **kwargs):147 kwargs.setdefault('environ_overrides', {}) \148 ['flask._preserve_context'] = self.preserve_context149 kwargs.setdefault('environ_base', self.environ_base)150147 as_tuple = kwargs.pop('as_tuple', False)148 buffered = kwargs.pop('buffered', False)149 follow_redirects = kwargs.pop('follow_redirects', False)154 builder = make_test_environ_builder(self.application, *args, **kwargs)150156 return Client.open(self, builder,157 as_tuple=as_tuple,158 buffered=buffered,159 follow_redirects=follow_redirects)151 if (152 not kwargs and len(args) == 1153 and isinstance(args[0], (EnvironBuilder, dict))154 ):155 environ = self.environ_base.copy()156157 if isinstance(args[0], EnvironBuilder):158 environ.update(args[0].get_environ())159 else:160 environ.update(args[0])161162 environ['flask._preserve_context'] = self.preserve_context163 else:164 kwargs.setdefault('environ_overrides', {}) \165 ['flask._preserve_context'] = self.preserve_context166 kwargs.setdefault('environ_base', self.environ_base)167 builder = make_test_environ_builder(168 self.application, *args, **kwargs169 )170171 try:172 environ = builder.get_environ()173 finally:174 builder.close()175176 return Client.open(177 self, environ,178 as_tuple=as_tuple,179 buffered=buffered,180 follow_redirects=follow_redirects181 )182183 def __enter__(self):184 if self.preserve_context:185 raise RuntimeError('Cannot nest client invocations')186 self.preserve_context = True
11 unmodified lines
modified:tests/test_testing.py+18 added
13 unmodified lines
14import werkzeug1516from flask._compat import text_type17from flask.json import jsonify18from flask.testing import make_test_environ_builder192021def test_environ_defaults_from_config(app, client):22 app.config['SERVER_NAME'] = 'example.com:1234'
51 unmodified lines
74 assert rv.data == b'0.0.0.1'75 assert flask.g.user_agent == 'Bar'767778def test_client_open_environ(app, client, request):79 @app.route('/index')80 def index():81 return flask.request.remote_addr8283 builder = make_test_environ_builder(app, path='/index', method='GET')84 request.addfinalizer(builder.close)8586 rv = client.open(builder)87 assert rv.data == b'127.0.0.1'8889 environ = builder.get_environ()90 client.environ_base['REMOTE_ADDR'] = '127.0.0.2'91 rv = client.open(environ)92 assert rv.data == b'127.0.0.2'939495def test_specify_url_scheme(app, client):96 @app.route('/')97 def index():98 return flask.request.url
231 unmodified lines
modified:CHANGES+3 added
95 unmodified lines
96 - ``Flask.static_path`` - use ``Flask.static_url_path`` instead.97 - ``Request.module`` - use ``Request.blueprint`` instead.9899- The ``request.json`` property is no longer deprecated. (`#1421`_)100- Support passing an existing ``EnvironBuilder`` or ``dict`` to101 ``test_client.open``. (`#2412`_)102103.. _#1421: https://github.com/pallets/flask/issues/1421104.. _#1489: https://github.com/pallets/flask/pull/1489105.. _#1621: https://github.com/pallets/flask/pull/1621
19 unmodified lines
125.. _#2362: https://github.com/pallets/flask/pull/2362126.. _#2374: https://github.com/pallets/flask/pull/2374127.. _#2373: https://github.com/pallets/flask/pull/2373128.. _#2385: https://github.com/pallets/flask/issues/2385129.. _#2412: https://github.com/pallets/flask/pull/2412130131Version 0.12.2132--------------133
617 unmodified lines