From c7f56c5a5539143662a04ec8e54aa78777d20730 Mon Sep 17 00:00:00 2001 From: Daniel Pope Date: Fri, 31 May 2019 17:37:18 +0100 Subject: [PATCH] Create json_dumps() method on new EnvironBuilder --- CHANGES.rst | 2 ++ flask/testing.py | 18 +++++++++--------- tests/test_testing.py | 5 ----- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f70461ba..6d1d6151 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/flask/testing.py b/flask/testing.py index 15e6d70d..a9aa6be9 100644 --- a/flask/testing.py +++ b/flask/testing.py @@ -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): diff --git a/tests/test_testing.py b/tests/test_testing.py index 5ea72b3f..559e4ba1 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -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"