1+ from typing import Callable
2+
13from starlette .applications import Starlette
24from starlette .middleware import Middleware
35from starlette .middleware .cors import CORSMiddleware
6+ from starlette .requests import Request
47from starlette .responses import PlainTextResponse
58from starlette .routing import Route
9+ from starlette .testclient import TestClient
10+ from starlette .types import ASGIApp
11+
12+ TestClientFactory = Callable [[ASGIApp ], TestClient ]
613
714
8- def test_cors_allow_all (test_client_factory ):
9- def homepage (request ):
15+ def test_cors_allow_all (
16+ test_client_factory : TestClientFactory ,
17+ ) -> None :
18+ def homepage (request : Request ) -> PlainTextResponse :
1019 return PlainTextResponse ("Homepage" , status_code = 200 )
1120
1221 app = Starlette (
@@ -64,8 +73,10 @@ def homepage(request):
6473 assert "access-control-allow-origin" not in response .headers
6574
6675
67- def test_cors_allow_all_except_credentials (test_client_factory ):
68- def homepage (request ):
76+ def test_cors_allow_all_except_credentials (
77+ test_client_factory : TestClientFactory ,
78+ ) -> None :
79+ def homepage (request : Request ) -> PlainTextResponse :
6980 return PlainTextResponse ("Homepage" , status_code = 200 )
7081
7182 app = Starlette (
@@ -113,8 +124,10 @@ def homepage(request):
113124 assert "access-control-allow-origin" not in response .headers
114125
115126
116- def test_cors_allow_specific_origin (test_client_factory ):
117- def homepage (request ):
127+ def test_cors_allow_specific_origin (
128+ test_client_factory : TestClientFactory ,
129+ ) -> None :
130+ def homepage (request : Request ) -> PlainTextResponse :
118131 return PlainTextResponse ("Homepage" , status_code = 200 )
119132
120133 app = Starlette (
@@ -160,8 +173,10 @@ def homepage(request):
160173 assert "access-control-allow-origin" not in response .headers
161174
162175
163- def test_cors_disallowed_preflight (test_client_factory ):
164- def homepage (request ):
176+ def test_cors_disallowed_preflight (
177+ test_client_factory : TestClientFactory ,
178+ ) -> None :
179+ def homepage (request : Request ) -> None :
165180 pass # pragma: no cover
166181
167182 app = Starlette (
@@ -200,9 +215,9 @@ def homepage(request):
200215
201216
202217def test_preflight_allows_request_origin_if_origins_wildcard_and_credentials_allowed (
203- test_client_factory ,
204- ):
205- def homepage (request ) :
218+ test_client_factory : TestClientFactory ,
219+ ) -> None :
220+ def homepage (request : Request ) -> None :
206221 return # pragma: no cover
207222
208223 app = Starlette (
@@ -234,8 +249,10 @@ def homepage(request):
234249 assert response .headers ["vary" ] == "Origin"
235250
236251
237- def test_cors_preflight_allow_all_methods (test_client_factory ):
238- def homepage (request ):
252+ def test_cors_preflight_allow_all_methods (
253+ test_client_factory : TestClientFactory ,
254+ ) -> None :
255+ def homepage (request : Request ) -> None :
239256 pass # pragma: no cover
240257
241258 app = Starlette (
@@ -258,8 +275,10 @@ def homepage(request):
258275 assert method in response .headers ["access-control-allow-methods" ]
259276
260277
261- def test_cors_allow_all_methods (test_client_factory ):
262- def homepage (request ):
278+ def test_cors_allow_all_methods (
279+ test_client_factory : TestClientFactory ,
280+ ) -> None :
281+ def homepage (request : Request ) -> PlainTextResponse :
263282 return PlainTextResponse ("Homepage" , status_code = 200 )
264283
265284 app = Starlette (
@@ -287,8 +306,10 @@ def homepage(request):
287306 assert response .status_code == 200
288307
289308
290- def test_cors_allow_origin_regex (test_client_factory ):
291- def homepage (request ):
309+ def test_cors_allow_origin_regex (
310+ test_client_factory : TestClientFactory ,
311+ ) -> None :
312+ def homepage (request : Request ) -> PlainTextResponse :
292313 return PlainTextResponse ("Homepage" , status_code = 200 )
293314
294315 app = Starlette (
@@ -357,8 +378,10 @@ def homepage(request):
357378 assert "access-control-allow-origin" not in response .headers
358379
359380
360- def test_cors_allow_origin_regex_fullmatch (test_client_factory ):
361- def homepage (request ):
381+ def test_cors_allow_origin_regex_fullmatch (
382+ test_client_factory : TestClientFactory ,
383+ ) -> None :
384+ def homepage (request : Request ) -> PlainTextResponse :
362385 return PlainTextResponse ("Homepage" , status_code = 200 )
363386
364387 app = Starlette (
@@ -393,8 +416,10 @@ def homepage(request):
393416 assert "access-control-allow-origin" not in response .headers
394417
395418
396- def test_cors_credentialed_requests_return_specific_origin (test_client_factory ):
397- def homepage (request ):
419+ def test_cors_credentialed_requests_return_specific_origin (
420+ test_client_factory : TestClientFactory ,
421+ ) -> None :
422+ def homepage (request : Request ) -> PlainTextResponse :
398423 return PlainTextResponse ("Homepage" , status_code = 200 )
399424
400425 app = Starlette (
@@ -412,8 +437,10 @@ def homepage(request):
412437 assert "access-control-allow-credentials" not in response .headers
413438
414439
415- def test_cors_vary_header_defaults_to_origin (test_client_factory ):
416- def homepage (request ):
440+ def test_cors_vary_header_defaults_to_origin (
441+ test_client_factory : TestClientFactory ,
442+ ) -> None :
443+ def homepage (request : Request ) -> PlainTextResponse :
417444 return PlainTextResponse ("Homepage" , status_code = 200 )
418445
419446 app = Starlette (
@@ -430,8 +457,10 @@ def homepage(request):
430457 assert response .headers ["vary" ] == "Origin"
431458
432459
433- def test_cors_vary_header_is_not_set_for_non_credentialed_request (test_client_factory ):
434- def homepage (request ):
460+ def test_cors_vary_header_is_not_set_for_non_credentialed_request (
461+ test_client_factory : TestClientFactory ,
462+ ) -> None :
463+ def homepage (request : Request ) -> PlainTextResponse :
435464 return PlainTextResponse (
436465 "Homepage" , status_code = 200 , headers = {"Vary" : "Accept-Encoding" }
437466 )
@@ -447,8 +476,10 @@ def homepage(request):
447476 assert response .headers ["vary" ] == "Accept-Encoding"
448477
449478
450- def test_cors_vary_header_is_properly_set_for_credentialed_request (test_client_factory ):
451- def homepage (request ):
479+ def test_cors_vary_header_is_properly_set_for_credentialed_request (
480+ test_client_factory : TestClientFactory ,
481+ ) -> None :
482+ def homepage (request : Request ) -> PlainTextResponse :
452483 return PlainTextResponse (
453484 "Homepage" , status_code = 200 , headers = {"Vary" : "Accept-Encoding" }
454485 )
@@ -467,9 +498,9 @@ def homepage(request):
467498
468499
469500def test_cors_vary_header_is_properly_set_when_allow_origins_is_not_wildcard (
470- test_client_factory ,
471- ):
472- def homepage (request ) :
501+ test_client_factory : TestClientFactory ,
502+ ) -> None :
503+ def homepage (request : Request ) -> PlainTextResponse :
473504 return PlainTextResponse (
474505 "Homepage" , status_code = 200 , headers = {"Vary" : "Accept-Encoding" }
475506 )
@@ -488,9 +519,9 @@ def homepage(request):
488519
489520
490521def test_cors_allowed_origin_does_not_leak_between_credentialed_requests (
491- test_client_factory ,
492- ):
493- def homepage (request ) :
522+ test_client_factory : TestClientFactory ,
523+ ) -> None :
524+ def homepage (request : Request ) -> PlainTextResponse :
494525 return PlainTextResponse ("Homepage" , status_code = 200 )
495526
496527 app = Starlette (
0 commit comments