From ed5203d445809c60f6c66dde1b0ae327124efe94 Mon Sep 17 00:00:00 2001 From: sveisvei Date: Wed, 28 Dec 2016 21:04:26 +0100 Subject: [PATCH 1/2] add app hooks --- lib/app.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/app.js b/lib/app.js index afc4dc31fb..a78b61f0d7 100644 --- a/lib/app.js +++ b/lib/app.js @@ -23,6 +23,11 @@ module.exports = function (config) { app.set('trust proxy'); app.set('port', config.port); app.locals.baseUriPath = baseUriPath; + + if (typeof config.preHook === 'function') { + config.preHook(app); + } + app.use(cookieParser()); if (publicFolder) { @@ -49,6 +54,10 @@ module.exports = function (config) { })); } + if (typeof config.preRouterHook === 'function') { + config.preRouterHook(app); + } + // Setup API routes const apiRouter = express.Router(); // eslint-disable-line new-cap routes.createAPI(apiRouter, config); From 4d63da7eff88abd3dbba7480fe32f2392b5df7fb Mon Sep 17 00:00:00 2001 From: sveisvei Date: Thu, 29 Dec 2016 11:42:54 +0100 Subject: [PATCH 2/2] add hook tests --- lib/app.test.js | 31 +++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 32 insertions(+) create mode 100644 lib/app.test.js diff --git a/lib/app.test.js b/lib/app.test.js new file mode 100644 index 0000000000..2124cb10a2 --- /dev/null +++ b/lib/app.test.js @@ -0,0 +1,31 @@ +'use strict'; + +const test = require('ava'); +const proxyquire = require('proxyquire'); +const getApp = proxyquire('./app', { + './routes': { + createAPI: () => {}, + createLegacy: () => {}, + }, +}); + +test('should not throw when valid config', t => { + const app = getApp({}); + t.true(typeof app.listen === 'function'); +}); + +test('should call preHook', t => { + let called = 0; + getApp({ preHook: () => { + called++; + } }); + t.true(called === 1); +}); + +test('should call preRouterHook', t => { + let called = 0; + getApp({ preRouterHook: () => { + called++; + } }); + t.true(called === 1); +}); diff --git a/package.json b/package.json index f0077faa6c..0e3ebdac3a 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "eslint": "^3.11.1", "eslint-config-finn": "^1.0.0-beta.1", "nyc": "^9.0.1", + "proxyquire": "^1.7.10", "sinon": "^1.17.5", "superagent": "^2.3.0", "supertest": "^2.0.1",