test_wsgi.py
This file
Path
tests/test_wsgi.py
194 lines · 5.4 KiB · Python
Last changed by
Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
History of this fileTake it
The walk stopped after 200 commits: lines older than deb904d point at it.
Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
lines 1–2why, one step back1from __future__ import annotations2remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
line 3why, one step back3import sysTyping: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 4why, one step back4import typingremove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 5–12why, one step back5import wsgiref.validate6from functools import partial7from io import StringIO89import pytest1011import httpx12Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
lines 13–14why, one step back13if typing.TYPE_CHECKING: # pragma: no cover14 from _typeshed.wsgi import StartResponse, WSGIApplication, WSGIEnvironmentremove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
line 15why, one step back15Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 16why, one step back16Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
line 17why, one step back17def application_factory(output: typing.Iterable[bytes]) -> WSGIApplication:remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 18–32why, one step back18 def application(environ, start_response):19 status = "200 OK"2021 response_headers = [22 ("Content-type", "text/plain"),23 ]2425 start_response(status, response_headers)2627 for item in output:28 yield item2930 return wsgiref.validate.validator(application)3132Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 33why, one step back33def echo_body(Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
line 34why, one step back34 environ: WSGIEnvironment, start_response: StartResponseTyping: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 35why, one step back35) -> typing.Iterable[bytes]:remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 36–47why, one step back36 status = "200 OK"37 output = environ["wsgi.input"].read()3839 response_headers = [40 ("Content-type", "text/plain"),41 ]4243 start_response(status, response_headers)4445 return [output]4647Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 48why, one step back48def echo_body_with_response_stream(Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
line 49why, one step back49 environ: WSGIEnvironment, start_response: StartResponseTyping: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 50why, one step back50) -> typing.Iterable[bytes]:remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 51–56why, one step back51 status = "200 OK"5253 response_headers = [("Content-Type", "text/plain")]5455 start_response(status, response_headers)56Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 57why, one step back57 def output_generator(f: typing.IO[bytes]) -> typing.Iterator[bytes]:remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 58–66why, one step back58 while True:59 output = f.read(2)60 if not output:61 break62 yield output6364 return output_generator(f=environ["wsgi.input"])6566Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 67why, one step back67def raise_exc(Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
lines 68–70why, one step back68 environ: WSGIEnvironment,69 start_response: StartResponse,70 exc: type[Exception] = ValueError,Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 71why, one step back71) -> typing.Iterable[bytes]:remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 72–82why, one step back72 status = "500 Server Error"73 output = b"Nope!"7475 response_headers = [76 ("Content-type", "text/plain"),77 ]7879 try:80 raise exc()81 except exc:82 exc_info = sys.exc_info()Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 83why, one step back83 start_response(status, response_headers, exc_info)remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 84–160why, one step back8485 return [output]868788def log_to_wsgi_log_buffer(environ, start_response):89 print("test1", file=environ["wsgi.errors"])90 environ["wsgi.errors"].write("test2")91 return echo_body(environ, start_response)929394def test_wsgi():95 client = httpx.Client(app=application_factory([b"Hello, World!"]))96 response = client.get("http://www.example.org/")97 assert response.status_code == 20098 assert response.text == "Hello, World!"99100101def test_wsgi_upload():102 client = httpx.Client(app=echo_body)103 response = client.post("http://www.example.org/", content=b"example")104 assert response.status_code == 200105 assert response.text == "example"106107108def test_wsgi_upload_with_response_stream():109 client = httpx.Client(app=echo_body_with_response_stream)110 response = client.post("http://www.example.org/", content=b"example")111 assert response.status_code == 200112 assert response.text == "example"113114115def test_wsgi_exc():116 client = httpx.Client(app=raise_exc)117 with pytest.raises(ValueError):118 client.get("http://www.example.org/")119120121def test_wsgi_http_error():122 client = httpx.Client(app=partial(raise_exc, exc=RuntimeError))123 with pytest.raises(RuntimeError):124 client.get("http://www.example.org/")125126127def test_wsgi_generator():128 output = [b"", b"", b"Some content", b" and more content"]129 client = httpx.Client(app=application_factory(output))130 response = client.get("http://www.example.org/")131 assert response.status_code == 200132 assert response.text == "Some content and more content"133134135def test_wsgi_generator_empty():136 output = [b"", b"", b"", b""]137 client = httpx.Client(app=application_factory(output))138 response = client.get("http://www.example.org/")139 assert response.status_code == 200140 assert response.text == ""141142143def test_logging():144 buffer = StringIO()145 transport = httpx.WSGITransport(app=log_to_wsgi_log_buffer, wsgi_errors=buffer)146 client = httpx.Client(transport=transport)147 response = client.post("http://www.example.org/", content=b"example")148 assert response.status_code == 200 # no errors149 buffer.seek(0)150 assert buffer.read() == "test1\ntest2"151152153@pytest.mark.parametrize(154 "url, expected_server_port",155 [156 pytest.param("http://www.example.org", "80", id="auto-http"),157 pytest.param("https://www.example.org", "443", id="auto-https"),158 pytest.param("http://www.example.org:8000", "8000", id="explicit-port"),159 ],160)Typing: enable strict_equality (#2480)Martijn Pietersbefore adoption884a69a
line 161why, one step back161def test_wsgi_server_port(url: str, expected_server_port: str) -> None:remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 162–165why, one step back162 """163 SERVER_PORT is populated correctly from the requested URL.164 """165 hello_world_app = application_factory([b"Hello, World!"])Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
line 166why, one step back166 server_port: str | None = Noneremove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d
lines 167–177why, one step back167168 def app(environ, start_response):169 nonlocal server_port170 server_port = environ["SERVER_PORT"]171 return hello_world_app(environ, start_response)172173 client = httpx.Client(app=app)174 response = client.get(url)175 assert response.status_code == 200176 assert response.text == "Hello, World!"177 assert server_port == expected_server_portEnsure all WSGITransport environs have a SERVER_PROTOCOL (#2708)Peter Lazorchakbefore adoptionabb994c
lines 178–194why, one step back178179180def test_wsgi_server_protocol():181 server_protocol = None182183 def app(environ, start_response):184 nonlocal server_protocol185 server_protocol = environ["SERVER_PROTOCOL"]186 start_response("200 OK", [("Content-Type", "text/plain")])187 return [b"success"]188189 with httpx.Client(app=app, base_url="http://testserver") as client:190 response = client.get("/")191192 assert response.status_code == 200193 assert response.text == "success"194 assert server_protocol == "HTTP/1.1"