Skip to content
Myr

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 file

Take 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 back
1from __future__ import annotations
2

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

line 3why, one step back
3import sys

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 4why, one step back
4import typing

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

lines 5–12why, one step back
5import wsgiref.validate
6from functools import partial7from io import StringIO89import pytest1011import httpx12

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

lines 13–14why, one step back
13if typing.TYPE_CHECKING: # pragma: no cover
14 from _typeshed.wsgi import StartResponse, WSGIApplication, WSGIEnvironment

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

line 15why, one step back
15

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 16why, one step back
16

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

line 17why, one step back
17def 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 back
18 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)3132

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 33why, one step back
33def echo_body(

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

line 34why, one step back
34 environ: WSGIEnvironment, start_response: StartResponse

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 35why, one step back
35) -> typing.Iterable[bytes]:

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

lines 36–47why, one step back
36 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]4647

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 48why, one step back
48def echo_body_with_response_stream(

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

line 49why, one step back
49 environ: WSGIEnvironment, start_response: StartResponse

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 50why, one step back
50) -> typing.Iterable[bytes]:

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

lines 51–56why, one step back
51 status = "200 OK"
5253 response_headers = [("Content-Type", "text/plain")]5455 start_response(status, response_headers)56

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 57why, one step back
57 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 back
58 while True:
59 output = f.read(2)60 if not output:61 break62 yield output6364 return output_generator(f=environ["wsgi.input"])6566

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 67why, one step back
67def raise_exc(

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

lines 68–70why, one step back
68 environ: WSGIEnvironment,
69 start_response: StartResponse,70 exc: type[Exception] = ValueError,

Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c

line 71why, one step back
71) -> typing.Iterable[bytes]:

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

lines 72–82why, one step back
72 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 back
83 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 back
84
85 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 back
161def 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 back
162 """
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 back
166 server_port: str | None = None

remove `chunk_size` from api docs for `iter_lines` variants (#2464)Simon Kbefore adoptiondeb904d

lines 167–177why, one step back
167
168 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_port

Ensure all WSGITransport environs have a SERVER_PROTOCOL (#2708)Peter Lazorchakbefore adoptionabb994c

lines 178–194why, one step back
178
179180def 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"