Skip to content

TestClient.request does not honor stream=True #1102

@ergoithz

Description

@ergoithz

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

The requests.request interface exposes an stream=True option which results on the call not waiting for the entire body to arrive.
stream=True is not handled properly by starlette.testclient._ASGIAdapter.send, as it is unconditionally waiting for the entire request to finish:

loop.run_until_complete(self.app(scope, receive, send))

To reproduce

  1. Perform an event wait asyncio.Event wait() after your ASGI application responds with your headers (http.response.start) and first chunk (http.response.body), this can be easily done inside the generator passed to starlette.StreamingResponse.
  2. Set the event viaasyncio.Event set() only after doing the request with stream=True.
  3. Deadlock.

Expected behavior

With stream=True, TestClient.request should return right after first http.response.body (asgiref defines server must sent http.response.start headers only after the first http.response.body event is generated).

Streaming-related response methods (like iter_content) should be implemented via awaiting further http.response.body until more_body is missing or False.

Actual behavior

Deadlock.

Additional context

I suspect #533 could be related to this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or requesttestclientTestClient-related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions