Skip to content

Allow empty data with content type application/json #1331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion debug_toolbar/panels/history/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions tests/panels/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are both lines necessary? Or is it possible to remove one of the content_type parameters? (I didn't check what they do, do maybe it isn't a very smart question.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the second is to force the header option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes I see. The first makes Django automatically encode the data as JSON, the second adds the header even if data is falsy.

It's certainly convenient that Django automatically encodes the data if the content_type has some particular value. Sometimes the interdependence of different arguments just isn't obvious (enough) to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, unfortunately, this looks more like a trick, the django client don't add the content_type if there is no data, so I add manually (extra kwargs which are not known by the test client are considered as extra headers).

)
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(
Expand Down