Skip to content

Commit 1484ac9

Browse files
author
Michael Brewer
committed
tests(event-handler): Add missing binary handling
1 parent 6f6a55c commit 1484ac9

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

tests/functional/event_handler/test_api_gateway.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import base64
22
import json
3-
import os
43
import zlib
4+
from pathlib import Path
55

66
import pytest
77

@@ -10,9 +10,13 @@
1010

1111

1212
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()
1620

1721

1822
def test_alb_event():
@@ -156,6 +160,21 @@ def handler(event, context):
156160
assert decompress == expected_value
157161

158162

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+
159178
def test_cache_control_200():
160179
app = ApiGatewayResolver()
161180

@@ -169,6 +188,7 @@ def handler(event, context):
169188
result = handler({"path": "/success", "httpMethod": "GET"}, None)
170189

171190
headers = result["headers"]
191+
assert headers["Content-Type"] == "text/html"
172192
assert headers["Cache-Control"] == "max-age=600"
173193

174194

@@ -185,4 +205,5 @@ def handler(event, context):
185205
result = handler({"path": "/fails", "httpMethod": "DELETE"}, None)
186206

187207
headers = result["headers"]
208+
assert headers["Content-Type"] == "text/html"
188209
assert headers["Cache-Control"] == "no-cache"

0 commit comments

Comments
 (0)