Skip to content
Myr

Handle default ports in WSGITransport (#1469)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Maybe port is `None` https://www.python.org/dev/peps/pep-3333/#environ-variables > SERVER_NAME, SERVER_PORT > When HTTP_HOST is not set, these variables can be combined to determine a default. See the URL Reconstruction section below for more detail. SERVER_NAME and SERVER_PORT are required strings and must never be empty. * Add unit test * Compute default port

authored by Aber · committed by GitHub · parent d964343 · Browse files · 2 files+30 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

2 files+30 added−0 removed

  • tests:+27 added−0 removed
  • other:+3 added−0 removed

2 files+30 added−0 removed

modified:httpx/_transports/wsgi.py+3 added
72 unmodified lines
73 stream = httpcore.PlainByteStream(content=b"") if stream is None else stream7475 scheme, host, port, full_path = url76 path, _, query = full_path.partition(b"?")77 if port is None:78 port = {b"http": 80, b"https": 443}[scheme]7980 environ = {81 "wsgi.version": (1, 0),82 "wsgi.url_scheme": scheme.decode("ascii"),83 "wsgi.input": io.BytesIO(b"".join(stream)),
49 unmodified lines
modified:tests/test_wsgi.py+27 added
114 unmodified lines
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 == ""119120121@pytest.mark.parametrize(122 "url, expected_server_port",123 [124 pytest.param("http://www.example.org", "80", id="auto-http"),125 pytest.param("https://www.example.org", "443", id="auto-https"),126 pytest.param("http://www.example.org:8000", "8000", id="explicit-port"),127 ],128)129def test_wsgi_server_port(url: str, expected_server_port: int):130 """131 SERVER_PORT is populated correctly from the requested URL.132 """133 hello_world_app = application_factory([b"Hello, World!"])134 server_port: str135136 def app(environ, start_response):137 nonlocal server_port138 server_port = environ["SERVER_PORT"]139 return hello_world_app(environ, start_response)140141 client = httpx.Client(app=app)142 response = client.get(url)143 assert response.status_code == 200144 assert response.text == "Hello, World!"145 assert server_port == expected_server_port