]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add type hints to `test_gzip.py` (#2464)
authorScirlat Danut <danut.scirlat@gmail.com>
Mon, 5 Feb 2024 10:46:34 +0000 (12:46 +0200)
committerGitHub <noreply@github.com>
Mon, 5 Feb 2024 10:46:34 +0000 (10:46 +0000)
* added type annotations to test_gzip.py

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Scirlat Danut <scirlatdanut@scirlats-mini.lan>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
tests/middleware/test_gzip.py

index b6ec1f3fa28a99cada46ac30e76bda7fa21291cb..5bfecadb72185b0d39edbcdd0bcd3323c8bc4dbf 100644 (file)
@@ -1,12 +1,19 @@
+from typing import Callable
+
 from starlette.applications import Starlette
 from starlette.middleware import Middleware
 from starlette.middleware.gzip import GZipMiddleware
-from starlette.responses import PlainTextResponse, StreamingResponse
+from starlette.requests import Request
+from starlette.responses import ContentStream, PlainTextResponse, StreamingResponse
 from starlette.routing import Route
+from starlette.testclient import TestClient
+from starlette.types import ASGIApp
+
+TestClientFactory = Callable[[ASGIApp], TestClient]
 
 
-def test_gzip_responses(test_client_factory):
-    def homepage(request):
+def test_gzip_responses(test_client_factory: TestClientFactory) -> None:
+    def homepage(request: Request) -> PlainTextResponse:
         return PlainTextResponse("x" * 4000, status_code=200)
 
     app = Starlette(
@@ -22,8 +29,8 @@ def test_gzip_responses(test_client_factory):
     assert int(response.headers["Content-Length"]) < 4000
 
 
-def test_gzip_not_in_accept_encoding(test_client_factory):
-    def homepage(request):
+def test_gzip_not_in_accept_encoding(test_client_factory: TestClientFactory) -> None:
+    def homepage(request: Request) -> PlainTextResponse:
         return PlainTextResponse("x" * 4000, status_code=200)
 
     app = Starlette(
@@ -39,8 +46,10 @@ def test_gzip_not_in_accept_encoding(test_client_factory):
     assert int(response.headers["Content-Length"]) == 4000
 
 
-def test_gzip_ignored_for_small_responses(test_client_factory):
-    def homepage(request):
+def test_gzip_ignored_for_small_responses(
+    test_client_factory: TestClientFactory,
+) -> None:
+    def homepage(request: Request) -> PlainTextResponse:
         return PlainTextResponse("OK", status_code=200)
 
     app = Starlette(
@@ -56,9 +65,9 @@ def test_gzip_ignored_for_small_responses(test_client_factory):
     assert int(response.headers["Content-Length"]) == 2
 
 
-def test_gzip_streaming_response(test_client_factory):
-    def homepage(request):
-        async def generator(bytes, count):
+def test_gzip_streaming_response(test_client_factory: TestClientFactory) -> None:
+    def homepage(request: Request) -> StreamingResponse:
+        async def generator(bytes: bytes, count: int) -> ContentStream:
             for index in range(count):
                 yield bytes
 
@@ -78,9 +87,11 @@ def test_gzip_streaming_response(test_client_factory):
     assert "Content-Length" not in response.headers
 
 
-def test_gzip_ignored_for_responses_with_encoding_set(test_client_factory):
-    def homepage(request):
-        async def generator(bytes, count):
+def test_gzip_ignored_for_responses_with_encoding_set(
+    test_client_factory: TestClientFactory,
+) -> None:
+    def homepage(request: Request) -> StreamingResponse:
+        async def generator(bytes: bytes, count: int) -> ContentStream:
             for index in range(count):
                 yield bytes