diff --git a/ariadne_lambda/schema.py b/ariadne_lambda/schema.py index a917b4b..b7d9724 100644 --- a/ariadne_lambda/schema.py +++ b/ariadne_lambda/schema.py @@ -29,7 +29,7 @@ def create_from_event(cls, event: dict[str, Any]) -> "Request": "body": "", "is_base64_encoded": event["isBase64Encoded"], "headers": lowered_key_headers, - "params": event["queryStringParameters"], + "params": event.get("queryStringParameters", {}), } if http_context := event["requestContext"].get("http"): @@ -43,12 +43,9 @@ def create_from_event(cls, event: dict[str, Any]) -> "Request": request_data["path"] = event["path"] request_data["method"] = event["httpMethod"].upper() - if body := event["body"]: + if body := event.get("body"): request_data["body"] = body - if not request_data["params"]: - request_data["params"] = {} - return cls(**request_data) diff --git a/tests/conftest.py b/tests/conftest.py index ca469f0..e54aa36 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,6 +22,11 @@ def api_gateway_v2_event_payload(): return load_data_file("api_gateway_v2_event.json") +@pytest.fixture +def api_gateway_v2_lambda_url_event_payload(): + return load_data_file("api_gateway_v2_lambda_url_event.json") + + @pytest.fixture def lambda_context(): return MagicMock() diff --git a/tests/data/api_gateway_v2_lambda_url_event.json b/tests/data/api_gateway_v2_lambda_url_event.json new file mode 100644 index 0000000..6b5c03d --- /dev/null +++ b/tests/data/api_gateway_v2_lambda_url_event.json @@ -0,0 +1,24 @@ +{ + "version": "2.0", + "routeKey": "GET /my-resource", + "rawPath": "/", + "rawQueryString": "", + "headers": { + "host": "api.example.com", + "user-agent": "Mozilla/5.0", + "accept": "application/json" + }, + "requestContext": { + "accountId": "anonymous", + "http": { + "method": "GET", + "path": "/my-resource", + "protocol": "HTTP/1.1", + "sourceIp": "109.243.69.50" + }, + "requestId": "08752adc-1bb9-4598-b314-0d147401a1eb", + "routeKey": "GET /my-resource", + "stage": "prod" + }, + "isBase64Encoded": false +} diff --git a/tests/test_schema.py b/tests/test_schema.py index 181fbe9..8f463e0 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -24,6 +24,16 @@ def test_api_v2_event(api_gateway_v2_event_payload): assert request.params == api_gateway_v2_event_payload["queryStringParameters"] +def test_api_v2_event_lambda_url(api_gateway_v2_lambda_url_event_payload): + request = Request.create_from_event(api_gateway_v2_lambda_url_event_payload) + assert request.method == api_gateway_v2_lambda_url_event_payload["requestContext"]["http"]["method"] + assert request.path == api_gateway_v2_lambda_url_event_payload["requestContext"]["http"]["path"] + assert request.body == "" + assert request.is_base64_encoded is False + assert request.headers == api_gateway_v2_lambda_url_event_payload["headers"] + assert request.params == {} + + def test_response_initialization(): # When response = Response(status_code=200, body="OK", headers={"Content-Type": "application/json"})