Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#34074 closed New feature (fixed)

'headers' argument for test RequestFactory/Client

Reported by: Adam Johnson Owned by: David Wobrock
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: David Wobrock Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We have request.headers since Django 2.2, but RequestFactory, and thus Client, still require using the awkward meta syntax for setting headers:

self.client.get("/", HTTP_ACCEPT_LANGUAGE="en")

I propose we add support for a headers argument that takes a dictionary like request.headers:

self.client.get("/", headers={"accept-language": "en"})

This would make tests easier to read and write, since they’d be consistent with HttpRequest.

(If this is accepted and merged, open a django-upgrade issue to rewrite request factory and client invocations.)

Change History (10)

comment:1 by Mariusz Felisiak, 2 years ago

Triage Stage: UnreviewedAccepted

Sounds good.

comment:2 by David Wobrock, 2 years ago

Cc: David Wobrock added
Has patch: set
Owner: changed from nobody to David Wobrock
Status: newassigned

I agree that it would come in handy!

Here's a draft PR.

comment:3 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:4 by David Wobrock, 2 years ago

Needs documentation: set
Patch needs improvement: unset

comment:5 by David Wobrock, 2 years ago

Needs documentation: unset

comment:6 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:7 by David Wobrock, 2 years ago

Patch needs improvement: unset

comment:8 by Mariusz Felisiak, 2 years ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 67da22f:

Fixed #34074 -- Added headers argument to RequestFactory and Client classes.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

In 99bd5fb4:

Refs #34074 -- Used headers argument for RequestFactory and Client in docs and tests.

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