Skip to content

Commit 129f93e

Browse files
committed
fix: refactored test
1 parent eff77c8 commit 129f93e

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

tests/functional/event_handler/test_openapi_validation_middleware.py

+26-8
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,39 @@ class Model(BaseModel):
342342

343343
# WHEN a handler is defined with a body parameter
344344
@app.post("/")
345-
def handler(user: Annotated[Model, Body(embed=True)]) -> Response[Model]:
345+
def handler(user: Model) -> Response[Model]:
346346
return Response(body=user, status_code=200)
347347

348348
LOAD_GW_EVENT["httpMethod"] = "POST"
349349
LOAD_GW_EVENT["path"] = "/"
350350
LOAD_GW_EVENT["body"] = json.dumps({"name": "John", "age": 30})
351351

352-
# THEN the handler should be invoked and return 422
353-
# THEN the body must be a dict
354-
result = app(LOAD_GW_EVENT, {})
355-
assert result["statusCode"] == 422
356-
assert "missing" in result["body"]
357-
358352
# THEN the handler should be invoked and return 200
359353
# THEN the body must be a dict
360-
LOAD_GW_EVENT["body"] = json.dumps({"user": {"name": "John", "age": 30}})
361354
result = app(LOAD_GW_EVENT, {})
362355
assert result["statusCode"] == 200
356+
assert result["body"] == {"name": "John", "age": 30}
357+
358+
359+
def test_validate_response_invalid_return():
360+
# GIVEN an APIGatewayRestResolver with validation enabled
361+
app = APIGatewayRestResolver(enable_validation=True)
362+
363+
class Model(BaseModel):
364+
name: str
365+
age: int
366+
367+
# WHEN a handler is defined with a body parameter
368+
@app.post("/")
369+
def handler(user: Model) -> Response[Model]:
370+
return Response(body=user, status_code=200)
371+
372+
LOAD_GW_EVENT["httpMethod"] = "POST"
373+
LOAD_GW_EVENT["path"] = "/"
374+
LOAD_GW_EVENT["body"] = json.dumps({})
375+
376+
# THEN the handler should be invoked and return 422
377+
# THEN the body should have the word missing
378+
result = app(LOAD_GW_EVENT, {})
379+
assert result["statusCode"] == 422
380+
assert "missing" in result["body"]

0 commit comments

Comments
 (0)