Skip to content

Commit ba2e6ba

Browse files
authored
Merge pull request #1331 from MDziwny/master
Allow empty data with content type application/json
2 parents 43f2285 + d428ae4 commit ba2e6ba

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

debug_toolbar/panels/history/panel.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def generate_stats(self, request, response):
4848
data = request.POST.copy()
4949
# GraphQL tends to not be populated in POST. If the request seems
5050
# empty, check if it's a JSON request.
51-
if not data and request.META.get("CONTENT_TYPE") == "application/json":
51+
if (
52+
not data
53+
and request.body
54+
and request.META.get("CONTENT_TYPE") == "application/json"
55+
):
5256
# Python <= 3.5's json.loads expects a string.
5357
data = json.loads(
5458
request.body

tests/panels/test_history.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,21 @@ def test_post(self):
2828
self.assertEqual(data["foo"], "bar")
2929

3030
def test_post_json(self):
31-
self.request = rf.post(
32-
"/", data={"foo": "bar"}, content_type="application/json"
33-
)
34-
response = self.panel.process_request(self.request)
35-
self.panel.generate_stats(self.request, response)
36-
data = self.panel.get_stats()["data"]
37-
self.assertEqual(data["foo"], "bar")
31+
for data, expected_stats_data in (
32+
({"foo": "bar"}, {"foo": "bar"}),
33+
("", {}),
34+
):
35+
with self.subTest(data=data):
36+
self.request = rf.post(
37+
"/",
38+
data=data,
39+
content_type="application/json",
40+
CONTENT_TYPE="application/json", # Force django test client to add the content-type even if no data
41+
)
42+
response = self.panel.process_request(self.request)
43+
self.panel.generate_stats(self.request, response)
44+
data = self.panel.get_stats()["data"]
45+
self.assertDictEqual(data, expected_stats_data)
3846

3947
def test_urls(self):
4048
self.assertEqual(

0 commit comments

Comments
 (0)