]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add type hints to `test_wsgi.py` (#2468)
authorScirlat Danut <danut.scirlat@gmail.com>
Sun, 4 Feb 2024 15:11:13 +0000 (17:11 +0200)
committerGitHub <noreply@github.com>
Sun, 4 Feb 2024 15:11:13 +0000 (08:11 -0700)
Co-authored-by: Scirlat Danut <scirlatdanut@scirlats-mini.lan>
tests/middleware/test_wsgi.py

index 316cb191f2bb3547123968162553aad1dc5a1866..69842d3ad98f2f90c1c8f9a08ff1b86be5c93e17 100644 (file)
@@ -1,12 +1,22 @@
 import sys
+from typing import Any, Callable, Dict, Iterable
 
 import pytest
 
 from starlette._utils import collapse_excgroups
 from starlette.middleware.wsgi import WSGIMiddleware, build_environ
+from starlette.testclient import TestClient
 
+WSGIResponse = Iterable[bytes]
+TestClientFactory = Callable[..., TestClient]
+StartResponse = Callable[..., Any]
+Environment = Dict[str, Any]
 
-def hello_world(environ, start_response):
+
+def hello_world(
+    environ: Environment,
+    start_response: StartResponse,
+) -> WSGIResponse:
     status = "200 OK"
     output = b"Hello World!\n"
     headers = [
@@ -17,7 +27,10 @@ def hello_world(environ, start_response):
     return [output]
 
 
-def echo_body(environ, start_response):
+def echo_body(
+    environ: Environment,
+    start_response: StartResponse,
+) -> WSGIResponse:
     status = "200 OK"
     output = environ["wsgi.input"].read()
     headers = [
@@ -28,11 +41,17 @@ def echo_body(environ, start_response):
     return [output]
 
 
-def raise_exception(environ, start_response):
+def raise_exception(
+    environ: Environment,
+    start_response: StartResponse,
+) -> WSGIResponse:
     raise RuntimeError("Something went wrong")
 
 
-def return_exc_info(environ, start_response):
+def return_exc_info(
+    environ: Environment,
+    start_response: StartResponse,
+) -> WSGIResponse:
     try:
         raise RuntimeError("Something went wrong")
     except RuntimeError:
@@ -46,7 +65,7 @@ def return_exc_info(environ, start_response):
         return [output]
 
 
-def test_wsgi_get(test_client_factory):
+def test_wsgi_get(test_client_factory: TestClientFactory) -> None:
     app = WSGIMiddleware(hello_world)
     client = test_client_factory(app)
     response = client.get("/")
@@ -54,7 +73,7 @@ def test_wsgi_get(test_client_factory):
     assert response.text == "Hello World!\n"
 
 
-def test_wsgi_post(test_client_factory):
+def test_wsgi_post(test_client_factory: TestClientFactory) -> None:
     app = WSGIMiddleware(echo_body)
     client = test_client_factory(app)
     response = client.post("/", json={"example": 123})
@@ -62,7 +81,7 @@ def test_wsgi_post(test_client_factory):
     assert response.text == '{"example": 123}'
 
 
-def test_wsgi_exception(test_client_factory):
+def test_wsgi_exception(test_client_factory: TestClientFactory) -> None:
     # Note that we're testing the WSGI app directly here.
     # The HTTP protocol implementations would catch this error and return 500.
     app = WSGIMiddleware(raise_exception)
@@ -71,7 +90,7 @@ def test_wsgi_exception(test_client_factory):
         client.get("/")
 
 
-def test_wsgi_exc_info(test_client_factory):
+def test_wsgi_exc_info(test_client_factory: TestClientFactory) -> None:
     # Note that we're testing the WSGI app directly here.
     # The HTTP protocol implementations would catch this error and return 500.
     app = WSGIMiddleware(return_exc_info)
@@ -86,7 +105,7 @@ def test_wsgi_exc_info(test_client_factory):
     assert response.text == "Internal Server Error"
 
 
-def test_build_environ():
+def test_build_environ() -> None:
     scope = {
         "type": "http",
         "http_version": "1.1",