Fix the StatusCode returned by lambda invoke

According to the AWS documentation:
```
The HTTP status code will be in the 200 range for successful request.
For the RequestResponse invocation type this status code will be 200.
For the Event invocation type this status code will be 202.
For the DryRun invocation type the status code will be 204.
```
This commit is contained in:
Roque Pinel 2018-08-14 19:00:03 -04:00 committed by Bert Blommers
parent b223cbc11b
commit 2ae09c5335
2 changed files with 40 additions and 3 deletions

View File

@ -172,7 +172,13 @@ class LambdaResponse(BaseResponse):
function_name, qualifier, self.body, self.headers, response_headers
)
if payload:
return 202, response_headers, payload
if request.headers['X-Amz-Invocation-Type'] == 'Event':
status_code = 202
elif request.headers['X-Amz-Invocation-Type'] == 'DryRun':
status_code = 204
else:
status_code = 200
return status_code, response_headers, payload
else:
return 404, response_headers, "{}"

View File

@ -113,7 +113,7 @@ def test_invoke_requestresponse_function():
Payload=json.dumps(in_data),
)
success_result["StatusCode"].should.equal(202)
success_result["StatusCode"].should.equal(200)
result_obj = json.loads(
base64.b64decode(success_result["LogResult"]).decode("utf-8")
)
@ -151,6 +151,37 @@ def test_invoke_event_function():
json.loads(success_result["Payload"].read().decode("utf-8")).should.equal({})
@mock_lambda
def test_invoke_dryrun_function():
conn = boto3.client('lambda', 'us-west-2')
conn.create_function(
FunctionName='testFunction',
Runtime='python2.7',
Role=get_role_name(),
Handler='lambda_function.lambda_handler',
Code={
'ZipFile': get_test_zip_file1(),
},
Description='test lambda function',
Timeout=3,
MemorySize=128,
Publish=True,
)
conn.invoke.when.called_with(
FunctionName='notAFunction',
InvocationType='Event',
Payload='{}'
).should.throw(botocore.client.ClientError)
in_data = {'msg': 'So long and thanks for all the fish'}
success_result = conn.invoke(
FunctionName='testFunction', InvocationType='DryRun', Payload=json.dumps(in_data))
success_result["StatusCode"].should.equal(204)
json.loads(success_result['Payload'].read().decode(
'utf-8')).should.equal({})
if settings.TEST_SERVER_MODE:
@mock_ec2
@ -179,7 +210,7 @@ if settings.TEST_SERVER_MODE:
InvocationType="RequestResponse",
Payload=json.dumps(in_data),
)
result["StatusCode"].should.equal(202)
result["StatusCode"].should.equal(200)
actual_payload = json.loads(result["Payload"].read().decode("utf-8"))
expected_payload = {"id": vol.id, "state": vol.state, "size": vol.size}
actual_payload.should.equal(expected_payload)