@@ -4456,3 +4456,65 @@ def create(item: Item):
44564456 result = app (gw_event , {})
44574457 assert result ["statusCode" ] == 200
44584458 assert json .loads (result ["body" ]) == {"name" : "powertools" }
4459+
4460+
4461+ def test_header_sequence_optional_absent_uses_default (gw_event_http ):
4462+ # GIVEN an APIGatewayHttpResolver with validation enabled and an optional sequence header that is not sent
4463+ app = APIGatewayHttpResolver (enable_validation = True )
4464+ gw_event_http ["rawPath" ] = gw_event_http ["requestContext" ]["http" ]["path" ] = "/hello"
4465+ gw_event_http ["requestContext" ]["http" ]["method" ] = "GET"
4466+ gw_event_http ["headers" ] = {}
4467+
4468+ @app .get ("/hello" )
4469+ def hello (x_country : Annotated [list [str ] | None , Header (alias = "x-country" )] = None ):
4470+ return {"c" : x_country }
4471+
4472+ # WHEN the request has no such header
4473+ result = app (gw_event_http , {})
4474+
4475+ # THEN the default is used instead of failing validation
4476+ assert result ["statusCode" ] == 200
4477+ assert json .loads (result ["body" ]) == {"c" : None }
4478+
4479+
4480+ def test_header_model_sequence_field_single_value (gw_event_http ):
4481+ # GIVEN a Pydantic header model with a sequence field and a single header value on HTTP API
4482+ class HeaderModel (BaseModel ):
4483+ x_country : list [str ] = Field (alias = "x-country" )
4484+ x_trace : str = Field (alias = "x-trace" )
4485+
4486+ app = APIGatewayHttpResolver (enable_validation = True )
4487+ gw_event_http ["rawPath" ] = gw_event_http ["requestContext" ]["http" ]["path" ] = "/hello"
4488+ gw_event_http ["requestContext" ]["http" ]["method" ] = "GET"
4489+ gw_event_http ["headers" ] = {"x-country" : "US" , "x-trace" : "t-1" }
4490+
4491+ @app .get ("/hello" )
4492+ def hello (headers : Annotated [HeaderModel , Header ()]):
4493+ return {"c" : headers .x_country , "t" : headers .x_trace }
4494+
4495+ # WHEN the request carries one value for the sequence field
4496+ result = app (gw_event_http , {})
4497+
4498+ # THEN it is accepted as a one-item list and the scalar field is untouched
4499+ assert result ["statusCode" ] == 200
4500+ assert json .loads (result ["body" ]) == {"c" : ["US" ], "t" : "t-1" }
4501+
4502+
4503+ def test_header_model_sequence_field_comma_joined (gw_event_http ):
4504+ # GIVEN a Pydantic header model with a sequence field and a comma-joined header value
4505+ class HeaderModel (BaseModel ):
4506+ x_country : list [str ] = Field (alias = "x-country" )
4507+
4508+ app = APIGatewayHttpResolver (enable_validation = True )
4509+ gw_event_http ["rawPath" ] = gw_event_http ["requestContext" ]["http" ]["path" ] = "/hello"
4510+ gw_event_http ["requestContext" ]["http" ]["method" ] = "GET"
4511+ gw_event_http ["headers" ] = {"x-country" : "US,PT" }
4512+
4513+ @app .get ("/hello" )
4514+ def hello (headers : Annotated [HeaderModel , Header ()]):
4515+ return {"c" : headers .x_country }
4516+
4517+ result = app (gw_event_http , {})
4518+
4519+ assert result ["statusCode" ] == 200
4520+ assert json .loads (result ["body" ]) == {"c" : ["US" , "PT" ]}
0 commit comments