Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected issue with base64 encoding on all responses #1853

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mock import Mock
import sys
import unittest
import json
from zappa.handler import LambdaHandler
from zappa.utilities import merge_headers

Expand Down Expand Up @@ -217,6 +218,34 @@ def test_wsgi_script_name_on_test_request(self):
'https://zappa:80/return/request/url'
)

def test_wsgi_script_name_on_test_json_request(self):
"""
Ensure that requests with text responses are not base64 encoded
"""
lh = LambdaHandler('tests.test_wsgi_script_json')

event = {
'body': '',
'resource': '/{proxy+}',
'requestContext': {},
'queryStringParameters': {},
'headers': {},
'pathParameters': {
'proxy': 'json'
},
'httpMethod': 'GET',
'stageVariables': {},
'path': '/json'
}
response = lh.handler(event, None)

self.assertEqual(response['statusCode'], 200)
self.assertEqual(response.get('isBase64Encoded', False), False)
self.assertEqual(
json.loads(response['body']).get('data', ''),
'json_data'
)

def test_exception_handler_on_web_request(self):
"""
Ensure that app exceptions triggered by web requests use the exception_handler.
Expand Down
20 changes: 20 additions & 0 deletions tests/test_wsgi_script_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from flask import Flask, jsonify

app = Flask(__name__)

API_STAGE = 'dev'
APP_FUNCTION = 'app'
APP_MODULE = 'tests.test_wsgi_script_json'
BINARY_SUPPORT = True
CONTEXT_HEADER_MAPPINGS = {}
DEBUG = 'True'
DJANGO_SETTINGS = None
DOMAIN = 'api.example.com'
ENVIRONMENT_VARIABLES = {}
LOG_LEVEL = 'DEBUG'
PROJECT_NAME = 'wsgi_script_json'
COGNITO_TRIGGER_MAPPING = {}

@app.route('/json', methods=['GET'])
def return_json():
return jsonify(data="json_data")
6 changes: 4 additions & 2 deletions zappa/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,10 @@ def handler(self, event, context):

if response.data:
if settings.BINARY_SUPPORT:
if not response.mimetype.startswith("text/") \
or response.mimetype != "application/json":
if (
not response.mimetype.startswith("text/") and
response.mimetype != "application/json"
):
zappa_returndict['body'] = base64.b64encode(response.data).decode('utf-8')
zappa_returndict["isBase64Encoded"] = True
else:
Expand Down