From beb64ae098b985005f8cd81761e86714ab668ab1 Mon Sep 17 00:00:00 2001 From: Aleksander Spyra Date: Thu, 19 Dec 2024 13:50:33 +0100 Subject: [PATCH 1/2] Adjust code to work with Lambda URLs --- ariadne_lambda/schema.py | 7 ++---- tests/conftest.py | 5 ++++ .../data/api_gateway_v2_lambda_url_event.json | 24 +++++++++++++++++++ tests/test_schema.py | 10 ++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 tests/data/api_gateway_v2_lambda_url_event.json 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"}) From 423946e5ea936a76facd20c4c0e737de75eca2fc Mon Sep 17 00:00:00 2001 From: Aleksander Spyra Date: Thu, 19 Dec 2024 14:11:17 +0100 Subject: [PATCH 2/2] Run ruff formatter --- ariadne_lambda/base.py | 2 +- tests/test_schema.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ariadne_lambda/base.py b/ariadne_lambda/base.py index 76ebd23..a265768 100644 --- a/ariadne_lambda/base.py +++ b/ariadne_lambda/base.py @@ -52,4 +52,4 @@ async def get_context_for_request( context = await context return context - return self.context_value or {"request": request} \ No newline at end of file + return self.context_value or {"request": request} diff --git a/tests/test_schema.py b/tests/test_schema.py index 8f463e0..bf242cd 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -26,8 +26,13 @@ def test_api_v2_event(api_gateway_v2_event_payload): 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.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"]