Skip to content

Commit

Permalink
fix: trigger error handler when missing headers (#34)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: when X-Hub-Signature header is missing an `error` event is emitted. Before a message was logged only
  • Loading branch information
bkeepers authored and gr2m committed Aug 8, 2018
1 parent dca3336 commit 06d375b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions middleware/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ function middleware (state, request, response, next) {

const missingHeaders = getMissingHeaders(request).join(', ')
if (missingHeaders) {
debug(`ignored: ${request.method} ${request.url} due to missing headers: ${missingHeaders}`)
const error = new Error(`Required headers missing: ${missingHeaders}`)

response.statusCode = 400
response.end(`Required headers missing: ${missingHeaders}`)
return
return state.eventHandler.receive(error)
.catch(() => {
response.statusCode = 400
response.end(error.message)
})
}

const eventName = request.headers['x-github-event']
Expand Down
3 changes: 3 additions & 0 deletions test/integration/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ test('POST / with push event payload', (t) => {
test('POST / with push event payload (no signature)', (t) => {
const api = new Webhooks({secret: 'mysecret'})
const server = http.createServer(api.middleware)
const errorHandler = simple.spy()
api.on('error', errorHandler)

promisify(server.listen.bind(server))(this.port)

Expand All @@ -114,6 +116,7 @@ test('POST / with push event payload (no signature)', (t) => {
})

.then(() => {
t.is(errorHandler.callCount, 1, 'calls "error" event handler')
server.close(t.end)
})

Expand Down

0 comments on commit 06d375b

Please sign in to comment.