@@ -324,7 +324,7 @@ def handler(event, context):
324324def test_cors ():
325325 # GIVEN a function with cors=True
326326 # AND http method set to GET
327- app = ApiGatewayResolver ()
327+ app = ApiGatewayResolver (cors = CORSConfig ( "https://aws.amazon.com" , allow_credentials = True ) )
328328
329329 @app .get ("/my/path" , cors = True )
330330 def with_cors () -> Response :
@@ -345,7 +345,7 @@ def handler(event, context):
345345 headers = result ["multiValueHeaders" ]
346346 assert headers ["Content-Type" ] == [content_types .TEXT_HTML ]
347347 assert headers ["Access-Control-Allow-Origin" ] == ["https://aws.amazon.com" ]
348- assert "Access-Control-Allow-Credentials" not in headers
348+ assert "Access-Control-Allow-Credentials" in headers
349349 assert headers ["Access-Control-Allow-Headers" ] == ["," .join (sorted (CORSConfig ._REQUIRED_HEADERS ))]
350350
351351 # THEN for routes without cors flag return no cors headers
@@ -354,7 +354,7 @@ def handler(event, context):
354354 assert "Access-Control-Allow-Origin" not in result ["multiValueHeaders" ]
355355
356356
357- def test_cors_no_origin ():
357+ def test_cors_no_request_origin ():
358358 # GIVEN a function with cors=True
359359 # AND http method set to GET
360360 app = ApiGatewayResolver ()
@@ -366,8 +366,41 @@ def with_cors() -> Response:
366366 def handler (event , context ):
367367 return app .resolve (event , context )
368368
369- # remove origin header from request
370- del LOAD_GW_EVENT ["multiValueHeaders" ]["Origin" ]
369+ event = LOAD_GW_EVENT .copy ()
370+ del event ["headers" ]["Origin" ]
371+ del event ["multiValueHeaders" ]["Origin" ]
372+
373+ # WHEN calling the event handler
374+ result = handler (LOAD_GW_EVENT , None )
375+
376+ # THEN the headers should include cors headers
377+ assert "multiValueHeaders" in result
378+ headers = result ["multiValueHeaders" ]
379+ assert headers ["Content-Type" ] == [content_types .TEXT_HTML ]
380+ assert "Access-Control-Allow-Credentials" not in headers
381+ assert "Access-Control-Allow-Origin" not in result ["multiValueHeaders" ]
382+
383+
384+ def test_cors_allow_all_request_origins ():
385+ # GIVEN a function with cors=True
386+ # AND http method set to GET
387+ app = ApiGatewayResolver (
388+ cors = CORSConfig (
389+ allow_origin = "*" ,
390+ allow_credentials = True ,
391+ ),
392+ )
393+
394+ @app .get ("/my/path" , cors = True )
395+ def with_cors () -> Response :
396+ return Response (200 , content_types .TEXT_HTML , "test" )
397+
398+ @app .get ("/without-cors" )
399+ def without_cors () -> Response :
400+ return Response (200 , content_types .TEXT_HTML , "test" )
401+
402+ def handler (event , context ):
403+ return app .resolve (event , context )
371404
372405 # WHEN calling the event handler
373406 result = handler (LOAD_GW_EVENT , None )
@@ -380,6 +413,11 @@ def handler(event, context):
380413 assert "Access-Control-Allow-Credentials" not in headers
381414 assert headers ["Access-Control-Allow-Headers" ] == ["," .join (sorted (CORSConfig ._REQUIRED_HEADERS ))]
382415
416+ # THEN for routes without cors flag return no cors headers
417+ mock_event = {"path" : "/my/request" , "httpMethod" : "GET" }
418+ result = handler (mock_event , None )
419+ assert "Access-Control-Allow-Origin" not in result ["multiValueHeaders" ]
420+
383421
384422def test_cors_preflight_body_is_empty_not_null ():
385423 # GIVEN CORS is configured
0 commit comments