Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ariadne_lambda/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ async def get_context_for_request(
context = await context
return context

return self.context_value or {"request": request}
return self.context_value or {"request": request}
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
}
15 changes: 15 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ 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