|
| 1 | +import pytest |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.parser import ValidationError, event_parser |
| 4 | +from aws_lambda_powertools.utilities.parser.models import AlbModel |
| 5 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 6 | +from tests.functional.parser.utils import load_event |
| 7 | + |
| 8 | + |
| 9 | +@event_parser(model=AlbModel) |
| 10 | +def handle_alb(event: AlbModel, _: LambdaContext): |
| 11 | + assert ( |
| 12 | + event.requestContext.elb.targetGroupArn |
| 13 | + == "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a" # noqa E501 |
| 14 | + ) |
| 15 | + assert event.httpMethod == "GET" |
| 16 | + assert event.path == "/lambda" |
| 17 | + assert event.queryStringParameters == {"query": "1234ABCD"} |
| 18 | + assert event.headers == { |
| 19 | + "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", |
| 20 | + "accept-encoding": "gzip", |
| 21 | + "accept-language": "en-US,en;q=0.9", |
| 22 | + "connection": "keep-alive", |
| 23 | + "host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com", |
| 24 | + "upgrade-insecure-requests": "1", |
| 25 | + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", # noqa E501 |
| 26 | + "x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476", |
| 27 | + "x-forwarded-for": "72.12.164.125", |
| 28 | + "x-forwarded-port": "80", |
| 29 | + "x-forwarded-proto": "http", |
| 30 | + "x-imforwards": "20", |
| 31 | + } |
| 32 | + assert event.body == "Test" |
| 33 | + assert not event.isBase64Encoded |
| 34 | + |
| 35 | + |
| 36 | +def test_alb_trigger_event(): |
| 37 | + event_dict = load_event("albEvent.json") |
| 38 | + handle_alb(event_dict, LambdaContext()) |
| 39 | + |
| 40 | + |
| 41 | +def test_validate_event_does_not_conform_with_model(): |
| 42 | + event = {"invalid": "event"} |
| 43 | + with pytest.raises(ValidationError): |
| 44 | + handle_alb(event, LambdaContext()) |
0 commit comments