Create json_dumps() method on new EnvironBuilder

This commit is contained in:
Daniel Pope 2019-05-31 17:37:18 +01:00
parent 976dfedaa9
commit c7f56c5a55
3 changed files with 11 additions and 14 deletions

View file

@ -66,6 +66,8 @@ Unreleased
ensuring nested contexts are cleaned up correctly. :pr:`3157`
- Show a better error message when the view return type is not
supported. :issue:`3214`
- ``flask.testing.make_test_environ_builder()`` has been deprecated in
favour of a new class ``flask.testing.EnvironBuilder``. :pr:`3232`
.. _#2935: https://github.com/pallets/flask/issues/2935
.. _#2957: https://github.com/pallets/flask/issues/2957

View file

@ -82,17 +82,17 @@ class EnvironBuilder(werkzeug.test.EnvironBuilder):
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'."
kwargs["data"] = self.json_dumps(kwargs.pop("json"), app=app)
if "content_type" not in kwargs:
kwargs["content_type"] = "application/json"
super(EnvironBuilder, self).__init__(path, base_url, *args, **kwargs)
self.app = app
super(EnvironBuilder, self).__init__(path, base_url, *args, **kwargs)
json_dumps = staticmethod(json_dumps)
def json_dumps(self, obj, **args):
"""Serialize ``obj`` to a JSON-formatted string.
The serialization will be configured according to the config associated
with this EnvironBuilder's ``app``.
"""
args.setdefault("app", self.app)
return json_dumps(obj, **args)
def make_test_environ_builder(*args, **kwargs):

View file

@ -136,11 +136,6 @@ def test_environbuilder_json_dumps(app):
assert eb.input_stream.read().decode("utf8") == u'"\u20ac"'
def test_environbuilder_json_dumps_static():
"""EnvironBuilder.json_dumps() can be called as a static method."""
assert EnvironBuilder.json_dumps(u"\u20ac") == u'"\\u20ac"'
def test_blueprint_with_subdomain():
app = flask.Flask(__name__, subdomain_matching=True)
app.config["SERVER_NAME"] = "example.com:1234"