Skip to content

Commit

Permalink
A small collection of fixes (#48)
Browse files Browse the repository at this point in the history
* fix: broken tests from #18

When the `done` function was removed in #18, the tests were not updated

* fix: TypeError when converting null or undefined

This will fix #34 by setting a default value on the
`lowerCaseKeys` function. You can get into this state when setting up a
test/demo incorrectly or using a lambda warmer (like serverless-warmup).
I am immediately returning because then a clear error can be shown.


Co-authored-by Mauricio Mercado <[email protected]>
  • Loading branch information
mattcan authored Nov 5, 2020
1 parent 0e6f825 commit 5685871
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const loadProbot = appFn => {
return probot
}

const lowerCaseKeys = obj =>
const lowerCaseKeys = (obj = {}) =>
Object.keys(obj).reduce((accumulator, key) =>
Object.assign(accumulator, { [key.toLocaleLowerCase()]: obj[key] }), {})

Expand All @@ -48,6 +48,12 @@ module.exports.serverless = appFn => {
// Determine incoming webhook event type
const headers = lowerCaseKeys(event.headers)
const e = headers['x-github-event']
if (!e) {
return {
statusCode: 400,
body: 'X-Github-Event header is missing'
}
}

// If body is expected to be base64 encoded, decode it and continue
if (event.isBase64Encoded) {
Expand All @@ -59,10 +65,10 @@ module.exports.serverless = appFn => {

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

// Do the thing
Expand Down
44 changes: 0 additions & 44 deletions tests/__snapshots__/index.test.js.snap

This file was deleted.

48 changes: 39 additions & 9 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ describe('serverless-lambda', () => {

it('responds with the homepage', async () => {
const event = { httpMethod: 'GET', path: '/probot' }
await handler(event, context)
expect(context.done).toHaveBeenCalled()
expect(context.done.mock.calls[0][0]).toMatchSnapshot()
const result = await handler(event, context)
expect(result).toMatchObject({
body: expect.any(String),
statusCode: 200,
headers: {
'Content-Type': 'text/html'
}
})
})

it('calls the event handler', async () => {
Expand All @@ -30,9 +35,9 @@ describe('serverless-lambda', () => {
}
}

await handler(event, context)
expect(context.done).toHaveBeenCalled()
const result = await handler(event, context)
expect(spy).toHaveBeenCalled()
expect(result.statusCode).toBe(200)
})

it('responds with a 400 error when body is null', async () => {
Expand All @@ -44,10 +49,35 @@ describe('serverless-lambda', () => {
}
}

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

it('responds with a 400 when no x-github-event header is sent (#48)', async () => {
const event = {
body: {
installation: { id: 1 }
},
headers: {
'x-github-delivery': 123
}
}

const result = await handler(event, context)
expect(spy).not.toHaveBeenCalled()
expect(result.statusCode).toBe(400)
})

it('responds with a 400 when no headers are present (#48)', async () => {
const event = {
body: {
installation: { id: 1 }
}
}

const result = await handler(event, context)
expect(spy).not.toHaveBeenCalled()
expect(result.statusCode).toBe(400)
})
})

0 comments on commit 5685871

Please sign in to comment.