[Django] #31810: test client populate request.headers

483 views
Skip to first unread message

Django

unread,
Jul 21, 2020, 3:42:17 PM7/21/20
to django-...@googlegroups.com
#31810: test client populate request.headers
---------------------------------------------+------------------------
Reporter: bcail | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31810>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 21, 2020, 3:42:58 PM7/21/20
to django-...@googlegroups.com
#31810: test client populate request.headers
-----------------------------------+--------------------------------------

Reporter: bcail | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by bcail):

* Attachment "request_headers.diff" added.

test showing the issue

Django

unread,
Jul 22, 2020, 1:35:12 AM7/22/20
to django-...@googlegroups.com
#31810: HttpRequest.headers doesn't contain headers without HTTP prefix.
-------------------------------+--------------------------------------
Reporter: bcail | Owner: nobody
Type: Uncategorized | Status: closed
Component: HTTP handling | Version: master
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => invalid
* component: Testing framework => HTTP handling


Comment:

You're trying to pass a custom header without HTTP prefix but
`HttpRequest.headers` contains only HTTP-prefixed headers, see
[https://docs.djangoproject.com/en/stable/ref/request-
response/#django.http.HttpRequest.headers 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31810#comment:1>

Django

unread,
Jul 22, 2020, 9:34:38 AM7/22/20
to django-...@googlegroups.com
#31810: HttpRequest.headers doesn't contain headers without HTTP prefix.
-------------------------------+--------------------------------------
Reporter: bcail | Owner: nobody

Type: Uncategorized | Status: closed
Component: HTTP handling | Version: master
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by bcail):

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?

--
Ticket URL: <https://code.djangoproject.com/ticket/31810#comment:2>

Reply all
Reply to author
Forward
0 new messages