Skip to content
Merged
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
9 changes: 9 additions & 0 deletions samcli/local/apigw/local_apigw_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def _parse_lambda_output(lambda_output, binary_types, flask_request):
:param str lambda_output: Output from Lambda Invoke
:return: Tuple(int, dict, str, bool)
"""
# pylint: disable-msg=too-many-statements
json_output = json.loads(lambda_output)

if not isinstance(json_output, dict):
Expand All @@ -251,6 +252,14 @@ def _parse_lambda_output(lambda_output, binary_types, flask_request):
LOG.error(message)
raise TypeError(message)

try:
if body:
body = str(body)
except ValueError:
message = "Non null response bodies should be able to convert to string: {}".format(body)
LOG.error(message)
raise TypeError(message)

# API Gateway only accepts statusCode, body, headers, and isBase64Encoded in
# a response shape.
invalid_keys = LocalApigwService._invalid_apig_response_keys(json_output)
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/local/start_api/test_start_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,12 @@ def test_function_writing_to_stderr(self):
self.assertEquals(response.status_code, 200)
self.assertEquals(response.json(), {'hello': 'world'})

def test_integer_body(self):
response = requests.get(self.url + "/echo_integer_body")

self.assertEquals(response.status_code, 200)
self.assertEquals(response.content.decode('utf-8'), '42')


class TestServiceRequests(StartApiIntegBaseClass):
"""
Expand Down
11 changes: 4 additions & 7 deletions tests/integration/testdata/start_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,36 @@


def handler(event, context):

return {"statusCode": 200, "body": json.dumps({"hello": "world"})}


def echo_event_handler(event, context):

return {"statusCode": 200, "body": json.dumps(event)}


def echo_event_handler_2(event, context):

event['handler'] = 'echo_event_handler_2'

return {"statusCode": 200, "body": json.dumps(event)}


def content_type_setter_handler(event, context):
def echo_integer_body(event, context):
return {"statusCode": 200, "body": 42}


def content_type_setter_handler(event, context):
return {"statusCode": 200, "body": "hello", "headers": {"Content-Type": "text/plain"}}


def only_set_status_code_handler(event, context):

return {"statusCode": 200}


def only_set_body_handler(event, context):

return {"body": json.dumps({"hello": "world"})}


def string_status_code_handler(event, context):

return {"statusCode": "200", "body": json.dumps({"hello": "world"})}


Expand Down
13 changes: 13 additions & 0 deletions tests/integration/testdata/start_api/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ Resources:
Method: GET
Path: /echoeventbody

EchoIntegerBodyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: main.echo_integer_body
Runtime: python3.6
CodeUri: .
Events:
EchoEventBodyPath:
Type: Api
Properties:
Method: GET
Path: /echo_integer_body

ContentTypeSetterFunction:
Type: AWS::Serverless::Function
Properties:
Expand Down