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
How to test streaming async responses with httpx AsyncClient #2006
Comments
Update: Following some hints in this issue I was able to get this to work with https://pypi.org/project/async-asgi-testclient/ and a conftest.py that sets an event loop test fixture. Still unable to get the stream to work with httpx.AsyncClient. Updated example: stream_yes.py
Running |
somewhat related (not |
This issue should probably be opened/moved to httpx repo since it seems to be caused by httpx.AsyncClient |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Question
I have an async endpoint that streams an arbitrarily large amount of output and I want to write a test to check a portion of its streaming response. I have tried to adapt the FastAPI async tests example to use the
httpx.AsyncClient.stream
method, but it appears that the call tostream
is happening synchronously in the test. How can I do this correctly?Example
The example app below defines two endpoints:
nlines
.There are two tests:
AsyncClient.stream
. Loops over the output withaiter_lines
asserting that each line is "y" until we have read 1000 lines, then break out of the loop.AsyncClient.stream
. Loops over the all of the output withaiter_lines
asserting that each line is "n". This test should fail right away.Description
Things that work as expected:
uvicorn stream_yes:app --reload
/stream_yes_infinite
.python stream_yes.py
.Things that don't work as expected:
pytest -s stream_yes.py -k test_stream_yes_infinite
.pytest -s stream_yes.py -k test_stream_yes_truncate
.Environment
The text was updated successfully, but these errors were encountered: