From d428ae4aa8bd7ab7a2dc945f3e58987ba1c2cbcb Mon Sep 17 00:00:00 2001 From: Antoine Fontaine Date: Mon, 21 Sep 2020 18:11:13 +0200 Subject: [PATCH] allow empty data with content type application/json fix #1330 --- debug_toolbar/panels/history/panel.py | 6 +++++- tests/panels/test_history.py | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/debug_toolbar/panels/history/panel.py b/debug_toolbar/panels/history/panel.py index 41e692125..5114079a7 100644 --- a/debug_toolbar/panels/history/panel.py +++ b/debug_toolbar/panels/history/panel.py @@ -48,7 +48,11 @@ def generate_stats(self, request, response): data = request.POST.copy() # GraphQL tends to not be populated in POST. If the request seems # empty, check if it's a JSON request. - if not data and request.META.get("CONTENT_TYPE") == "application/json": + if ( + not data + and request.body + and request.META.get("CONTENT_TYPE") == "application/json" + ): # Python <= 3.5's json.loads expects a string. data = json.loads( request.body diff --git a/tests/panels/test_history.py b/tests/panels/test_history.py index 2ac38d68f..6802491ca 100644 --- a/tests/panels/test_history.py +++ b/tests/panels/test_history.py @@ -28,13 +28,21 @@ def test_post(self): self.assertEqual(data["foo"], "bar") def test_post_json(self): - self.request = rf.post( - "/", data={"foo": "bar"}, content_type="application/json" - ) - response = self.panel.process_request(self.request) - self.panel.generate_stats(self.request, response) - data = self.panel.get_stats()["data"] - self.assertEqual(data["foo"], "bar") + for data, expected_stats_data in ( + ({"foo": "bar"}, {"foo": "bar"}), + ("", {}), + ): + with self.subTest(data=data): + self.request = rf.post( + "/", + data=data, + content_type="application/json", + CONTENT_TYPE="application/json", # Force django test client to add the content-type even if no data + ) + response = self.panel.process_request(self.request) + self.panel.generate_stats(self.request, response) + data = self.panel.get_stats()["data"] + self.assertDictEqual(data, expected_stats_data) def test_urls(self): self.assertEqual(