Open falkben opened 2 years ago
Hi @falkben
Thank you for your detailed report.
It seems that starlette doesn't like reading the request body after starting the response. I changed the test to consume the request body and then start the response and this seems to work fine (https://github.com/vinissimus/async-asgi-testclient/pull/57).
I also created this example app without asgi-testclient to be sure it's not a bug in the test client, and it seems that the problem is in starlette:
# example.py
from starlette.applications import Starlette
from starlette.responses import StreamingResponse
app = Starlette()
async def buffered(request):
chunks = [chunk async for chunk in request.stream()]
async def gen(chunks):
for chunk in chunks:
yield chunk
return StreamingResponse(gen(chunks))
async def unbuffered(request):
return StreamingResponse(request.stream())
app.add_route("/buffered", buffered, methods=["POST"])
app.add_route("/unbuffered", unbuffered, methods=["POST"])
"""
1. Create a test file
dd if=/dev/urandom of=./file count=1024 bs=1024
2. Run server
uvicorn example:app
3. Check buffered stream works
curl -v -X POST localhost:8000/buffered --data-binary @file --output -
4. Test unbuffered stream doesn't work (empty response)
curl -v -X POST localhost:8000/unbuffered --data-binary @file --output -
"""
I was going to open a discussion in Starlette but I found this one reporting a similar problem
First, thank you for creating this! This package is the only testing utility I've found that can consume and test an asgi streaming response (fastapi/starlette).
Inside this package, with newer versions of
starlette
, I do see some failures with some of the streaming tests.Starting in 0.13.3
test_upload_stream_from_download_stream
Then in 0.13.4, the same test starts to hang, instead of outright fail.
In 0.13.5, this test starts to hang as well.
test_request_stream
In the latest version of starlette, 0.21.0,
test_request_stream
hangs, whiletest_upload_stream_from_download_stream
fails with the following error: