starlette
starlette copied to clipboard
invalid format of client's host in testclient
Discussed in https://github.com/encode/starlette/discussions/2109
Originally posted by pjknkda April 3, 2023
According to ASGI specification, scope["client"] for HTTP and Websocket protocol should have its host value in either IPv4 or IPv6 address formats.
However, in starlette/testclient.py, the _TestClientTransport set its host value to "testclient" and breaks applications that expect IPv4 or IPv6 address formats during tests.
https://github.com/encode/starlette/blob/f7bf74194358cceb5708f758989df89240c0c1aa/starlette/testclient.py#L246
https://github.com/encode/starlette/blob/f7bf74194358cceb5708f758989df89240c0c1aa/starlette/testclient.py#L264
[!IMPORTANT]
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
Is it even possible to get the client IP, port at this stage when the request is not made? I think you could allow specifying the client IP, port but not get them when the request is not done (at least not in a reliable way)
And also the spec mentions: Optional; if missing defaults to None and I think that might make more sense when working with TestClient, because both of IP, Port in scope["client"] are invalid.
PR is welcome to set scope["client"] to None.
But I'll preemptively close this issue. I don't think it is a big deal.
Setting to None and to ['testclient', 50000] are both bad solutions.
I propose to pass transport to TestClient constructor, so that it will be possible to subclass from _TestClientTransport and implement custom logic (like setting scope["client"] to None or to ['::1', 12345]).
PR welcome to add client parameter to TestClient.
I have created a PR #2810 for this...any idea how this can be tested? How was this issue first identified?