@@ -324,7 +324,7 @@ def handler(event, context):
324
324
def test_cors ():
325
325
# GIVEN a function with cors=True
326
326
# AND http method set to GET
327
- app = ApiGatewayResolver ()
327
+ app = ApiGatewayResolver (cors = CORSConfig ( "https://aws.amazon.com" , allow_credentials = True ) )
328
328
329
329
@app .get ("/my/path" , cors = True )
330
330
def with_cors () -> Response :
@@ -345,7 +345,7 @@ def handler(event, context):
345
345
headers = result ["multiValueHeaders" ]
346
346
assert headers ["Content-Type" ] == [content_types .TEXT_HTML ]
347
347
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
349
349
assert headers ["Access-Control-Allow-Headers" ] == ["," .join (sorted (CORSConfig ._REQUIRED_HEADERS ))]
350
350
351
351
# THEN for routes without cors flag return no cors headers
@@ -354,7 +354,7 @@ def handler(event, context):
354
354
assert "Access-Control-Allow-Origin" not in result ["multiValueHeaders" ]
355
355
356
356
357
- def test_cors_no_origin ():
357
+ def test_cors_no_request_origin ():
358
358
# GIVEN a function with cors=True
359
359
# AND http method set to GET
360
360
app = ApiGatewayResolver ()
@@ -366,8 +366,41 @@ def with_cors() -> Response:
366
366
def handler (event , context ):
367
367
return app .resolve (event , context )
368
368
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 )
371
404
372
405
# WHEN calling the event handler
373
406
result = handler (LOAD_GW_EVENT , None )
@@ -380,6 +413,11 @@ def handler(event, context):
380
413
assert "Access-Control-Allow-Credentials" not in headers
381
414
assert headers ["Access-Control-Allow-Headers" ] == ["," .join (sorted (CORSConfig ._REQUIRED_HEADERS ))]
382
415
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
+
383
421
384
422
def test_cors_preflight_body_is_empty_not_null ():
385
423
# GIVEN CORS is configured
0 commit comments