Skip to content

Commit 5685871

Browse files
authored
A small collection of fixes (#48)
* 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]>
1 parent 0e6f825 commit 5685871

File tree

3 files changed

+48
-56
lines changed

3 files changed

+48
-56
lines changed

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const loadProbot = appFn => {
2121
return probot
2222
}
2323

24-
const lowerCaseKeys = obj =>
24+
const lowerCaseKeys = (obj = {}) =>
2525
Object.keys(obj).reduce((accumulator, key) =>
2626
Object.assign(accumulator, { [key.toLocaleLowerCase()]: obj[key] }), {})
2727

@@ -48,6 +48,12 @@ module.exports.serverless = appFn => {
4848
// Determine incoming webhook event type
4949
const headers = lowerCaseKeys(event.headers)
5050
const e = headers['x-github-event']
51+
if (!e) {
52+
return {
53+
statusCode: 400,
54+
body: 'X-Github-Event header is missing'
55+
}
56+
}
5157

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

6066
// Bail for null body
6167
if (!event.body) {
62-
return context.done(null, {
68+
return {
6369
statusCode: 400,
6470
body: 'Event body is null.'
65-
})
71+
}
6672
}
6773

6874
// Do the thing

tests/__snapshots__/index.test.js.snap

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/index.test.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ describe('serverless-lambda', () => {
1414

1515
it('responds with the homepage', async () => {
1616
const event = { httpMethod: 'GET', path: '/probot' }
17-
await handler(event, context)
18-
expect(context.done).toHaveBeenCalled()
19-
expect(context.done.mock.calls[0][0]).toMatchSnapshot()
17+
const result = await handler(event, context)
18+
expect(result).toMatchObject({
19+
body: expect.any(String),
20+
statusCode: 200,
21+
headers: {
22+
'Content-Type': 'text/html'
23+
}
24+
})
2025
})
2126

2227
it('calls the event handler', async () => {
@@ -30,9 +35,9 @@ describe('serverless-lambda', () => {
3035
}
3136
}
3237

33-
await handler(event, context)
34-
expect(context.done).toHaveBeenCalled()
38+
const result = await handler(event, context)
3539
expect(spy).toHaveBeenCalled()
40+
expect(result.statusCode).toBe(200)
3641
})
3742

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

47-
await handler(event, context)
48-
expect(context.done).toHaveBeenCalledWith(null, expect.objectContaining({
49-
statusCode: 400
50-
}))
52+
const result = await handler(event, context)
53+
expect(result.statusCode).toBe(400)
54+
expect(spy).not.toHaveBeenCalled()
55+
})
56+
57+
it('responds with a 400 when no x-github-event header is sent (#48)', async () => {
58+
const event = {
59+
body: {
60+
installation: { id: 1 }
61+
},
62+
headers: {
63+
'x-github-delivery': 123
64+
}
65+
}
66+
67+
const result = await handler(event, context)
68+
expect(spy).not.toHaveBeenCalled()
69+
expect(result.statusCode).toBe(400)
70+
})
71+
72+
it('responds with a 400 when no headers are present (#48)', async () => {
73+
const event = {
74+
body: {
75+
installation: { id: 1 }
76+
}
77+
}
78+
79+
const result = await handler(event, context)
5180
expect(spy).not.toHaveBeenCalled()
81+
expect(result.statusCode).toBe(400)
5282
})
5383
})

0 commit comments

Comments
 (0)