#34074 closed New feature (fixed)
'headers' argument for test RequestFactory/Client
Reported by: | Adam Johnson | Owned by: | David Wobrock |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | David Wobrock | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
We have request.headers
since Django 2.2, but RequestFactory
, and thus Client
, still require using the awkward meta syntax for setting headers:
self.client.get("/", HTTP_ACCEPT_LANGUAGE="en")
I propose we add support for a headers
argument that takes a dictionary like request.headers
:
self.client.get("/", headers={"accept-language": "en"})
This would make tests easier to read and write, since they’d be consistent with HttpRequest
.
(If this is accepted and merged, open a django-upgrade issue to rewrite request factory and client invocations.)
Change History (10)
comment:1 Changed 14 months ago by
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 Changed 14 months ago by
Cc: | David Wobrock added |
---|---|
Has patch: | set |
Owner: | changed from nobody to David Wobrock |
Status: | new → assigned |
I agree that it would come in handy!
Here's a draft PR.
comment:3 Changed 14 months ago by
Patch needs improvement: | set |
---|
comment:4 Changed 14 months ago by
Needs documentation: | set |
---|---|
Patch needs improvement: | unset |
comment:5 Changed 14 months ago by
Needs documentation: | unset |
---|
comment:6 Changed 13 months ago by
Patch needs improvement: | set |
---|
comment:7 Changed 13 months ago by
Patch needs improvement: | unset |
---|
comment:8 Changed 13 months ago by
Triage Stage: | Accepted → Ready for checkin |
---|
Sounds good.