starlette icon indicating copy to clipboard operation
starlette copied to clipboard

invalid format of client's host in testclient

Open Kludex opened this issue 2 years ago • 3 comments

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.
Fund with Polar

Kludex avatar Oct 02 '23 14:10 Kludex

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.

aminalaee avatar Oct 04 '23 08:10 aminalaee

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.

Kludex avatar Dec 16 '23 13:12 Kludex

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]).

toxadx avatar May 15 '24 21:05 toxadx

PR welcome to add client parameter to TestClient.

Kludex avatar Dec 05 '24 08:12 Kludex

I have created a PR #2810 for this...any idea how this can be tested? How was this issue first identified?

iudeen avatar Dec 26 '24 07:12 iudeen