Skip to content
Myr

Default environ (#2047)

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

* Add init to FlaskClient This addresses #1467. The init in the subclass can now take in `environ_base`, which will then get passed to `make_test_environ_builder` and to `EnvironBuilder` via keyword args. This should provide the default environment capability on `app.test_client()` init. * Add kwarg `environ_base` to `make_test_environ_builder` call This change now passes `environ_base` from either `kwargs` in `FlaskClient.open` or `FlaskClient.environ_base` if passed into the init. * Fix assignment reference typo * Add default `environ_base` to `FlaskClient.__init__` * Set default kwargs for `environ_base` in `FlaskClient.open` * Remove specific environ_base kwarg since its in kwargs * Add docstring to FlaskClient detailing environ_base * Document app.test_client default environ in CHANGES * Re-word environ_base changes in FlaskClient docstring * Add client.environ_base tests * Mention preset default environ in `app.test_client` * Add versionchanged directive to docstring in FlaskClient

authored by Michael Recachinas · committed by Markus Unterwaditzer · parent cd13a5c · Browse files · 3 files+51 added−0 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+51 added−0 removed

  • tests:+35 added−0 removed
  • other:+16 added−0 removed

3 files+51 added−0 removed

modified:flask/testing.py+14 added
8 unmodified lines
9 :copyright: (c) 2015 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213import werkzeug14from contextlib import contextmanager15from werkzeug.test import Client, EnvironBuilder16from flask import _request_ctx_stack17
25 unmodified lines
43 end of a ``with`` body when used in a ``with`` statement. For general44 information about how to use this class refer to45 :class:`werkzeug.test.Client`.4647 .. versionchanged:: 0.1248 `app.test_client()` includes preset default environment, which can be49 set after instantiation of the `app.test_client()` object in50 `client.environ_base`.5152 Basic usage is outlined in the :ref:`testing` chapter.53 """5455 preserve_context = False5657 def __init__(self, *args, **kwargs):58 super(FlaskClient, self).__init__(*args, **kwargs)59 self.environ_base = {60 "REMOTE_ADDR": "127.0.0.1",61 "HTTP_USER_AGENT": "werkzeug/" + werkzeug.__version__62 }6364 @contextmanager65 def session_transaction(self, *args, **kwargs):66 """When used in combination with a ``with`` statement this opens a67 session transaction. This can be used to modify the session that
45 unmodified lines
113114 def open(self, *args, **kwargs):115 kwargs.setdefault('environ_overrides', {}) \116 ['flask._preserve_context'] = self.preserve_context117 kwargs.setdefault('environ_base', self.environ_base)118119 as_tuple = kwargs.pop('as_tuple', False)120 buffered = kwargs.pop('buffered', False)121 follow_redirects = kwargs.pop('follow_redirects', False)
22 unmodified lines
modified:tests/test_testing.py+35 added
9 unmodified lines
10"""11import pytest1213import flask14import werkzeug1516from flask._compat import text_type1718
24 unmodified lines
43 with app.test_client() as c:44 rv = c.get('/')45 assert rv.data == b'http://localhost/'4647def test_environ_base_default():48 app = flask.Flask(__name__)49 app.testing = True50 @app.route('/')51 def index():52 flask.g.user_agent = flask.request.headers["User-Agent"]53 return flask.request.remote_addr5455 with app.test_client() as c:56 rv = c.get('/')57 assert rv.data == b'127.0.0.1'58 assert flask.g.user_agent == 'werkzeug/' + werkzeug.__version__5960def test_environ_base_modified():61 app = flask.Flask(__name__)62 app.testing = True63 @app.route('/')64 def index():65 flask.g.user_agent = flask.request.headers["User-Agent"]66 return flask.request.remote_addr6768 with app.test_client() as c:69 c.environ_base['REMOTE_ADDR'] = '0.0.0.0'70 c.environ_base['HTTP_USER_AGENT'] = 'Foo'71 rv = c.get('/')72 assert rv.data == b'0.0.0.0'73 assert flask.g.user_agent == 'Foo'7475 c.environ_base['REMOTE_ADDR'] = '0.0.0.1'76 c.environ_base['HTTP_USER_AGENT'] = 'Bar'77 rv = c.get('/')78 assert rv.data == b'0.0.0.1'79 assert flask.g.user_agent == 'Bar'8081def test_redirect_keep_session():82 app = flask.Flask(__name__)83 app.secret_key = 'testing'84
187 unmodified lines
modified:CHANGES+2 added
17 unmodified lines
18- Correctly invoke response handlers for both regular request dispatching as19 well as error handlers.20- Disable logger propagation by default for the app logger.21- Add support for range requests in ``send_file``.22- ``app.test_client`` includes preset default environment, which can now be23 directly set, instead of per ``client.get``.2425Version 0.11.226--------------27
572 unmodified lines