Skip to content
Myr

test_wsgi.py

This file

Path

tests/test_wsgi.py

146 lines · 3.8 KiB · Python

Last changed by

Close WSGI iterable when WSGIByteStream is closed (#1830)Stanis Trendelenburgbefore adoption0d3fcc7

History of this file

Take it

The walk stopped after 200 commits: lines older than fa7661b point at it.

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

line 1why, one step back
1import sys

Close WSGI iterable when WSGIByteStream is closed (#1830)Stanis Trendelenburgbefore adoption0d3fcc7

line 2why, one step back
2import wsgiref.validate

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 3–22why, one step back
3from functools import partial
45import pytest67import httpx8910def application_factory(output):11 def application(environ, start_response):12 status = "200 OK"1314 response_headers = [15 ("Content-type", "text/plain"),16 ]1718 start_response(status, response_headers)1920 for item in output:21 yield item22

Close WSGI iterable when WSGIByteStream is closed (#1830)Stanis Trendelenburgbefore adoption0d3fcc7

line 23why, one step back
23 return wsgiref.validate.validator(application)

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 24–81why, one step back
24
2526def echo_body(environ, start_response):27 status = "200 OK"28 output = environ["wsgi.input"].read()2930 response_headers = [31 ("Content-type", "text/plain"),32 ]3334 start_response(status, response_headers)3536 return [output]373839def echo_body_with_response_stream(environ, start_response):40 status = "200 OK"4142 response_headers = [("Content-Type", "text/plain")]4344 start_response(status, response_headers)4546 def output_generator(f):47 while True:48 output = f.read(2)49 if not output:50 break51 yield output5253 return output_generator(f=environ["wsgi.input"])545556def raise_exc(environ, start_response, exc=ValueError):57 status = "500 Server Error"58 output = b"Nope!"5960 response_headers = [61 ("Content-type", "text/plain"),62 ]6364 try:65 raise exc()66 except exc:67 exc_info = sys.exc_info()68 start_response(status, response_headers, exc_info=exc_info)6970 return [output]717273def test_wsgi():74 client = httpx.Client(app=application_factory([b"Hello, World!"]))75 response = client.get("http://www.example.org/")76 assert response.status_code == 20077 assert response.text == "Hello, World!"787980def test_wsgi_upload():81 client = httpx.Client(app=echo_body)

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 82why, one step back
82 response = client.post("http://www.example.org/", content=b"example")

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 83–88why, one step back
83 assert response.status_code == 200
84 assert response.text == "example"858687def test_wsgi_upload_with_response_stream():88 client = httpx.Client(app=echo_body_with_response_stream)

Seperate `content=...` and `data=...` parameters (#1266)Tom Christiebefore adoptionfeb404f

line 89why, one step back
89 response = client.post("http://www.example.org/", content=b"example")

Closing AsyncClient in all tests (#871) (#1219)cdelerbefore adoptionfa7661b

lines 90–119why, one step back
90 assert response.status_code == 200
91 assert response.text == "example"929394def test_wsgi_exc():95 client = httpx.Client(app=raise_exc)96 with pytest.raises(ValueError):97 client.get("http://www.example.org/")9899100def test_wsgi_http_error():101 client = httpx.Client(app=partial(raise_exc, exc=RuntimeError))102 with pytest.raises(RuntimeError):103 client.get("http://www.example.org/")104105106def test_wsgi_generator():107 output = [b"", b"", b"Some content", b" and more content"]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 == "Some content and more content"112113114def test_wsgi_generator_empty():115 output = [b"", b"", b"", b""]116 client = httpx.Client(app=application_factory(output))117 response = client.get("http://www.example.org/")118 assert response.status_code == 200119 assert response.text == ""

Handle default ports in WSGITransport (#1469)Aberbefore adoption02a692a

lines 120–146why, one step back
120
121122@pytest.mark.parametrize(123 "url, expected_server_port",124 [125 pytest.param("http://www.example.org", "80", id="auto-http"),126 pytest.param("https://www.example.org", "443", id="auto-https"),127 pytest.param("http://www.example.org:8000", "8000", id="explicit-port"),128 ],129)130def test_wsgi_server_port(url: str, expected_server_port: int):131 """132 SERVER_PORT is populated correctly from the requested URL.133 """134 hello_world_app = application_factory([b"Hello, World!"])135 server_port: str136137 def app(environ, start_response):138 nonlocal server_port139 server_port = environ["SERVER_PORT"]140 return hello_world_app(environ, start_response)141142 client = httpx.Client(app=app)143 response = client.get(url)144 assert response.status_code == 200145 assert response.text == "Hello, World!"146 assert server_port == expected_server_port