Skip to content

Commit

Permalink
Adjust code to work with Lambda URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kwaidan00 committed Dec 19, 2024
1 parent 3528234 commit beb64ae
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
7 changes: 2 additions & 5 deletions ariadne_lambda/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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)


Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
24 changes: 24 additions & 0 deletions tests/data/api_gateway_v2_lambda_url_event.json
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 10 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit beb64ae

Please sign in to comment.