-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathtest_custom.py
61 lines (53 loc) · 1.91 KB
/
test_custom.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from mangum.types import Headers, LambdaConfig, LambdaContext, LambdaEvent, Scope
class CustomHandler:
@classmethod
def infer(cls, event: LambdaEvent, context: LambdaContext, config: LambdaConfig) -> bool:
return "my-custom-key" in event
def __init__(self, event: LambdaEvent, context: LambdaContext, config: LambdaConfig) -> None:
self.event = event
self.context = context
self.config = config
@property
def body(self) -> bytes:
return b"My request body"
@property
def scope(self) -> Scope:
headers: dict[str, str] = {}
return {
"type": "http",
"http_version": "1.1",
"method": "GET",
"headers": [[k.encode(), v.encode()] for k, v in headers.items()],
"path": "/",
"raw_path": None,
"root_path": "",
"scheme": "https",
"query_string": b"",
"server": ("mangum", 8080),
"client": ("127.0.0.1", 0),
"asgi": {"version": "3.0", "spec_version": "2.0"},
"aws.event": self.event,
"aws.context": self.context,
}
def __call__(self, *, status: int, headers: Headers, body: bytes) -> dict:
return {"statusCode": status, "headers": {}, "body": body.decode()}
def test_custom_handler():
event = {"my-custom-key": 1}
handler = CustomHandler(event, {}, {"api_gateway_base_path": "/"})
assert isinstance(handler.body, bytes)
assert handler.scope == {
"asgi": {"version": "3.0", "spec_version": "2.0"},
"aws.context": {},
"aws.event": event,
"client": ("127.0.0.1", 0),
"headers": [],
"http_version": "1.1",
"method": "GET",
"path": "/",
"query_string": b"",
"raw_path": None,
"root_path": "",
"scheme": "https",
"server": ("mangum", 8080),
"type": "http",
}