|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import asyncio |
| 4 | +import json |
| 5 | + |
3 | 6 | from aws_lambda_powertools.event_handler import ( |
4 | 7 | Response, |
5 | 8 | VPCLatticeV2Resolver, |
@@ -50,6 +53,61 @@ def foo(): |
50 | 53 | assert result["body"] == json_dump(expected) |
51 | 54 |
|
52 | 55 |
|
| 56 | +def test_vpclatticev2_event_with_query_string(): |
| 57 | + # GIVEN an event captured from a VPC Lattice V2 target group, whose path carries the query string |
| 58 | + app = VPCLatticeV2Resolver() |
| 59 | + |
| 60 | + @app.post("/echo") |
| 61 | + def echo(): |
| 62 | + return {"query": app.current_event.query_string_parameters, "body": app.current_event.json_body} |
| 63 | + |
| 64 | + # WHEN calling the event handler |
| 65 | + result = app(load_event("vpcLatticeV2EventWithQueryString.json"), {}) |
| 66 | + |
| 67 | + # THEN the route matches the path without the query string |
| 68 | + assert result["statusCode"] == 200 |
| 69 | + assert json.loads(result["body"]) == { |
| 70 | + "query": {"QS1": "value1,value2", "single": "one"}, |
| 71 | + "body": {"hello": "world"}, |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +def test_vpclatticev2_dynamic_route_with_query_string(): |
| 76 | + # GIVEN a VPC Lattice V2 event for a dynamic route, with the query string in path |
| 77 | + app = VPCLatticeV2Resolver() |
| 78 | + |
| 79 | + @app.get("/orders/<order_id>") |
| 80 | + def get_order(order_id: str): |
| 81 | + return {"order_id": order_id, "expand": app.current_event.query_string_parameters["expand"]} |
| 82 | + |
| 83 | + event = load_event("vpcLatticeV2Event.json") |
| 84 | + event["path"] = "/orders/42?expand=items" |
| 85 | + event["queryStringParameters"] = {"expand": ["items"]} |
| 86 | + |
| 87 | + # WHEN calling the event handler |
| 88 | + result = app(event, {}) |
| 89 | + |
| 90 | + # THEN the path parameter does not include the query string |
| 91 | + assert result["statusCode"] == 200 |
| 92 | + assert json.loads(result["body"]) == {"order_id": "42", "expand": "items"} |
| 93 | + |
| 94 | + |
| 95 | +def test_vpclatticev2_resolve_async_with_query_string(): |
| 96 | + # GIVEN an async handler for a VPC Lattice V2 event whose path carries the query string |
| 97 | + app = VPCLatticeV2Resolver() |
| 98 | + |
| 99 | + @app.post("/echo") |
| 100 | + async def echo(): |
| 101 | + return {"single": app.current_event.query_string_parameters["single"]} |
| 102 | + |
| 103 | + # WHEN resolving the event asynchronously |
| 104 | + result = asyncio.run(app.resolve_async(load_event("vpcLatticeV2EventWithQueryString.json"), {})) |
| 105 | + |
| 106 | + # THEN the route matches the path without the query string |
| 107 | + assert result["statusCode"] == 200 |
| 108 | + assert json.loads(result["body"]) == {"single": "one"} |
| 109 | + |
| 110 | + |
53 | 111 | def test_cors_preflight_body_is_empty_not_null(): |
54 | 112 | # GIVEN CORS is configured |
55 | 113 | app = VPCLatticeV2Resolver(cors=CORSConfig()) |
|
0 commit comments