|
| 1 | +from pydantic import BaseModel |
| 2 | + |
| 3 | +from aws_lambda_powertools.event_handler import content_types |
| 4 | +from aws_lambda_powertools.event_handler.api_gateway import ( |
| 5 | + ApiGatewayResolver, |
| 6 | + Response, |
| 7 | +) |
| 8 | +from aws_lambda_powertools.event_handler.openapi.exceptions import RequestValidationError |
| 9 | +from tests.functional.utils import load_event |
| 10 | + |
| 11 | +LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json") |
| 12 | + |
| 13 | + |
| 14 | +def test_exception_handler_with_data_validation(): |
| 15 | + # GIVEN a resolver with an exception handler defined for RequestValidationError |
| 16 | + app = ApiGatewayResolver(enable_validation=True) |
| 17 | + |
| 18 | + @app.exception_handler(RequestValidationError) |
| 19 | + def handle_validation_error(ex: RequestValidationError): |
| 20 | + return Response( |
| 21 | + status_code=422, |
| 22 | + content_type=content_types.TEXT_PLAIN, |
| 23 | + body=f"Invalid data. Number of errors: {len(ex.errors())}", |
| 24 | + ) |
| 25 | + |
| 26 | + @app.get("/my/path") |
| 27 | + def get_lambda(param: int): ... |
| 28 | + |
| 29 | + # WHEN calling the event handler |
| 30 | + # AND a RequestValidationError is raised |
| 31 | + result = app(LOAD_GW_EVENT, {}) |
| 32 | + |
| 33 | + # THEN call the exception_handler |
| 34 | + assert result["statusCode"] == 422 |
| 35 | + assert result["multiValueHeaders"]["Content-Type"] == [content_types.TEXT_PLAIN] |
| 36 | + assert result["body"] == "Invalid data. Number of errors: 1" |
| 37 | + |
| 38 | + |
| 39 | +def test_exception_handler_with_data_validation_pydantic_response(): |
| 40 | + # GIVEN a resolver with an exception handler defined for RequestValidationError |
| 41 | + app = ApiGatewayResolver(enable_validation=True) |
| 42 | + |
| 43 | + class Err(BaseModel): |
| 44 | + msg: str |
| 45 | + |
| 46 | + @app.exception_handler(RequestValidationError) |
| 47 | + def handle_validation_error(ex: RequestValidationError): |
| 48 | + return Response( |
| 49 | + status_code=422, |
| 50 | + content_type=content_types.APPLICATION_JSON, |
| 51 | + body=Err(msg=f"Invalid data. Number of errors: {len(ex.errors())}"), |
| 52 | + ) |
| 53 | + |
| 54 | + @app.get("/my/path") |
| 55 | + def get_lambda(param: int): ... |
| 56 | + |
| 57 | + # WHEN calling the event handler |
| 58 | + # AND a RequestValidationError is raised |
| 59 | + result = app(LOAD_GW_EVENT, {}) |
| 60 | + |
| 61 | + # THEN exception handler's pydantic response should be serialized correctly |
| 62 | + assert result["statusCode"] == 422 |
| 63 | + assert result["body"] == '{"msg":"Invalid data. Number of errors: 1"}' |
| 64 | + |
| 65 | + |
| 66 | +def test_data_validation_error(): |
| 67 | + # GIVEN a resolver without an exception handler |
| 68 | + app = ApiGatewayResolver(enable_validation=True) |
| 69 | + |
| 70 | + @app.get("/my/path") |
| 71 | + def get_lambda(param: int): ... |
| 72 | + |
| 73 | + # WHEN calling the event handler |
| 74 | + # AND a RequestValidationError is raised |
| 75 | + result = app(LOAD_GW_EVENT, {}) |
| 76 | + |
| 77 | + # THEN call the exception_handler |
| 78 | + assert result["statusCode"] == 422 |
| 79 | + assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON] |
| 80 | + assert "missing" in result["body"] |
0 commit comments