diff --git a/index.js b/index.js index a8ebbb6..f34a009 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/tests/index.test.js b/tests/index.test.js index 6935575..a2e3776 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -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() + }) })