From 06d375b1e752a905aed00d2b9c49bc1809e7eda5 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Wed, 8 Aug 2018 14:26:03 -0400 Subject: [PATCH] fix: trigger error handler when missing headers (#34) BREAKING CHANGE: when X-Hub-Signature header is missing an `error` event is emitted. Before a message was logged only --- middleware/middleware.js | 10 ++++++---- test/integration/server-test.js | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/middleware/middleware.js b/middleware/middleware.js index 8f1ef25c..2cf0ca72 100644 --- a/middleware/middleware.js +++ b/middleware/middleware.js @@ -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'] diff --git a/test/integration/server-test.js b/test/integration/server-test.js index 5e02d7fb..33d1cf3e 100644 --- a/test/integration/server-test.js +++ b/test/integration/server-test.js @@ -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) @@ -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) })