Skip to content
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

Support legacy WSGI write functions #2920

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion httpx/_transports/wsgi.py
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions tests/test_wsgi.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down