Skip to content

Commit 6a0a708

Browse files
committed
docs: add testing section
1 parent a164291 commit 6a0a708

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

docs/core/event_handler/api_gateway.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,60 @@ Like `compress` feature, the client must send the `Accept` header with the corre
651651
}
652652
```
653653

654-
### Testing your code
654+
## Testing your code
655+
656+
You can test your routes by passing a proxy event request where `path` and `httpMethod`.
657+
658+
=== "test_app.py"
659+
660+
```python hl_lines="18-24"
661+
from dataclasses import dataclass
662+
663+
import pytest
664+
import app
665+
666+
@pytest.fixture
667+
def lambda_context():
668+
@dataclass
669+
class LambdaContext:
670+
function_name: str = "test"
671+
memory_limit_in_mb: int = 128
672+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
673+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
674+
675+
return LambdaContext()
676+
677+
def test_lambda_handler(lambda_context):
678+
minimal_event = {
679+
"path": "/hello",
680+
"httpMethod": "GET"
681+
"requestContext": { # correlation ID
682+
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef"
683+
}
684+
}
685+
686+
app.lambda_handler(minimal_event, lambda_context)
687+
```
688+
689+
=== "app.py"
690+
691+
```python
692+
from aws_lambda_powertools import Logger
693+
from aws_lambda_powertools.logging import correlation_paths
694+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
695+
696+
logger = Logger()
697+
app = ApiGatewayResolver() # by default API Gateway REST API (v1)
698+
699+
@app.get("/hello")
700+
def get_hello_universe():
701+
return {"message": "hello universe"}
702+
703+
# You can continue to use other utilities just as before
704+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
705+
def lambda_handler(event, context):
706+
return app.resolve(event, context)
707+
```
655708

656709

657710
## FAQ

0 commit comments

Comments
 (0)