test_wsgi.py
This file
Path
tests/test_wsgi.py
95 lines · 2.2 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 1–4why, one step back1import sys23import pytest4Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 5why, one step back5import httpxSupport WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 6–63why, one step back678def hello_world(environ, start_response):9 status = "200 OK"10 output = b"Hello, World!"1112 response_headers = [13 ("Content-type", "text/plain"),14 ("Content-Length", str(len(output))),15 ]1617 start_response(status, response_headers)1819 return [output]202122def echo_body(environ, start_response):23 status = "200 OK"24 output = environ["wsgi.input"].read()2526 response_headers = [27 ("Content-type", "text/plain"),28 ("Content-Length", str(len(output))),29 ]3031 start_response(status, response_headers)3233 return [output]343536def echo_body_with_response_stream(environ, start_response):37 status = "200 OK"3839 response_headers = [("Content-Type", "text/plain")]4041 start_response(status, response_headers)4243 def output_generator(f):44 while True:45 output = f.read(2)46 if not output:47 break48 yield output4950 return output_generator(f=environ["wsgi.input"])515253def raise_exc(environ, start_response):54 status = "500 Server Error"55 output = b"Nope!"5657 response_headers = [58 ("Content-type", "text/plain"),59 ("Content-Length", str(len(output))),60 ]6162 try:63 raise ValueError()Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca
line 64why, one step back64 except ValueError:Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 65–71why, one step back65 exc_info = sys.exc_info()66 start_response(status, response_headers, exc_info=exc_info)6768 return [output]697071def test_wsgi():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 72why, one step back72 client = httpx.Client(app=hello_world)Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 73–78why, one step back73 response = client.get("http://www.example.org/")74 assert response.status_code == 20075 assert response.text == "Hello, World!"767778def test_wsgi_upload():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 79why, one step back79 client = httpx.Client(app=echo_body)Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 80–85why, one step back80 response = client.post("http://www.example.org/", data=b"example")81 assert response.status_code == 20082 assert response.text == "example"838485def test_wsgi_upload_with_response_stream():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 86why, one step back86 client = httpx.Client(app=echo_body_with_response_stream)Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
lines 87–92why, one step back87 response = client.post("http://www.example.org/", data=b"example")88 assert response.status_code == 20089 assert response.text == "example"909192def test_wsgi_exc():Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 93why, one step back93 client = httpx.Client(app=raise_exc)Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970
line 94why, one step back94 with pytest.raises(ValueError):Start using flake8, fix lint issues (#126)Seth Michael Larsonbefore adoptionc1044ca
line 95why, one step back95 client.get("http://www.example.org/")