|
| 1 | +from aws_lambda_powertools.event_handler import ( |
| 2 | + Response, |
| 3 | + VPCLatticeResolver, |
| 4 | + content_types, |
| 5 | +) |
| 6 | +from aws_lambda_powertools.event_handler.api_gateway import CORSConfig |
| 7 | +from aws_lambda_powertools.utilities.data_classes import VPCLatticeEvent |
| 8 | +from tests.functional.utils import load_event |
| 9 | + |
| 10 | + |
| 11 | +def test_vpclattice_event(): |
| 12 | + # GIVEN a VPC Lattice event |
| 13 | + app = VPCLatticeResolver() |
| 14 | + |
| 15 | + @app.get("/testpath") |
| 16 | + def foo(): |
| 17 | + assert isinstance(app.current_event, VPCLatticeEvent) |
| 18 | + assert app.lambda_context == {} |
| 19 | + return Response(200, content_types.TEXT_HTML, "foo") |
| 20 | + |
| 21 | + # WHEN calling the event handler |
| 22 | + result = app(load_event("vpcLatticeEvent.json"), {}) |
| 23 | + |
| 24 | + # THEN process event correctly |
| 25 | + # AND set the current_event type as VPCLatticeEvent |
| 26 | + assert result["statusCode"] == 200 |
| 27 | + assert result["headers"]["Content-Type"] == content_types.TEXT_HTML |
| 28 | + assert result["body"] == "foo" |
| 29 | + |
| 30 | + |
| 31 | +def test_vpclattice_event_path_trailing_slash(json_dump): |
| 32 | + # GIVEN a VPC Lattice event |
| 33 | + app = VPCLatticeResolver() |
| 34 | + |
| 35 | + @app.get("/testpath") |
| 36 | + def foo(): |
| 37 | + assert isinstance(app.current_event, VPCLatticeEvent) |
| 38 | + assert app.lambda_context == {} |
| 39 | + return Response(200, content_types.TEXT_HTML, "foo") |
| 40 | + |
| 41 | + # WHEN calling the event handler using path with trailing "/" |
| 42 | + result = app(load_event("vpcLatticeEventPathTrailingSlash.json"), {}) |
| 43 | + |
| 44 | + # THEN |
| 45 | + assert result["statusCode"] == 404 |
| 46 | + assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON |
| 47 | + expected = {"statusCode": 404, "message": "Not found"} |
| 48 | + assert result["body"] == json_dump(expected) |
| 49 | + |
| 50 | + |
| 51 | +def test_cors_preflight_body_is_empty_not_null(): |
| 52 | + # GIVEN CORS is configured |
| 53 | + app = VPCLatticeResolver(cors=CORSConfig()) |
| 54 | + |
| 55 | + event = {"raw_path": "/my/request", "method": "OPTIONS", "headers": {}} |
| 56 | + |
| 57 | + # WHEN calling the event handler |
| 58 | + result = app(event, {}) |
| 59 | + |
| 60 | + # THEN there body should be empty strings |
| 61 | + assert result["body"] == "" |
| 62 | + |
| 63 | + |
| 64 | +def test_vpclattice_url_no_matches(): |
| 65 | + # GIVEN a VPC Lattice event |
| 66 | + app = VPCLatticeResolver() |
| 67 | + |
| 68 | + @app.post("/no_match") |
| 69 | + def foo(): |
| 70 | + raise RuntimeError() |
| 71 | + |
| 72 | + # WHEN calling the event handler |
| 73 | + result = app(load_event("vpcLatticeEvent.json"), {}) |
| 74 | + |
| 75 | + # THEN process event correctly |
| 76 | + # AND return 404 because the event doesn't match any known route |
| 77 | + assert result["statusCode"] == 404 |
0 commit comments