Skip to content

docs(apigateway): add all resolvers in testing your code section for accuracy #1688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 9, 2022
62 changes: 53 additions & 9 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,19 +562,63 @@ your development, building, deployment tooling need to accommodate the distinct

## Testing your code

You can test your routes by passing a proxy event request where `path` and `httpMethod`.
You can test your routes by passing a proxy event request with required params.

=== "assert_http_response.py"
=== "API Gateway REST API"

```python hl_lines="21-24"
--8<-- "examples/event_handler_rest/src/assert_http_response.py"
```
=== "assert_rest_api_resolver_response.py"

=== "assert_http_response_module.py"
```python hl_lines="21-24"
--8<-- "examples/event_handler_rest/src/assert_rest_api_resolver_response.py"
```

```python
--8<-- "examples/event_handler_rest/src/assert_http_response_module.py"
```
=== "assert_rest_api_response_module.py"

```python
--8<-- "examples/event_handler_rest/src/assert_rest_api_response_module.py"
```

=== "API Gateway HTTP API"

=== "assert_http_api_resolver_response.py"

```python hl_lines="21-29"
--8<-- "examples/event_handler_rest/src/assert_http_api_resolver_response.py"
```

=== "assert_http_api_response_module.py"

```python
--8<-- "examples/event_handler_rest/src/assert_http_api_response_module.py"
```

=== "Application Load Balancer"

=== "assert_alb_api_resolver_response.py"

```python hl_lines="21-24"
--8<-- "examples/event_handler_rest/src/assert_alb_api_resolver_response.py"
```

=== "assert_alb_api_response_module.py"

```python
--8<-- "examples/event_handler_rest/src/assert_alb_api_response_module.py"
```

=== "Lambda Function URL"

=== "assert_function_url_api_resolver_response.py"

```python hl_lines="21-29"
--8<-- "examples/event_handler_rest/src/assert_function_url_api_resolver_response.py"
```

=== "assert_function_url_api_response_module.py"

```python
--8<-- "examples/event_handler_rest/src/assert_function_url_api_response_module.py"
```

## FAQ

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from dataclasses import dataclass

import assert_alb_api_response_module
import pytest


@pytest.fixture
def lambda_context():
@dataclass
class LambdaContext:
function_name: str = "test"
memory_limit_in_mb: int = 128
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"

return LambdaContext()


def test_lambda_handler(lambda_context):
minimal_event = {
"path": "/todos",
"httpMethod": "GET",
"headers": {"x-amzn-trace-id": "b25827e5-0e30-4d52-85a8-4df449ee4c5a"},
}
# Example of Application Load Balancer request event:
# https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html

ret = assert_alb_api_response_module.lambda_handler(minimal_event, lambda_context)
assert ret["statusCode"] == 200
assert ret["body"] != ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import ALBResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = ALBResolver()


@app.get("/todos")
@tracer.capture_method
def get_todos():
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()

return {"todos": todos.json()[:10]}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPLICATION_LOAD_BALANCER)
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dataclasses import dataclass

import assert_function_url_api_response_module
import pytest


@pytest.fixture
def lambda_context():
@dataclass
class LambdaContext:
function_name: str = "test"
memory_limit_in_mb: int = 128
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"

return LambdaContext()


def test_lambda_handler(lambda_context):
minimal_event = {
"rawPath": "/todos",
"requestContext": {
"requestContext": {"requestId": "227b78aa-779d-47d4-a48e-ce62120393b8"}, # correlation ID
"http": {
"method": "GET",
},
"stage": "$default",
},
}
# Example of Lambda Function URL request event:
# https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads

ret = assert_function_url_api_response_module.lambda_handler(minimal_event, lambda_context)
assert ret["statusCode"] == 200
assert ret["body"] != ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import LambdaFunctionUrlResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = LambdaFunctionUrlResolver()


@app.get("/todos")
@tracer.capture_method
def get_todos():
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()

return {"todos": todos.json()[:10]}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.LAMBDA_FUNCTION_URL)
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from dataclasses import dataclass

import assert_http_api_response_module
import pytest


@pytest.fixture
def lambda_context():
@dataclass
class LambdaContext:
function_name: str = "test"
memory_limit_in_mb: int = 128
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"

return LambdaContext()


def test_lambda_handler(lambda_context):
minimal_event = {
"rawPath": "/todos",
"requestContext": {
"requestContext": {"requestId": "227b78aa-779d-47d4-a48e-ce62120393b8"}, # correlation ID
"http": {
"method": "GET",
},
"stage": "$default",
},
}
# Example of API Gateway HTTP API request event:
# https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

ret = assert_http_api_response_module.lambda_handler(minimal_event, lambda_context)
assert ret["statusCode"] == 200
assert ret["body"] != ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayHttpResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = APIGatewayHttpResolver()


@app.get("/todos")
@tracer.capture_method
def get_todos():
todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
todos.raise_for_status()

return {"todos": todos.json()[:10]}


# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_HTTP)
@tracer.capture_lambda_handler
def lambda_handler(event: dict, context: LambdaContext) -> dict:
return app.resolve(event, context)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass

import assert_http_response_module
import assert_rest_api_resolver_response
import pytest


Expand All @@ -22,7 +22,9 @@ def test_lambda_handler(lambda_context):
"httpMethod": "GET",
"requestContext": {"requestId": "227b78aa-779d-47d4-a48e-ce62120393b8"}, # correlation ID
}

ret = assert_http_response_module.lambda_handler(minimal_event, lambda_context)
# Example of API Gateway REST API request event:
# https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html#apigateway-example-event

ret = assert_rest_api_resolver_response.lambda_handler(minimal_event, lambda_context)
assert ret["statusCode"] == 200
assert ret["body"] != ""