Opened 10 months ago

Closed 10 months ago

Last modified 10 months ago

#35247 closed Uncategorized (invalid)

Test Client constructor accepts headers that are ignored/overwitten

Reported by: Paul Garner Owned by: nobody
Component: Testing framework Version: 5.0
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

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.

Change History (2)

comment:1 by Paul Garner, 10 months ago

Resolution: invalid
Status: newclosed

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

comment:2 by Mariusz Felisiak, 10 months ago

Component: UncategorizedTesting framework
Last edited 10 months ago by Mariusz Felisiak (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top