diff --git a/tests/test_handler.py b/tests/test_handler.py index 66e25fe30..a3ac8be10 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -285,6 +285,30 @@ def test_wsgi_script_binary_support_base64_behavior(self): self.assertTrue(response["isBase64Encoded"]) self.assertTrue(is_base64(response["body"])) + content_encoded_json = { + **text_plain_event, + **{"path": "/content_encoding_header_json1"}, + } + + response = lh.handler(content_encoded_json, None) + + self.assertEqual(response["statusCode"], 200) + self.assertIn("isBase64Encoded", response) + self.assertTrue(response["isBase64Encoded"]) + self.assertTrue(is_base64(response["body"])) + + content_encoded_text_arbitrary = { + **text_plain_event, + **{"path": "/content_encoding_header_textarbitrary1"}, + } + + response = lh.handler(content_encoded_text_arbitrary, None) + + self.assertEqual(response["statusCode"], 200) + self.assertIn("isBase64Encoded", response) + self.assertTrue(response["isBase64Encoded"]) + self.assertTrue(is_base64(response["body"])) + def test_wsgi_script_on_cognito_event_request(self): """ Ensure that requests sent by cognito behave sensibly diff --git a/tests/test_wsgi_binary_support_app.py b/tests/test_wsgi_binary_support_app.py index 5666426de..ccb78397a 100644 --- a/tests/test_wsgi_binary_support_app.py +++ b/tests/test_wsgi_binary_support_app.py @@ -28,3 +28,21 @@ def json_mimetype_response_1(): @app.route("/arbitrarybinary_mimetype_response1", methods=["GET"]) def arbitrary_mimetype_response_1(): return Response(response=b"some binary data", mimetype="arbitrary/binary_mimetype") + + +@app.route("/content_encoding_header_json1", methods=["GET"]) +def response_with_content_encoding_mimetype1(): + return Response( + response=json.dumps({"some": "data"}), + mimetype="application/json", + headers={"Content-Encoding": "gzip"}, + ) + + +@app.route("/content_encoding_header_textarbitrary1", methods=["GET"]) +def response_with_content_encoding_mimetype2(): + return Response( + response="OK", + mimetype="text/arbitrary", + headers={"Content-Encoding": "shouldnt_matter"}, + ) diff --git a/zappa/handler.py b/zappa/handler.py index 41336dc15..8d95718f1 100644 --- a/zappa/handler.py +++ b/zappa/handler.py @@ -583,7 +583,17 @@ def handler(self, event, context): ) if response.data: - if ( + if settings.BINARY_SUPPORT and response.headers.get( + "Content-Encoding" + ): + # We could have a text response that's gzip + # encoded. Therefore, we base-64 encode it. + # Related: https://github.com/zappa/Zappa/issues/908 + zappa_returndict["body"] = base64.b64encode( + response.data + ).decode("utf-8") + zappa_returndict["isBase64Encoded"] = True + elif ( settings.BINARY_SUPPORT and not response.mimetype.startswith("text/") and response.mimetype != "application/json"