test_wsgi.py
This file
Path
tests/test_wsgi.py
118 lines · 2.9 KiB · Python
Last changed by
Exception hierarchy (#1095)Tom Christiebefore adoption9409900
History of this fileTake it
Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 1why, one step back1import sysIncrease test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 2why, one step back2from functools import partialReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 3–8why, one step back34import pytest56import httpx78Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
lines 9–11why, one step back9def application_factory(output):10 def application(environ, start_response):11 status = "200 OK"Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 12why, one step back12Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
lines 13–15why, one step back13 response_headers = [14 ("Content-type", "text/plain"),15 ]Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 16why, one step back16Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
line 17why, one step back17 start_response(status, response_headers)Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 18why, one step back18Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
lines 19–20why, one step back19 for item in output:20 yield itemReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 21why, one step back21Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
line 22why, one step back22 return applicationReintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 23why, one step back23Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
line 24why, one step back24Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 25–54why, one step back25def echo_body(environ, start_response):26 status = "200 OK"27 output = environ["wsgi.input"].read()2829 response_headers = [30 ("Content-type", "text/plain"),31 ]3233 start_response(status, response_headers)3435 return [output]363738def echo_body_with_response_stream(environ, start_response):39 status = "200 OK"4041 response_headers = [("Content-Type", "text/plain")]4243 start_response(status, response_headers)4445 def output_generator(f):46 while True:47 output = f.read(2)48 if not output:49 break50 yield output5152 return output_generator(f=environ["wsgi.input"])5354Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
line 55why, one step back55def raise_exc(environ, start_response, exc=ValueError):Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 56–63why, one step back56 status = "500 Server Error"57 output = b"Nope!"5859 response_headers = [60 ("Content-type", "text/plain"),61 ]6263 try:Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 64–65why, one step back64 raise exc()65 except exc:Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 66–72why, one step back66 exc_info = sys.exc_info()67 start_response(status, response_headers, exc_info=exc_info)6869 return [output]707172def test_wsgi():Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
line 73why, one step back73 client = httpx.Client(app=application_factory([b"Hello, World!"]))Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
lines 74–95why, one step back74 response = client.get("http://www.example.org/")75 assert response.status_code == 20076 assert response.text == "Hello, World!"777879def test_wsgi_upload():80 client = httpx.Client(app=echo_body)81 response = client.post("http://www.example.org/", data=b"example")82 assert response.status_code == 20083 assert response.text == "example"848586def test_wsgi_upload_with_response_stream():87 client = httpx.Client(app=echo_body_with_response_stream)88 response = client.post("http://www.example.org/", data=b"example")89 assert response.status_code == 20090 assert response.text == "example"919293def test_wsgi_exc():94 client = httpx.Client(app=raise_exc)95 with pytest.raises(ValueError):Increase test coverage - take 2 (#1012)Josep Cugatbefore adoption620b067
lines 96–99why, one step back96 client.get("http://www.example.org/")979899def test_wsgi_http_error():Exception hierarchy (#1095)Tom Christiebefore adoption9409900
lines 100–101why, one step back100 client = httpx.Client(app=partial(raise_exc, exc=RuntimeError))101 with pytest.raises(RuntimeError):Reintroduce sync API. (#735)Tom Christiebefore adoptionee37a76
line 102why, one step back102 client.get("http://www.example.org/")Fix support for generator-based WSGI apps (#887)Ed Singletonbefore adoption94323f9
lines 103–118why, one step back103104105def test_wsgi_generator():106 output = [b"", b"", b"Some content", b" and more content"]107 client = httpx.Client(app=application_factory(output))108 response = client.get("http://www.example.org/")109 assert response.status_code == 200110 assert response.text == "Some content and more content"111112113def test_wsgi_generator_empty():114 output = [b"", b"", b"", b""]115 client = httpx.Client(app=application_factory(output))116 response = client.get("http://www.example.org/")117 assert response.status_code == 200118 assert response.text == ""