Skip to content
Myr

Fix support for generator-based WSGI apps (#887)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Handle generator WSGI app * Lint code * Add type annotations * Add more tests * Refactor test to use application_factory * Remove content length as it's misleading * Add test for WSGI generator * Add test for empty generator * Remove previous tests * Move docstring to a comment * Fix whitespace * Fix name of function Co-Authored-By: Florimond Manca <florimond.manca@gmail.com> * Update tests/test_wsgi.py Co-Authored-By: Florimond Manca <florimond.manca@gmail.com> * Update tests/test_wsgi.py Co-Authored-By: Florimond Manca <florimond.manca@gmail.com> * Update httpx/_dispatch/wsgi.py Co-Authored-By: Florimond Manca <florimond.manca@gmail.com>

authored by Ed Singleton · committed by GitHub · parent 1c9b852 · Browse files · 2 files+40 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

2 files+40 added−12 removed

  • tests:+28 added−12 removed
  • other:+12 added−0 removed

2 files+40 added−12 removed

modified:httpx/_dispatch/wsgi.py+12 added
1import io2import itertools3import typing45from .._config import TimeoutTypes6from .._content_streams import IteratorStream7from .._models import Request, Response8from .base import SyncDispatcher91011def _skip_leading_empty_chunks(body: typing.Iterable) -> typing.Iterable:12 body = iter(body)13 for chunk in body:14 if chunk:15 return itertools.chain([chunk], body)16 return []171819class WSGIDispatch(SyncDispatcher):20 """21 A custom SyncDispatcher that handles sending requests directly to an WSGI app.22 The simplest way to use this functionality is to use the `app` argument.
73 unmodified lines
96 seen_response_headers = response_headers97 seen_exc_info = exc_info9899 result = self.app(environ, start_response)100 # This is needed because the status returned by start_response101 # shouldn't be used until the first non-empty chunk has been served.102 result = _skip_leading_empty_chunks(result)103104 assert seen_status is not None105 assert seen_response_headers is not None106 if seen_exc_info and self.raise_app_exceptions:
9 unmodified lines
modified:tests/test_wsgi.py+28 added-12 removed
3 unmodified lines
45import httpx678def hello_world(environ, start_response):9 status = "200 OK"10 output = b"Hello, World!"8def application_factory(output):9 def application(environ, start_response):10 status = "200 OK"1112 response_headers = [13 ("Content-type", "text/plain"),14 ("Content-Length", str(len(output))),15 ]12 response_headers = [13 ("Content-type", "text/plain"),14 ]1517 start_response(status, response_headers)16 start_response(status, response_headers)1719 return [output]18 for item in output:19 yield item2021 return application222324def echo_body(environ, start_response):25 status = "200 OK"26 output = environ["wsgi.input"].read()2728 response_headers = [29 ("Content-type", "text/plain"),28 ("Content-Length", str(len(output))),30 ]3132 start_response(status, response_headers)33
22 unmodified lines
56 output = b"Nope!"5758 response_headers = [59 ("Content-type", "text/plain"),59 ("Content-Length", str(len(output))),60 ]6162 try:63 raise ValueError()
4 unmodified lines
68 return [output]697071def test_wsgi():72 client = httpx.Client(app=hello_world)72 client = httpx.Client(app=application_factory([b"Hello, World!"]))73 response = client.get("http://www.example.org/")74 assert response.status_code == 20075 assert response.text == "Hello, World!"76
15 unmodified lines
92def test_wsgi_exc():93 client = httpx.Client(app=raise_exc)94 with pytest.raises(ValueError):95 client.get("http://www.example.org/")969798def test_wsgi_generator():99 output = [b"", b"", b"Some content", b" and more content"]100 client = httpx.Client(app=application_factory(output))101 response = client.get("http://www.example.org/")102 assert response.status_code == 200103 assert response.text == "Some content and more content"104105106def test_wsgi_generator_empty():107 output = [b"", b"", b"", b""]108 client = httpx.Client(app=application_factory(output))109 response = client.get("http://www.example.org/")110 assert response.status_code == 200111 assert response.text == ""