Skip to content
Myr

test_wsgi.py

This file

Path

tests/test_wsgi.py

163 lines · 4.4 KiB · Python

Last changed by

BUG: wsgi.error should be TextIO, not BytesIO in WSGI transport (#1828)Adrian Garcia Badaraccobefore adoption0b4a832

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

line 3why, one step back
3from functools import partial

BUG: wsgi.error should be TextIO, not BytesIO in WSGI transport (#1828)Adrian Garcia Badaraccobefore adoption0b4a832

line 4why, one step back
4from io import StringIO

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

lines 5–23why, one step back
5
6import pytest78import httpx91011def application_factory(output):12 def application(environ, start_response):13 status = "200 OK"1415 response_headers = [16 ("Content-type", "text/plain"),17 ]1819 start_response(status, response_headers)2021 for item in output:22 yield item23

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

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

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

lines 25–73why, one step back
25
2627def echo_body(environ, start_response):28 status = "200 OK"29 output = environ["wsgi.input"].read()3031 response_headers = [32 ("Content-type", "text/plain"),33 ]3435 start_response(status, response_headers)3637 return [output]383940def echo_body_with_response_stream(environ, start_response):41 status = "200 OK"4243 response_headers = [("Content-Type", "text/plain")]4445 start_response(status, response_headers)4647 def output_generator(f):48 while True:49 output = f.read(2)50 if not output:51 break52 yield output5354 return output_generator(f=environ["wsgi.input"])555657def raise_exc(environ, start_response, exc=ValueError):58 status = "500 Server Error"59 output = b"Nope!"6061 response_headers = [62 ("Content-type", "text/plain"),63 ]6465 try:66 raise exc()67 except exc:68 exc_info = sys.exc_info()69 start_response(status, response_headers, exc_info=exc_info)7071 return [output]7273

BUG: wsgi.error should be TextIO, not BytesIO in WSGI transport (#1828)Adrian Garcia Badaraccobefore adoption0b4a832

lines 74–79why, one step back
74def log_to_wsgi_log_buffer(environ, start_response):
75 print("test1", file=environ["wsgi.errors"])76 environ["wsgi.errors"].write("test2")77 return echo_body(environ, start_response)7879

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

lines 80–88why, one step back
80def test_wsgi():
81 client = httpx.Client(app=application_factory([b"Hello, World!"]))82 response = client.get("http://www.example.org/")83 assert response.status_code == 20084 assert response.text == "Hello, World!"858687def test_wsgi_upload():88 client = httpx.Client(app=echo_body)

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–95why, one step back
90 assert response.status_code == 200
91 assert response.text == "example"929394def test_wsgi_upload_with_response_stream():95 client = httpx.Client(app=echo_body_with_response_stream)

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

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

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

lines 97–126why, one step back
97 assert response.status_code == 200
98 assert response.text == "example"99100101def test_wsgi_exc():102 client = httpx.Client(app=raise_exc)103 with pytest.raises(ValueError):104 client.get("http://www.example.org/")105106107def test_wsgi_http_error():108 client = httpx.Client(app=partial(raise_exc, exc=RuntimeError))109 with pytest.raises(RuntimeError):110 client.get("http://www.example.org/")111112113def test_wsgi_generator():114 output = [b"", b"", b"Some content", b" and more content"]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 == "Some content and more content"119120121def test_wsgi_generator_empty():122 output = [b"", b"", b"", b""]123 client = httpx.Client(app=application_factory(output))124 response = client.get("http://www.example.org/")125 assert response.status_code == 200126 assert response.text == ""

BUG: wsgi.error should be TextIO, not BytesIO in WSGI transport (#1828)Adrian Garcia Badaraccobefore adoption0b4a832

lines 127–136why, one step back
127
128129def test_logging():130 buffer = StringIO()131 transport = httpx.WSGITransport(app=log_to_wsgi_log_buffer, wsgi_errors=buffer)132 client = httpx.Client(transport=transport)133 response = client.post("http://www.example.org/", content=b"example")134 assert response.status_code == 200 # no errors135 buffer.seek(0)136 assert buffer.read() == "test1\ntest2"

Handle default ports in WSGITransport (#1469)Aberbefore adoption02a692a

lines 137–163why, one step back
137
138139@pytest.mark.parametrize(140 "url, expected_server_port",141 [142 pytest.param("http://www.example.org", "80", id="auto-http"),143 pytest.param("https://www.example.org", "443", id="auto-https"),144 pytest.param("http://www.example.org:8000", "8000", id="explicit-port"),145 ],146)147def test_wsgi_server_port(url: str, expected_server_port: int):148 """149 SERVER_PORT is populated correctly from the requested URL.150 """151 hello_world_app = application_factory([b"Hello, World!"])152 server_port: str153154 def app(environ, start_response):155 nonlocal server_port156 server_port = environ["SERVER_PORT"]157 return hello_world_app(environ, start_response)158159 client = httpx.Client(app=app)160 response = client.get(url)161 assert response.status_code == 200162 assert response.text == "Hello, World!"163 assert server_port == expected_server_port