Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 366cc3c

Browse files
committed
Write tests confirming Binary Support response behavior
No functional change to the Zappa codebase is introduced by this commit. This area of the application was untested. Tests introduced to ensure new behavior discussed in zappa#908 does not cause a regression.
1 parent 9036220 commit 366cc3c

4 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
API_STAGE = 'dev'
2+
APP_FUNCTION = 'app'
3+
APP_MODULE = 'tests.test_wsgi_binary_support_app'
4+
BINARY_SUPPORT = True
5+
CONTEXT_HEADER_MAPPINGS = {}
6+
DEBUG = 'True'
7+
DJANGO_SETTINGS = None
8+
DOMAIN = 'api.example.com'
9+
ENVIRONMENT_VARIABLES = {}
10+
LOG_LEVEL = 'DEBUG'
11+
PROJECT_NAME = 'binary_support_settings'
12+
COGNITO_TRIGGER_MAPPING = {}

tests/test_handler.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from mock import Mock
22
import sys
33
import unittest
4+
5+
from tests.utils import is_base64
46
from zappa.handler import LambdaHandler
57
from zappa.utilities import merge_headers
68

@@ -242,6 +244,59 @@ def test_exception_handler_on_web_request(self):
242244
self.assertEqual(response['statusCode'], 500)
243245
mocked_exception_handler.assert_called()
244246

247+
def test_wsgi_script_binary_support_base64_behavior(self):
248+
"""
249+
With Binary Support enabled, response mimetypes that are not text/* or application/json will be base64 encoded
250+
"""
251+
lh = LambdaHandler('tests.test_binary_support_settings')
252+
253+
text_plain_event = {
254+
'body': '',
255+
'resource': '/{proxy+}',
256+
'requestContext': {},
257+
'queryStringParameters': {},
258+
'headers': {
259+
'Host': '1234567890.execute-api.us-east-1.amazonaws.com',
260+
},
261+
'pathParameters': {
262+
'proxy': 'return/request/url'
263+
},
264+
'httpMethod': 'GET',
265+
'stageVariables': {},
266+
'path': '/textplain_mimetype_response1'
267+
}
268+
269+
response = lh.handler(text_plain_event, None)
270+
271+
self.assertEqual(response['statusCode'], 200)
272+
self.assertNotIn('isBase64Encoded', response)
273+
self.assertFalse(is_base64(response['body']))
274+
275+
text_arbitrary_event = {**text_plain_event, **{'path': '/textarbitrary_mimetype_response1'}}
276+
277+
response = lh.handler(text_arbitrary_event, None)
278+
279+
self.assertEqual(response['statusCode'], 200)
280+
self.assertNotIn('isBase64Encoded', response)
281+
self.assertFalse(is_base64(response['body']))
282+
283+
application_json_event = {**text_plain_event, **{'path': '/json_mimetype_response1'}}
284+
285+
response = lh.handler(application_json_event, None)
286+
287+
self.assertEqual(response['statusCode'], 200)
288+
self.assertNotIn('isBase64Encoded', response)
289+
self.assertFalse(is_base64(response['body']))
290+
291+
arbitrary_binary_event = {**text_plain_event, **{'path': '/arbitrarybinary_mimetype_response1'}}
292+
293+
response = lh.handler(arbitrary_binary_event, None)
294+
295+
self.assertEqual(response['statusCode'], 200)
296+
self.assertIn('isBase64Encoded', response)
297+
self.assertTrue(response['isBase64Encoded'])
298+
self.assertTrue(is_base64(response['body']))
299+
245300
def test_wsgi_script_on_cognito_event_request(self):
246301
"""
247302
Ensure that requests sent by cognito behave sensibly
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
###
2+
# This test application exists to confirm how Zappa handles WSGI application
3+
# _responses_ when Binary Support is enabled.
4+
###
5+
6+
import io
7+
import json
8+
from flask import Flask, Response, send_file
9+
10+
app = Flask(__name__)
11+
12+
13+
@app.route('/textplain_mimetype_response1', methods=['GET'])
14+
def text_mimetype_response_1():
15+
return Response(response="OK", mimetype="text/plain")
16+
17+
18+
@app.route('/textarbitrary_mimetype_response1', methods=['GET'])
19+
def text_mimetype_response_2():
20+
return Response(response="OK", mimetype="text/arbitary")
21+
22+
23+
@app.route('/json_mimetype_response1', methods=['GET'])
24+
def json_mimetype_response_1():
25+
return Response(response=json.dumps({"some": "data"}), mimetype="application/json")
26+
27+
28+
@app.route('/arbitrarybinary_mimetype_response1', methods=['GET'])
29+
def arbitrary_mimetype_response_1():
30+
return Response(response=b"some binary data", mimetype="arbitrary/binary_mimetype")

tests/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import placebo
23
import boto3
34
import os
@@ -73,3 +74,11 @@ def stub_open(*args, **kwargs):
7374

7475
with patch('__builtin__.open', stub_open):
7576
yield mock_open, mock_file
77+
78+
79+
def is_base64(test_string: str) -> bool:
80+
# Taken from https://stackoverflow.com/a/45928164/3200002
81+
try:
82+
return base64.b64encode(base64.b64decode(test_string)).decode('utf-8') == test_string
83+
except Exception:
84+
return False

0 commit comments

Comments
 (0)