[Django] #35247: Test Client constructor accepts headers that are ignored/overwitten

14 views
Skip to first unread message

Django

unread,
Feb 23, 2024, 12:54:15 PM2/23/24
to django-...@googlegroups.com
#35247: Test Client constructor accepts headers that are ignored/overwitten
-----------------------------------------+------------------------
Reporter: Paul Garner | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
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 |
-----------------------------------------+------------------------
At first glance it seems like you could supply 'default' headers that are
applied to every request when instantiating a test Client...

{{{
class Client(ClientMixin, RequestFactory):
def __init__(
self,
enforce_csrf_checks=False,
raise_request_exception=True,
*,
headers=None,
query_params=None,
**defaults,
):
super().__init__(headers=headers, query_params=query_params,
**defaults)
self.handler = ClientHandler(enforce_csrf_checks)
self.raise_request_exception = raise_request_exception
self.exc_info = None
self.extra = None
self.headers = None
}}}
https://github.com/django/django/blob/main/django/test/client.py#L1059

However passing `headers` arg is completely useless, because the request
methods do this:

{{{
def get(
self,
path,
data=None,
follow=False,
secure=False,
*,
headers=None,
query_params=None,
**extra,
):
"""Request a response from the server using GET."""
self.extra = extra
self.headers = headers
response = super().get(
path,
data=data,
secure=secure,
headers=headers,
query_params=query_params,
**extra,
)
}}}
https://github.com/django/django/blob/main/django/test/client.py#L1121

This seems like a bad design? Shouldn't the `get` etc methods merge their
arg `headers` with `self.headers` without overwriting it? (and same for
`extra`)

I'm happy to submit a PR for this, assuming things aren't the way they are
for some necessary reason that I've missed.
--
Ticket URL: <https://code.djangoproject.com/ticket/35247>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 23, 2024, 1:05:00 PM2/23/24
to django-...@googlegroups.com
#35247: Test Client constructor accepts headers that are ignored/overwitten
-------------------------------+--------------------------------------
Reporter: Paul Garner | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 5.0
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 Paul Garner):

* resolution: => invalid
* status: new => closed

Comment:

despite looking wrong, it works as intended since the headers are passed
into `RequestFactory` constructor which adds them to `defaults`

so ultimately the args from the `get` call do get merged on top, just in a
roundabout way
--
Ticket URL: <https://code.djangoproject.com/ticket/35247#comment:1>

Django

unread,
Feb 23, 2024, 1:39:56 PM2/23/24
to django-...@googlegroups.com
#35247: Test Client constructor accepts headers that are ignored/overwitten
-----------------------------------+--------------------------------------
Reporter: Paul Garner | Owner: nobody
Type: Uncategorized | Status: closed
Component: Testing framework | Version: 5.0
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 Mariusz Felisiak):

* component: Uncategorized => Testing framework

Comment:

Please check how it works before filling a ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/35247#comment:2>

Django

unread,
Feb 23, 2024, 1:40:12 PM2/23/24
to django-...@googlegroups.com
#35247: Test Client constructor accepts headers that are ignored/overwitten
-----------------------------------+--------------------------------------
Reporter: Paul Garner | Owner: nobody
Type: Uncategorized | Status: closed
Component: Testing framework | Version: 5.0
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 Mariusz Felisiak):

OK, we click in the same time.
--
Ticket URL: <https://code.djangoproject.com/ticket/35247#comment:3>
Reply all
Reply to author
Forward
0 new messages