Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#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)

request_headers.diff (1.9 KB ) - added by bcail 4 years ago.
test showing the issue

Download all attachments as: .zip

Change History (3)

by bcail, 4 years ago

Attachment: request_headers.diff added

test showing the issue

comment:1 by Mariusz Felisiak, 4 years ago

Component: Testing frameworkHTTP handling
Resolution: invalid
Status: newclosed
Summary: test client populate request.headersHttpRequest.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 bcail, 4 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?

Note: See TracTickets for help on using tickets.
Back to Top