Skip to content

Commit 99e7860

Browse files
Adding 2e2 tests
1 parent 4dd80ae commit 99e7860

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from aws_lambda_powertools.event_handler import (
2+
ALBResolver,
3+
Response,
4+
)
5+
6+
app = ALBResolver()
7+
8+
9+
@app.get("/todos_with_no_body")
10+
def todos():
11+
return Response(
12+
status_code=200,
13+
)
14+
15+
16+
def lambda_handler(event, context):
17+
return app.resolve(event, context)

tests/e2e/event_handler/infrastructure.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, Optional
1+
from typing import Dict, List, Optional
22

33
from aws_cdk import CfnOutput
44
from aws_cdk import aws_apigateway as apigwv1
@@ -17,12 +17,12 @@ class EventHandlerStack(BaseInfrastructure):
1717
def create_resources(self):
1818
functions = self.create_lambda_functions()
1919

20-
self._create_alb(function=functions["AlbHandler"])
20+
self._create_alb(function=[functions["AlbHandler"], functions["AlbHandlerWithBodyNone"]])
2121
self._create_api_gateway_rest(function=functions["ApiGatewayRestHandler"])
2222
self._create_api_gateway_http(function=functions["ApiGatewayHttpHandler"])
2323
self._create_lambda_function_url(function=functions["LambdaFunctionUrlHandler"])
2424

25-
def _create_alb(self, function: Function):
25+
def _create_alb(self, function: List[Function]):
2626
vpc = ec2.Vpc.from_lookup(
2727
self.stack,
2828
"VPC",
@@ -33,15 +33,19 @@ def _create_alb(self, function: Function):
3333
alb = elbv2.ApplicationLoadBalancer(self.stack, "ALB", vpc=vpc, internet_facing=True)
3434
CfnOutput(self.stack, "ALBDnsName", value=alb.load_balancer_dns_name)
3535

36-
self._create_alb_listener(alb=alb, name="Basic", port=80, function=function)
36+
# Function with Body
37+
self._create_alb_listener(alb=alb, name="Basic", port=80, function=function[0])
3738
self._create_alb_listener(
3839
alb=alb,
3940
name="MultiValueHeader",
4041
port=8080,
41-
function=function,
42+
function=function[0],
4243
attributes={"lambda.multi_value_headers.enabled": "true"},
4344
)
4445

46+
# Function without Body
47+
self._create_alb_listener(alb=alb, name="BasicWithoutBody", port=8081, function=function[1])
48+
4549
def _create_alb_listener(
4650
self,
4751
alb: elbv2.ApplicationLoadBalancer,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from requests import Request
3+
4+
from tests.e2e.utils import data_fetcher
5+
from tests.e2e.utils.auth import build_iam_auth
6+
7+
8+
@pytest.fixture
9+
def alb_basic_without_body_listener_endpoint(infrastructure: dict) -> str:
10+
dns_name = infrastructure.get("ALBDnsName")
11+
port = infrastructure.get("ALBBasicWithoutBodyListenerPort", "")
12+
return f"http://{dns_name}:{port}"
13+
14+
15+
@pytest.mark.xdist_group(name="event_handler")
16+
def test_alb_with_body_empty(alb_basic_without_body_listener_endpoint):
17+
# GIVEN url has a trailing slash - it should behave as if there was not one
18+
url = f"{alb_basic_without_body_listener_endpoint}/todos_with_no_body"
19+
20+
# WHEN calling an invalid URL (with trailing slash) expect HTTPError exception from data_fetcher
21+
response = data_fetcher.get_http_response(
22+
Request(
23+
method="GET",
24+
url=url,
25+
auth=build_iam_auth(url=url, aws_service="lambda"),
26+
),
27+
)
28+
29+
assert response.status_code == 200

0 commit comments

Comments
 (0)