Skip to content

Commit 4190dd1

Browse files
committed
chore: add test
1 parent 3b5132e commit 4190dd1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/functional/event_handler/test_api_gateway.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
ServiceError,
3131
UnauthorizedError,
3232
)
33+
from aws_lambda_powertools.event_handler.openapi.exceptions import RequestValidationError
3334
from aws_lambda_powertools.shared import constants
3435
from aws_lambda_powertools.shared.cookies import Cookie
3536
from aws_lambda_powertools.shared.json_encoder import Encoder
@@ -1458,6 +1459,51 @@ def get_lambda() -> Response:
14581459
assert result["body"] == "Foo!"
14591460

14601461

1462+
def test_exception_handler_with_data_validation():
1463+
# GIVEN a resolver with an exception handler defined for RequestValidationError
1464+
app = ApiGatewayResolver(enable_validation=True)
1465+
1466+
@app.exception_handler(RequestValidationError)
1467+
def handle_validation_error(ex: RequestValidationError):
1468+
print(f"request path is '{app.current_event.path}'")
1469+
return Response(
1470+
status_code=422,
1471+
content_type=content_types.TEXT_PLAIN,
1472+
body=f"Invalid data. Number of errors: {len(ex.errors())}",
1473+
)
1474+
1475+
@app.get("/my/path")
1476+
def get_lambda(param: int):
1477+
...
1478+
1479+
# WHEN calling the event handler
1480+
# AND a RequestValidationError is raised
1481+
result = app(LOAD_GW_EVENT, {})
1482+
1483+
# THEN call the exception_handler
1484+
assert result["statusCode"] == 422
1485+
assert result["multiValueHeaders"]["Content-Type"] == [content_types.TEXT_PLAIN]
1486+
assert result["body"] == "Invalid data. Number of errors: 1"
1487+
1488+
1489+
def test_data_validation_error():
1490+
# GIVEN a resolver without an exception handler
1491+
app = ApiGatewayResolver(enable_validation=True)
1492+
1493+
@app.get("/my/path")
1494+
def get_lambda(param: int):
1495+
...
1496+
1497+
# WHEN calling the event handler
1498+
# AND a RequestValidationError is raised
1499+
result = app(LOAD_GW_EVENT, {})
1500+
1501+
# THEN call the exception_handler
1502+
assert result["statusCode"] == 422
1503+
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]
1504+
assert "missing" in result["body"]
1505+
1506+
14611507
def test_exception_handler_service_error():
14621508
# GIVEN
14631509
app = ApiGatewayResolver()

0 commit comments

Comments
 (0)