forked from orbit-oss/flask
Default environ (#2047)
* 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
This commit is contained in:
parent
cd13a5cf62
commit
bd5e297aa9
3 changed files with 51 additions and 0 deletions
2
CHANGES
2
CHANGES
|
|
@ -19,6 +19,8 @@ Version 0.12
|
|||
well as error handlers.
|
||||
- Disable logger propagation by default for the app logger.
|
||||
- Add support for range requests in ``send_file``.
|
||||
- ``app.test_client`` includes preset default environment, which can now be
|
||||
directly set, instead of per ``client.get``.
|
||||
|
||||
Version 0.11.2
|
||||
--------------
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import werkzeug
|
||||
from contextlib import contextmanager
|
||||
from werkzeug.test import Client, EnvironBuilder
|
||||
from flask import _request_ctx_stack
|
||||
|
|
@ -43,11 +44,23 @@ class FlaskClient(Client):
|
|||
information about how to use this class refer to
|
||||
:class:`werkzeug.test.Client`.
|
||||
|
||||
.. versionchanged:: 0.12
|
||||
`app.test_client()` includes preset default environment, which can be
|
||||
set after instantiation of the `app.test_client()` object in
|
||||
`client.environ_base`.
|
||||
|
||||
Basic usage is outlined in the :ref:`testing` chapter.
|
||||
"""
|
||||
|
||||
preserve_context = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(FlaskClient, self).__init__(*args, **kwargs)
|
||||
self.environ_base = {
|
||||
"REMOTE_ADDR": "127.0.0.1",
|
||||
"HTTP_USER_AGENT": "werkzeug/" + werkzeug.__version__
|
||||
}
|
||||
|
||||
@contextmanager
|
||||
def session_transaction(self, *args, **kwargs):
|
||||
"""When used in combination with a ``with`` statement this opens a
|
||||
|
|
@ -101,6 +114,7 @@ class FlaskClient(Client):
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
import pytest
|
||||
|
||||
import flask
|
||||
import werkzeug
|
||||
|
||||
from flask._compat import text_type
|
||||
|
||||
|
|
@ -43,6 +44,40 @@ def test_environ_defaults():
|
|||
rv = c.get('/')
|
||||
assert rv.data == b'http://localhost/'
|
||||
|
||||
def test_environ_base_default():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.user_agent = flask.request.headers["User-Agent"]
|
||||
return flask.request.remote_addr
|
||||
|
||||
with app.test_client() as c:
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'127.0.0.1'
|
||||
assert flask.g.user_agent == 'werkzeug/' + werkzeug.__version__
|
||||
|
||||
def test_environ_base_modified():
|
||||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
@app.route('/')
|
||||
def index():
|
||||
flask.g.user_agent = flask.request.headers["User-Agent"]
|
||||
return flask.request.remote_addr
|
||||
|
||||
with app.test_client() as c:
|
||||
c.environ_base['REMOTE_ADDR'] = '0.0.0.0'
|
||||
c.environ_base['HTTP_USER_AGENT'] = 'Foo'
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'0.0.0.0'
|
||||
assert flask.g.user_agent == 'Foo'
|
||||
|
||||
c.environ_base['REMOTE_ADDR'] = '0.0.0.1'
|
||||
c.environ_base['HTTP_USER_AGENT'] = 'Bar'
|
||||
rv = c.get('/')
|
||||
assert rv.data == b'0.0.0.1'
|
||||
assert flask.g.user_agent == 'Bar'
|
||||
|
||||
def test_redirect_keep_session():
|
||||
app = flask.Flask(__name__)
|
||||
app.secret_key = 'testing'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue