1
1
import base64
2
2
import json
3
- import os
4
3
import zlib
4
+ from pathlib import Path
5
5
6
6
import pytest
7
7
10
10
11
11
12
12
def load_event (file_name : str ) -> dict :
13
- full_file_name = os .path .dirname (os .path .realpath (__file__ )) + "/../../events/" + file_name
14
- with open (full_file_name ) as fp :
15
- return json .load (fp )
13
+ path = Path (str (Path (__file__ ).parent .parent .parent ) + "/events/" + file_name )
14
+ return json .loads (path .read_text ())
15
+
16
+
17
+ def read_media (file_name : str ) -> bytes :
18
+ path = Path (str (Path (__file__ ).parent .parent .parent .parent ) + "/docs/media/" + file_name )
19
+ return path .read_bytes ()
16
20
17
21
18
22
def test_alb_event ():
@@ -156,6 +160,21 @@ def handler(event, context):
156
160
assert decompress == expected_value
157
161
158
162
163
+ def test_base64_encode ():
164
+ app = ApiGatewayResolver ()
165
+
166
+ @app .get ("/my/path" , compress = True )
167
+ def read_image ():
168
+ return 200 , "image/png" , read_media ("idempotent_sequence_exception.png" )
169
+
170
+ mock_event = {"path" : "/my/path" , "httpMethod" : "GET" , "headers" : {"Accept-Encoding" : "deflate, gzip" }}
171
+ result = app (mock_event , None )
172
+
173
+ assert result ["isBase64Encoded" ] is True
174
+ body = result ["body" ]
175
+ assert isinstance (body , str )
176
+
177
+
159
178
def test_cache_control_200 ():
160
179
app = ApiGatewayResolver ()
161
180
@@ -169,6 +188,7 @@ def handler(event, context):
169
188
result = handler ({"path" : "/success" , "httpMethod" : "GET" }, None )
170
189
171
190
headers = result ["headers" ]
191
+ assert headers ["Content-Type" ] == "text/html"
172
192
assert headers ["Cache-Control" ] == "max-age=600"
173
193
174
194
@@ -185,4 +205,5 @@ def handler(event, context):
185
205
result = handler ({"path" : "/fails" , "httpMethod" : "DELETE" }, None )
186
206
187
207
headers = result ["headers" ]
208
+ assert headers ["Content-Type" ] == "text/html"
188
209
assert headers ["Cache-Control" ] == "no-cache"
0 commit comments