forked from orbit-oss/flask
parent
5b309831ec
commit
025589ee76
63 changed files with 3784 additions and 3459 deletions
|
|
@ -22,8 +22,7 @@ from werkzeug.urls import url_parse
|
|||
|
||||
|
||||
def make_test_environ_builder(
|
||||
app, path='/', base_url=None, subdomain=None, url_scheme=None,
|
||||
*args, **kwargs
|
||||
app, path="/", base_url=None, subdomain=None, url_scheme=None, *args, **kwargs
|
||||
):
|
||||
"""Create a :class:`~werkzeug.test.EnvironBuilder`, taking some
|
||||
defaults from the application.
|
||||
|
|
@ -46,44 +45,41 @@ def make_test_environ_builder(
|
|||
:class:`~werkzeug.test.EnvironBuilder`.
|
||||
"""
|
||||
|
||||
assert (
|
||||
not (base_url or subdomain or url_scheme)
|
||||
or (base_url is not None) != bool(subdomain or url_scheme)
|
||||
assert not (base_url or subdomain or url_scheme) or (base_url is not None) != bool(
|
||||
subdomain or url_scheme
|
||||
), 'Cannot pass "subdomain" or "url_scheme" with "base_url".'
|
||||
|
||||
if base_url is None:
|
||||
http_host = app.config.get('SERVER_NAME') or 'localhost'
|
||||
app_root = app.config['APPLICATION_ROOT']
|
||||
http_host = app.config.get("SERVER_NAME") or "localhost"
|
||||
app_root = app.config["APPLICATION_ROOT"]
|
||||
|
||||
if subdomain:
|
||||
http_host = '{0}.{1}'.format(subdomain, http_host)
|
||||
http_host = "{0}.{1}".format(subdomain, http_host)
|
||||
|
||||
if url_scheme is None:
|
||||
url_scheme = app.config['PREFERRED_URL_SCHEME']
|
||||
url_scheme = app.config["PREFERRED_URL_SCHEME"]
|
||||
|
||||
url = url_parse(path)
|
||||
base_url = '{scheme}://{netloc}/{path}'.format(
|
||||
base_url = "{scheme}://{netloc}/{path}".format(
|
||||
scheme=url.scheme or url_scheme,
|
||||
netloc=url.netloc or http_host,
|
||||
path=app_root.lstrip('/')
|
||||
path=app_root.lstrip("/"),
|
||||
)
|
||||
path = url.path
|
||||
|
||||
if url.query:
|
||||
sep = b'?' if isinstance(url.query, bytes) else '?'
|
||||
sep = b"?" if isinstance(url.query, bytes) else "?"
|
||||
path += sep + url.query
|
||||
|
||||
if 'json' in kwargs:
|
||||
assert 'data' not in kwargs, (
|
||||
"Client cannot provide both 'json' and 'data'."
|
||||
)
|
||||
if "json" in kwargs:
|
||||
assert "data" not in kwargs, "Client cannot provide both 'json' and 'data'."
|
||||
|
||||
# push a context so flask.json can use app's json attributes
|
||||
with app.app_context():
|
||||
kwargs['data'] = json_dumps(kwargs.pop('json'))
|
||||
kwargs["data"] = json_dumps(kwargs.pop("json"))
|
||||
|
||||
if 'content_type' not in kwargs:
|
||||
kwargs['content_type'] = 'application/json'
|
||||
if "content_type" not in kwargs:
|
||||
kwargs["content_type"] = "application/json"
|
||||
|
||||
return EnvironBuilder(path, base_url, *args, **kwargs)
|
||||
|
||||
|
|
@ -109,7 +105,7 @@ class FlaskClient(Client):
|
|||
super(FlaskClient, self).__init__(*args, **kwargs)
|
||||
self.environ_base = {
|
||||
"REMOTE_ADDR": "127.0.0.1",
|
||||
"HTTP_USER_AGENT": "werkzeug/" + werkzeug.__version__
|
||||
"HTTP_USER_AGENT": "werkzeug/" + werkzeug.__version__,
|
||||
}
|
||||
|
||||
@contextmanager
|
||||
|
|
@ -131,18 +127,20 @@ class FlaskClient(Client):
|
|||
passed through.
|
||||
"""
|
||||
if self.cookie_jar is None:
|
||||
raise RuntimeError('Session transactions only make sense '
|
||||
'with cookies enabled.')
|
||||
raise RuntimeError(
|
||||
"Session transactions only make sense " "with cookies enabled."
|
||||
)
|
||||
app = self.application
|
||||
environ_overrides = kwargs.setdefault('environ_overrides', {})
|
||||
environ_overrides = kwargs.setdefault("environ_overrides", {})
|
||||
self.cookie_jar.inject_wsgi(environ_overrides)
|
||||
outer_reqctx = _request_ctx_stack.top
|
||||
with app.test_request_context(*args, **kwargs) as c:
|
||||
session_interface = app.session_interface
|
||||
sess = session_interface.open_session(app, c.request)
|
||||
if sess is None:
|
||||
raise RuntimeError('Session backend did not open a session. '
|
||||
'Check the configuration')
|
||||
raise RuntimeError(
|
||||
"Session backend did not open a session. " "Check the configuration"
|
||||
)
|
||||
|
||||
# Since we have to open a new request context for the session
|
||||
# handling we want to make sure that we hide out own context
|
||||
|
|
@ -164,12 +162,13 @@ class FlaskClient(Client):
|
|||
self.cookie_jar.extract_wsgi(c.request.environ, headers)
|
||||
|
||||
def open(self, *args, **kwargs):
|
||||
as_tuple = kwargs.pop('as_tuple', False)
|
||||
buffered = kwargs.pop('buffered', False)
|
||||
follow_redirects = kwargs.pop('follow_redirects', False)
|
||||
as_tuple = kwargs.pop("as_tuple", False)
|
||||
buffered = kwargs.pop("buffered", False)
|
||||
follow_redirects = kwargs.pop("follow_redirects", False)
|
||||
|
||||
if (
|
||||
not kwargs and len(args) == 1
|
||||
not kwargs
|
||||
and len(args) == 1
|
||||
and isinstance(args[0], (EnvironBuilder, dict))
|
||||
):
|
||||
environ = self.environ_base.copy()
|
||||
|
|
@ -179,14 +178,13 @@ class FlaskClient(Client):
|
|||
else:
|
||||
environ.update(args[0])
|
||||
|
||||
environ['flask._preserve_context'] = self.preserve_context
|
||||
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
|
||||
)
|
||||
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()
|
||||
|
|
@ -194,15 +192,16 @@ class FlaskClient(Client):
|
|||
builder.close()
|
||||
|
||||
return Client.open(
|
||||
self, environ,
|
||||
self,
|
||||
environ,
|
||||
as_tuple=as_tuple,
|
||||
buffered=buffered,
|
||||
follow_redirects=follow_redirects
|
||||
follow_redirects=follow_redirects,
|
||||
)
|
||||
|
||||
def __enter__(self):
|
||||
if self.preserve_context:
|
||||
raise RuntimeError('Cannot nest client invocations')
|
||||
raise RuntimeError("Cannot nest client invocations")
|
||||
self.preserve_context = True
|
||||
return self
|
||||
|
||||
|
|
@ -222,6 +221,7 @@ class FlaskCliRunner(CliRunner):
|
|||
CLI commands. Typically created using
|
||||
:meth:`~flask.Flask.test_cli_runner`. See :ref:`testing-cli`.
|
||||
"""
|
||||
|
||||
def __init__(self, app, **kwargs):
|
||||
self.app = app
|
||||
super(FlaskCliRunner, self).__init__(**kwargs)
|
||||
|
|
@ -244,7 +244,7 @@ class FlaskCliRunner(CliRunner):
|
|||
if cli is None:
|
||||
cli = self.app.cli
|
||||
|
||||
if 'obj' not in kwargs:
|
||||
kwargs['obj'] = ScriptInfo(create_app=lambda: self.app)
|
||||
if "obj" not in kwargs:
|
||||
kwargs["obj"] = ScriptInfo(create_app=lambda: self.app)
|
||||
|
||||
return super(FlaskCliRunner, self).invoke(cli, args, **kwargs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue