diff --git a/httpx/_transports/wsgi.py b/httpx/_transports/wsgi.py index 8592ffe017..7a63c0a367 100644 --- a/httpx/_transports/wsgi.py +++ b/httpx/_transports/wsgi.py @@ -119,6 +119,7 @@ def handle_request(self, request: Request) -> Response: seen_status = None seen_response_headers = None seen_exc_info = None + seen_output: typing.List[bytes] = [] def start_response( status: str, @@ -129,9 +130,11 @@ def start_response( seen_status = status seen_response_headers = response_headers seen_exc_info = exc_info - return lambda _: None + return seen_output.append result = self.app(environ, start_response) + if seen_output: + result = itertools.chain(seen_output, result) stream = WSGIByteStream(result) diff --git a/tests/test_wsgi.py b/tests/test_wsgi.py index 0134bee854..29d7fe4838 100644 --- a/tests/test_wsgi.py +++ b/tests/test_wsgi.py @@ -64,6 +64,22 @@ def output_generator(f: typing.IO[bytes]) -> typing.Iterator[bytes]: return output_generator(f=environ["wsgi.input"]) +def echo_body_with_write_callable( + environ: "WSGIEnvironment", start_response: "StartResponse" +) -> typing.Iterable[bytes]: + status = "200 OK" + output = environ["wsgi.input"].read() + + response_headers = [ + ("Content-type", "text/plain"), + ] + + write = start_response(status, response_headers) + write(output) + + return (b" and ", b"more") + + def raise_exc( environ: WSGIEnvironment, start_response: StartResponse, @@ -115,6 +131,13 @@ def test_wsgi_upload_with_response_stream(): assert response.text == "example" +def test_wsgi_upload_with_write_callable(): + client = httpx.Client(app=echo_body_with_write_callable) + response = client.post("http://www.example.org/", content=b"example") + assert response.status_code == 200 + assert response.text == "example and more" + + def test_wsgi_exc(): transport = httpx.WSGITransport(app=raise_exc) client = httpx.Client(transport=transport)