A use case for per-request cookies with a Client (maybe they don't need to be deprecated?) #2931
Replies: 4 comments 5 replies
-
I wonder if we might be able to provide for this by using |
Beta Was this translation helpful? Give feedback.
-
How about automatically setting domain to cookies passed as a dictionary? This could be controlled by a flag, enabled by default for security. |
Beta Was this translation helpful? Give feedback.
-
Just linking #3071 , that removes per-request cookies altogether. |
Beta Was this translation helpful? Give feedback.
-
Hey, I agree with your point of view; sometimes we have an AsyncClient for efficiency, and need to send specific cookies in a single request without having to create another client for each request needing different cookies. |
Beta Was this translation helpful? Give feedback.
-
Currently, passing
cookies
when making a request from aClient
orAsyncClient
will raise aDeprecationWarning
:The documentation says:
But I'm not sure that the behavior is always ambiguous or that persistence is always what's desired when passing
cookies
to a request method.The specific use case I have is unit testing of a web application, where I'm using a client bound to my ASGI application object to, effectively, be able to say "make a request to this URL on this ASGI app, sending these cookies".
And unfortunately I can't just, say, make a function-scoped pytest fixture that returns a client with a fixed set of cookies, because sometimes I do need to change the cookies during the course of a single test (say, to simulate what happens if a user logs in while in the middle of a longer sequence of actions). I suppose I could try instantiating one client per unique cookie set (so one that has only the non-logged-in cookies, one that represents the same user after a login, etc.), but given that some of them would make only a single request it seems a waste of a client instance and also a bit unwieldy to keep track of which client instances are bearing which cookies. Especially compared to the simplicity and clarity of just seeing the cookies being explicitly set on the appropriate request.
So I wonder if the semantic ambiguity hinted at in the deprecation warning could be resolved by breaking down the possible cases as:
Set-Cookie
header, persistcookies
is passed to a request method of the client, don't persistAt which point per-request cookies with a client might not need to be deprecated and removed? 🙏
Beta Was this translation helpful? Give feedback.
All reactions