Opened 7 years ago

Last modified 4 years ago

#29268 new Cleanup/optimization

Support passing None to test Client and RequestFactory request methods for default content_type

Reported by: Chris Jerdonek Owned by:
Component: Testing framework Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, passing content_type=None to `test.Client.post()` results in the following error:

TypeError: expected string or bytes-like object

instead of causing the default content_type value. It would be more user-friendly if passing None resulted in the default value.

One reason is as follows. Currently, if one wants to create a wrapper function around post() with the same default behavior, the user either needs to (1) duplicate / hard-code the default value in their wrapper function, or (2) special-case None with a second invocation of post() using a different function signature (i.e. one that doesn't pass any content_type value). You can even see this happening in client.py with the way that Client.post() invokes RequestFactory.post() -- the default needs to be hard-coded twice.

Change History (5)

comment:1 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

Should the same design be used for the other HTTP methods that have content_type='application/octet-stream'?

comment:2 by Chris Jerdonek, 7 years ago

Oh, good observation. I didn't examine the other methods. Yes, I do.

Generally, I've found this type of style / pattern helpful anywhere an API has a default value. Perhaps other areas of Django could use a similar treatment. Might it be a candidate for the style guide?

comment:3 by Chris Jerdonek, 7 years ago

Owner: changed from nobody to Chris Jerdonek
Status: newassigned
Summary: Support passing content_type=None to test.Client.post() to get the default valueSupport passing None to test Client and RequestFactory request methods for default content_type

comment:4 by Chris Jerdonek, 7 years ago

comment:5 by Mariusz Felisiak, 4 years ago

Owner: Chris Jerdonek removed
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top