#31810 closed Uncategorized (invalid)
HttpRequest.headers doesn't contain headers without HTTP prefix.
| Reported by: | bcail | Owned by: | nobody | 
|---|---|---|---|
| Component: | HTTP handling | Version: | dev | 
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
The test client doesn't seem to populate request.headers - if I pass a header in a test, it gets into request.META but not request.headers. Should that header get into request.headers as well?
This caused a problem for me when I wrote code that passed when deployed, but then the tests failed because the header wasn't getting into request.headers like it did when the project was deployed.
I'm attaching a django tests patch that shows the issue.
Attachments (1)
Change History (3)
by , 5 years ago
| Attachment: | request_headers.diff added | 
|---|
comment:1 by , 5 years ago
| Component: | Testing framework → HTTP handling | 
|---|---|
| Resolution: | → invalid | 
| Status: | new → closed | 
| Summary: | test client populate request.headers → HttpRequest.headers doesn't contain headers without HTTP prefix. | 
You're trying to pass a custom header without HTTP prefix but HttpRequest.headers contains only HTTP-prefixed headers, see documentation. It's by design. This has nothing to do with test Client. For example:
self.client.get('/request_headers/', HTTP_someheader='some header value')
works.
comment:2 by , 5 years ago
Thanks felixxm. Did this change since django 2.2? On django 2.2, when I pass a custom header in the request, it does show up in request.headers, even though I'm not putting an HTTP_ prefix on the header. Is it showing up in request.headers because django is adding HTTP_ to the custom header? I do see that django added HTTP_ to the header in request.META, but in request.headers it shows without the HTTP_.
It feels like if django is adding HTTP_ to the header, then the test client should also add HTTP_ to the header?
test showing the issue