3
3
import zlib
4
4
from decimal import Decimal
5
5
from pathlib import Path
6
- from typing import Dict , Tuple
6
+ from typing import Dict
7
7
8
8
from aws_lambda_powertools .event_handler .api_gateway import (
9
9
ApiGatewayResolver ,
@@ -29,10 +29,10 @@ def read_media(file_name: str) -> bytes:
29
29
30
30
def test_alb_event ():
31
31
# GIVEN a Application Load Balancer proxy type event
32
- app = ApiGatewayResolver (proxy_type = ProxyEventType .alb_event )
32
+ app = ApiGatewayResolver (proxy_type = ProxyEventType .ALBEvent )
33
33
34
34
@app .get ("/lambda" )
35
- def foo () -> Tuple [ int , str , str ] :
35
+ def foo ():
36
36
assert isinstance (app .current_event , ALBEvent )
37
37
assert app .lambda_context == {}
38
38
return 200 , TEXT_HTML , "foo"
@@ -49,13 +49,13 @@ def foo() -> Tuple[int, str, str]:
49
49
50
50
def test_api_gateway_v1 ():
51
51
# GIVEN a Http API V1 proxy type event
52
- app = ApiGatewayResolver (proxy_type = ProxyEventType .http_api_v1 )
52
+ app = ApiGatewayResolver (proxy_type = ProxyEventType .APIGatewayProxyEvent )
53
53
54
54
@app .get ("/my/path" )
55
- def get_lambda () -> Tuple [ int , str , str ] :
55
+ def get_lambda () -> Response :
56
56
assert isinstance (app .current_event , APIGatewayProxyEvent )
57
57
assert app .lambda_context == {}
58
- return 200 , APPLICATION_JSON , json .dumps ({"foo" : "value" })
58
+ return Response ( 200 , APPLICATION_JSON , json .dumps ({"foo" : "value" }) )
59
59
60
60
# WHEN calling the event handler
61
61
result = app (LOAD_GW_EVENT , {})
@@ -68,12 +68,12 @@ def get_lambda() -> Tuple[int, str, str]:
68
68
69
69
def test_api_gateway ():
70
70
# GIVEN a Rest API Gateway proxy type event
71
- app = ApiGatewayResolver (proxy_type = ProxyEventType .api_gateway )
71
+ app = ApiGatewayResolver (proxy_type = ProxyEventType .APIGatewayProxyEvent )
72
72
73
73
@app .get ("/my/path" )
74
- def get_lambda () -> Tuple [ int , str , str ] :
74
+ def get_lambda () -> Response :
75
75
assert isinstance (app .current_event , APIGatewayProxyEvent )
76
- return 200 , TEXT_HTML , "foo"
76
+ return Response ( 200 , TEXT_HTML , "foo" )
77
77
78
78
# WHEN calling the event handler
79
79
result = app (LOAD_GW_EVENT , {})
@@ -87,13 +87,13 @@ def get_lambda() -> Tuple[int, str, str]:
87
87
88
88
def test_api_gateway_v2 ():
89
89
# GIVEN a Http API V2 proxy type event
90
- app = ApiGatewayResolver (proxy_type = ProxyEventType .http_api_v2 )
90
+ app = ApiGatewayResolver (proxy_type = ProxyEventType .APIGatewayProxyEventV2 )
91
91
92
92
@app .post ("/my/path" )
93
- def my_path () -> Tuple [ int , str , str ] :
93
+ def my_path () -> Response :
94
94
assert isinstance (app .current_event , APIGatewayProxyEventV2 )
95
95
post_data = app .current_event .json_body
96
- return 200 , "plain/text" , post_data ["username" ]
96
+ return Response ( 200 , "plain/text" , post_data ["username" ])
97
97
98
98
# WHEN calling the event handler
99
99
result = app (load_event ("apiGatewayProxyV2Event.json" ), {})
@@ -110,9 +110,9 @@ def test_include_rule_matching():
110
110
app = ApiGatewayResolver ()
111
111
112
112
@app .get ("/<name>/<my_id>" )
113
- def get_lambda (my_id : str , name : str ) -> Tuple [ int , str , str ] :
113
+ def get_lambda (my_id : str , name : str ) -> Response :
114
114
assert name == "my"
115
- return 200 , TEXT_HTML , my_id
115
+ return Response ( 200 , TEXT_HTML , my_id )
116
116
117
117
# WHEN calling the event handler
118
118
result = app (LOAD_GW_EVENT , {})
@@ -179,8 +179,8 @@ def test_cors():
179
179
app = ApiGatewayResolver ()
180
180
181
181
@app .get ("/my/path" , cors = True )
182
- def with_cors () -> Tuple [ int , str , str ] :
183
- return 200 , TEXT_HTML , "test"
182
+ def with_cors () -> Response :
183
+ return Response ( 200 , TEXT_HTML , "test" )
184
184
185
185
def handler (event , context ):
186
186
return app .resolve (event , context )
@@ -205,8 +205,8 @@ def test_compress():
205
205
expected_value = '{"test": "value"}'
206
206
207
207
@app .get ("/my/request" , compress = True )
208
- def with_compression () -> Tuple [ int , str , str ] :
209
- return 200 , APPLICATION_JSON , expected_value
208
+ def with_compression () -> Response :
209
+ return Response ( 200 , APPLICATION_JSON , expected_value )
210
210
211
211
def handler (event , context ):
212
212
return app .resolve (event , context )
@@ -230,8 +230,8 @@ def test_base64_encode():
230
230
mock_event = {"path" : "/my/path" , "httpMethod" : "GET" , "headers" : {"Accept-Encoding" : "deflate, gzip" }}
231
231
232
232
@app .get ("/my/path" , compress = True )
233
- def read_image () -> Tuple [ int , str , bytes ] :
234
- return 200 , "image/png" , read_media ("idempotent_sequence_exception.png" )
233
+ def read_image () -> Response :
234
+ return Response ( 200 , "image/png" , read_media ("idempotent_sequence_exception.png" ) )
235
235
236
236
# WHEN calling the event handler
237
237
result = app (mock_event , None )
@@ -251,8 +251,8 @@ def test_compress_no_accept_encoding():
251
251
expected_value = "Foo"
252
252
253
253
@app .get ("/my/path" , compress = True )
254
- def return_text () -> Tuple [ int , str , str ] :
255
- return 200 , "text/plain" , expected_value
254
+ def return_text () -> Response :
255
+ return Response ( 200 , "text/plain" , expected_value )
256
256
257
257
# WHEN calling the event handler
258
258
result = app ({"path" : "/my/path" , "httpMethod" : "GET" , "headers" : {}}, None )
@@ -267,8 +267,8 @@ def test_cache_control_200():
267
267
app = ApiGatewayResolver ()
268
268
269
269
@app .get ("/success" , cache_control = "max-age=600" )
270
- def with_cache_control () -> Tuple [ int , str , str ] :
271
- return 200 , TEXT_HTML , "has 200 response"
270
+ def with_cache_control () -> Response :
271
+ return Response ( 200 , TEXT_HTML , "has 200 response" )
272
272
273
273
def handler (event , context ):
274
274
return app .resolve (event , context )
@@ -288,8 +288,8 @@ def test_cache_control_non_200():
288
288
app = ApiGatewayResolver ()
289
289
290
290
@app .delete ("/fails" , cache_control = "max-age=600" )
291
- def with_cache_control_has_500 () -> Tuple [ int , str , str ] :
292
- return 503 , TEXT_HTML , "has 503 response"
291
+ def with_cache_control_has_500 () -> Response :
292
+ return Response ( 503 , TEXT_HTML , "has 503 response" )
293
293
294
294
def handler (event , context ):
295
295
return app .resolve (event , context )
@@ -306,7 +306,7 @@ def handler(event, context):
306
306
307
307
def test_rest_api ():
308
308
# GIVEN a function that returns a Dict
309
- app = ApiGatewayResolver (proxy_type = ProxyEventType .http_api_v1 )
309
+ app = ApiGatewayResolver (proxy_type = ProxyEventType .APIGatewayProxyEvent )
310
310
expected_dict = {"foo" : "value" , "second" : Decimal ("100.01" )}
311
311
312
312
@app .get ("/my/path" )
@@ -325,7 +325,7 @@ def rest_func() -> Dict:
325
325
326
326
def test_handling_response_type ():
327
327
# GIVEN a function that returns Response
328
- app = ApiGatewayResolver (proxy_type = ProxyEventType .http_api_v1 )
328
+ app = ApiGatewayResolver (proxy_type = ProxyEventType .APIGatewayProxyEvent )
329
329
330
330
@app .get ("/my/path" )
331
331
def rest_func () -> Response :
0 commit comments