Skip to content
Myr

test_wsgi.py

This file

Path

tests/test_wsgi.py

95 lines · 2.2 KiB · Python

Last changed by

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

History of this file

Take it

Support WSGI and ASGI apps. (#94)Tom Christiebefore adoptionbb86970

lines 1–95why, one step back
1import sys
23import pytest45import http3678def 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()64 except:65 exc_info = sys.exc_info()66 start_response(status, response_headers, exc_info=exc_info)6768 return [output]697071def test_wsgi():72 client = http3.Client(app=hello_world)73 response = client.get("http://www.example.org/")74 assert response.status_code == 20075 assert response.text == "Hello, World!"767778def test_wsgi_upload():79 client = http3.Client(app=echo_body)80 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():86 client = http3.Client(app=echo_body_with_response_stream)87 response = client.post("http://www.example.org/", data=b"example")88 assert response.status_code == 20089 assert response.text == "example"909192def test_wsgi_exc():93 client = http3.Client(app=raise_exc)94 with pytest.raises(ValueError):95 response = client.get("http://www.example.org/")