|
2 | 2 | from dataclasses import dataclass
|
3 | 3 | from enum import Enum
|
4 | 4 | from pathlib import PurePath
|
5 |
| -from typing import Tuple |
| 5 | +from typing import List, Tuple |
6 | 6 |
|
7 | 7 | import pytest
|
8 | 8 | from pydantic import BaseModel
|
@@ -113,6 +113,24 @@ def handler() -> int:
|
113 | 113 | assert result["body"] == "123"
|
114 | 114 |
|
115 | 115 |
|
| 116 | +def test_validate_return_list(): |
| 117 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 118 | + app = APIGatewayRestResolver(enable_validation=True) |
| 119 | + |
| 120 | + # WHEN a handler is defined with a return type |
| 121 | + @app.get("/") |
| 122 | + def handler() -> List[int]: |
| 123 | + return [123, 234] |
| 124 | + |
| 125 | + LOAD_GW_EVENT["path"] = "/" |
| 126 | + |
| 127 | + # THEN the handler should be invoked and return 200 |
| 128 | + # THEN the body must be [123, 234] |
| 129 | + result = app(LOAD_GW_EVENT, {}) |
| 130 | + assert result["statusCode"] == 200 |
| 131 | + assert result["body"] == "[123, 234]" |
| 132 | + |
| 133 | + |
116 | 134 | def test_validate_return_tuple():
|
117 | 135 | # GIVEN an APIGatewayRestResolver with validation enabled
|
118 | 136 | app = APIGatewayRestResolver(enable_validation=True)
|
@@ -143,7 +161,7 @@ def test_validate_return_purepath():
|
143 | 161 | # WHEN return value is a PurePath
|
144 | 162 | @app.get("/")
|
145 | 163 | def handler() -> str:
|
146 |
| - return sample_path |
| 164 | + return sample_path.as_posix() |
147 | 165 |
|
148 | 166 | LOAD_GW_EVENT["path"] = "/"
|
149 | 167 |
|
@@ -266,6 +284,30 @@ def handler(user: Model) -> Model:
|
266 | 284 | assert json.loads(result["body"]) == {"name": "John", "age": 30}
|
267 | 285 |
|
268 | 286 |
|
| 287 | +def test_validate_body_param_with_invalid_date(): |
| 288 | + # GIVEN an APIGatewayRestResolver with validation enabled |
| 289 | + app = APIGatewayRestResolver(enable_validation=True) |
| 290 | + |
| 291 | + class Model(BaseModel): |
| 292 | + name: str |
| 293 | + age: int |
| 294 | + |
| 295 | + # WHEN a handler is defined with a body parameter |
| 296 | + @app.post("/") |
| 297 | + def handler(user: Model) -> Model: |
| 298 | + return user |
| 299 | + |
| 300 | + LOAD_GW_EVENT["httpMethod"] = "POST" |
| 301 | + LOAD_GW_EVENT["path"] = "/" |
| 302 | + LOAD_GW_EVENT["body"] = "{" # invalid JSON |
| 303 | + |
| 304 | + # THEN the handler should be invoked and return 422 |
| 305 | + # THEN the body must have the "json_invalid" error message |
| 306 | + result = app(LOAD_GW_EVENT, {}) |
| 307 | + assert result["statusCode"] == 422 |
| 308 | + assert "json_invalid" in result["body"] |
| 309 | + |
| 310 | + |
269 | 311 | def test_validate_embed_body_param():
|
270 | 312 | # GIVEN an APIGatewayRestResolver with validation enabled
|
271 | 313 | app = APIGatewayRestResolver(enable_validation=True)
|
|
0 commit comments