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

fix(serverless): gracefully fail with 400 for null body #38

Merged
merged 2 commits into from
Sep 17, 2020
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
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ module.exports.serverless = appFn => {
// Convert the payload to an Object if API Gateway stringifies it
event.body = (typeof event.body === 'string') ? JSON.parse(event.body) : event.body

// Bail for null body
if (!event.body) {
return context.done(null, {
statusCode: 400,
body: 'Event body is null.'
})
}

// Do the thing
console.log(`Received event ${e}${event.body.action ? ('.' + event.body.action) : ''}`)
if (event) {
Expand Down
16 changes: 16 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,20 @@ describe('serverless-lambda', () => {
expect(context.done).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
})

it('responds with a 400 error when body is null', async () => {
const event = {
body: null,
headers: {
'X-Github-Event': 'issues',
'x-github-delivery': 123
}
}

await handler(event, context)
expect(context.done).toHaveBeenCalledWith(null, expect.objectContaining({
statusCode: 400
}))
expect(spy).not.toHaveBeenCalled()
})
})