test_wsgi.py
This file
Path
tests/test_wsgi.py
118 lines · 2.9 KiB · Python
Last changed by
Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
History of this fileTake it
The walk stopped after 200 commits: lines older than 838f417 point at it.
Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
lines 1–80why, one step back1import sys2from functools import partial34import pytest56import httpx789def application_factory(output):10 def application(environ, start_response):11 status = "200 OK"1213 response_headers = [14 ("Content-type", "text/plain"),15 ]1617 start_response(status, response_headers)1819 for item in output:20 yield item2122 return application232425def 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"])535455def raise_exc(environ, start_response, exc=ValueError):56 status = "500 Server Error"57 output = b"Nope!"5859 response_headers = [60 ("Content-type", "text/plain"),61 ]6263 try:64 raise exc()65 except exc:66 exc_info = sys.exc_info()67 start_response(status, response_headers, exc_info=exc_info)6869 return [output]707172def test_wsgi():73 client = httpx.Client(app=application_factory([b"Hello, World!"]))74 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)Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 81why, one step back81 response = client.post("http://www.example.org/", content=b"example")Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
lines 82–87why, one step back82 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)Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f
line 88why, one step back88 response = client.post("http://www.example.org/", content=b"example")Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
lines 89–99why, one step back89 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):96 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):Run ASGI tests on trio too (#1020)Florimond Mancabefore adoption838f417
lines 102–118why, one step back102 client.get("http://www.example.org/")103104105def 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 == ""