-
-
Notifications
You must be signed in to change notification settings - Fork 829
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to use httpx.AsyncClient for ASGI testing without persisting cookies or open/closing it #1359
Comments
One suggestion for how this might be implemented: the |
Hello! Those are interesting points. I can see two topics here: 1/ ability to turn off cookie persistance, and 2/ refining the "unclosed client warning" so it is skipped under certain conditions. For 1/ — I don't think that's something we discussed or came across yet, mainly because there hasn't been a push or need for it yet. I don't think there's any workaround to a Lines 1324 to 1332 in 7fda99f
Lines 1118 to 1124 in 7fda99f
But we can certainly go through and change things around, if we find out that I think we may be okay with a separate ticket for optional cookie persistence, in any case? As for 2/, for the ASGI transport case incidentally Is there a way you can call # conftest.py
@pytest.fixture(scope="session")
async def client():
async with httpx.AsyncClient() as client:
datasette.some_init_hook(client=client) # `.some_init_hook` could also be private/internal. Maybe that initialisation hook doesn't exist for now (I haven't looked through the code, though the docs mention "the Alternatively, since Datasette uses ASGI underneath, you might be able to register (There's also a more general concern there about "what do we do with transports that don't keep persistent resources in practice?" (like for mock transports, or ones whose resources are on a per-request basis, etc). I'm not sure we can do anything better than to just ignore that specificity, and default to an agnostic "warn if a request was sent but |
Yeah so, two different things here. I'm absolutely in favour of supporting a no-cookie-persistence option. I've been surprised in the past that it doesn't(?) exist in requests, particularly because if you're eg. using In practise folks just tend not to notice that since they won't have differing endpoints that rely on cookie persistence between requests/responses. But all the same, you end up using a client that'll have potentially odd and inconsistent behaviour wrt cookie persistence. So, yes I think I'd like us to support this. Perhaps, with this API?... client = httpx.Client(cookies=False) In the meantime I think you can achieve the same behaviour with something like... from http import cookiejar
import httpx
class NullCookieJar(cookiejar.CookieJar):
def extract_cookies(self, request, response):
pass
client = httpx.Client(cookies=NullCookieJar()) Wrt. not closing clients. Yes, it's true that the open/close constraint exists on the transport, not on the client itself. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I've started using
httpx.AsyncClient
internally in Datasette, for two purposes:await datasette.client.get("/db/table.json")
, which lets plugin authors make in-process calls to Datasette's external JSON API, without the overhead of an HTTP network request - see https://docs.datasette.io/en/stable/internals.html#datasette-clientIn both of these cases I instantiate a new
AsyncClient
instance for every call.This has a small but measurable performance impact on my unit tests, since I need to instantiate a brand new client object for every one of several hundred tests.
I experimented with reusing a client object created like this (instead of using the
with ...
context manager):This had two problems: it persisted cookies, which isn't what I want for my test suite. It also produced warnings about unclosed clients thanks to this code:
httpx/httpx/_client.py
Lines 1779 to 1785 in ca5f524
As far as I can tell the need to open and close a client relates to the use of proxies and persistent HTTP connections. If I'm using the client to exercise an ASGI app directly I don't believe the open/closing mechanism is necessary at all.
So, my feature request: I'd love to be able to reuse a single
AsyncClient
instance in a way that doesn't persist cookies between requests and doesn't produce warnings about the client being unclosed.The text was updated successfully, but these errors were encountered: