#35082 closed Bug (invalid)

AsyncRequestFactory doesn't pass REMOTE_ADDR when set

Reported by: alex 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

I can describe this bug easily with an example:

from django.test import RequestFactory, AsyncRequestFactory
factory = RequestFactory()
afactory = AsyncRequestFactory()
request = factory.get("/customer/details", REMOTE_ADDR="127.0.1.1")
assert request.META["REMOTE_ADDR"] == "127.0.1.1"
request = afactory.get("/customer/details", REMOTE_ADDR="127.0.1.1")
# will fail
assert request.META["REMOTE_ADDR"] == "127.0.1.1"

I think there are some other variables also affected. I tested it only with django 5.0.

Change History (2)

comment:1 by alex, 11 months ago

Workaround: often it is enough to use RequestFactory even it is incorrect

comment:2 by Mariusz Felisiak, 11 months ago

Resolution: invalid
Status: newclosed

According to the ASGI spec, REMOTE_ADDR is based on the client in the connection scope, so you can override it with:

>>> afactory = AsyncRequestFactory(client=("127.0.1.1", "88"))
>>> request = afactory.get("/somewhere")
>>> request.META["REMOTE_ADDR"]
"127.0.1.1"

If you're having trouble understanding how Django works, see TicketClosingReasons/UseSupportChannels for ways to get help.

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