Skip to content

Commit 9ee7702

Browse files
author
Michael Brewer
committed
fix(event-handler): Set Content-Encoding header for compress
1 parent 1484ac9 commit 9ee7702

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ def resolve(self, event: Dict, context: LambdaContext) -> Dict:
7575
headers["Cache-Control"] = route.cache_control if status_code == 200 else "no-cache"
7676

7777
if route.compress and "gzip" in (self.current_event.get_header_value("accept-encoding") or ""):
78+
headers["Content-Encoding"] = "gzip"
7879
if isinstance(body, str):
7980
body = bytes(body, "utf-8")
80-
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
81-
body = gzip_compress.compress(body) + gzip_compress.flush()
81+
gzip = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
82+
body = gzip.compress(body) + gzip.flush()
8283

8384
base64_encoded = False
8485
if isinstance(body, bytes):

tests/functional/event_handler/test_api_gateway.py

+18
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def handler(event, context):
158158
assert isinstance(body, str)
159159
decompress = zlib.decompress(base64.b64decode(body), wbits=zlib.MAX_WBITS | 16).decode("UTF-8")
160160
assert decompress == expected_value
161+
headers = result["headers"]
162+
assert headers["Content-Encoding"] == "gzip"
161163

162164

163165
def test_base64_encode():
@@ -173,6 +175,22 @@ def read_image():
173175
assert result["isBase64Encoded"] is True
174176
body = result["body"]
175177
assert isinstance(body, str)
178+
headers = result["headers"]
179+
assert headers["Content-Encoding"] == "gzip"
180+
181+
182+
def test_compress_no_accept_encoding():
183+
app = ApiGatewayResolver()
184+
expected_value = "Foo"
185+
186+
@app.get("/my/path", compress=True)
187+
def return_text():
188+
return 200, "text/plain", expected_value
189+
190+
result = app({"path": "/my/path", "httpMethod": "GET", "headers": {}}, None)
191+
192+
assert result["isBase64Encoded"] is False
193+
assert result["body"] == expected_value
176194

177195

178196
def test_cache_control_200():

0 commit comments

Comments
 (0)