From 4d48bec4a1c9d08a4801cf36fbffe01d9101f36b Mon Sep 17 00:00:00 2001 From: ersoma Date: Sun, 24 Mar 2019 11:24:59 +0100 Subject: [PATCH] add excludeEnvVariables option with tests --- README.md | 1 + index.js | 1 + src/main.js | 7 ++-- tests/express.js | 88 +++++++++++++++++++++++++++++++++++++++++------ tests/koa.js | 89 ++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 162 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 98a97a1..403f8b9 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ The following configuration options are available: { theme: 'okaidia', // The prismjs theme to use disabledForXHR: true // Disable the middleware for XHR requests + excludeEnvVariables: [], // Name of environment variables to exclude disableSourceMapSupport: false // Disables support for sourcemaps } ``` diff --git a/index.js b/index.js index 8e22206..75c7cc5 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const defaultOptions = { disabledForXHR: true, disableSourceMapSupport: false, + excludeEnvVariables: [], theme: 'okaidia' }; diff --git a/src/main.js b/src/main.js index 71e94d3..f051824 100644 --- a/src/main.js +++ b/src/main.js @@ -115,8 +115,9 @@ function parseStack(opts, error) { return results.then(r => r.filter(i => i)); } -function getEnvironment() { - return util.toKeyValueList(process.env); +function getEnvironment(opts) { + const environment = util.toKeyValueList(process.env); + return environment.filter(v => opts.excludeEnvVariables.indexOf(v.key) < 0); } function getGlobals() { @@ -167,7 +168,7 @@ module.exports = function main(opts, error, req) { headers: getHeaders(req), request: getRequest(req), stack, - environment: getEnvironment(), + environment: getEnvironment(opts), globals: getGlobals(), process: getProcess() }; diff --git a/tests/express.js b/tests/express.js index 58827fb..7b1e319 100644 --- a/tests/express.js +++ b/tests/express.js @@ -10,23 +10,35 @@ const stack = require('../index').express; let app; let server; -beforeEach(done => { - app = express(); +function checkBody(body) { + const html = cheerio.load(body); - server = app.listen(3000, done); -}); + expect(html('title').text()).to.contain('Exception Page'); +} -afterEach(() => { - server.close(); -}); +function checkEnvVarsInclude(body, envVar) { + const html = cheerio.load(body); -function checkBody(body) { + expect(html('div.environment').text()).to.contain(envVar); +} + +function checkEnvVarsExclude(body, envVar) { const html = cheerio.load(body); - expect(html('title').text()).to.contain('Exception Page'); + expect(html('div.environment').text()).to.not.contain(envVar); } -describe('#stack', () => { +describe('#stack with default config', () => { + beforeEach(done => { + app = express(); + + server = app.listen(3000, done); + }); + + afterEach(() => { + server.close(); + }); + it('fails when NODE_ENV="production"', () => { process.env.NODE_ENV = 'production'; @@ -67,3 +79,59 @@ describe('#stack', () => { }); }); }); + +describe('#stack with custom excludeEnvVariables config', () => { + const testEnvVarName = 'TEST_ENV_VAR'; + + beforeEach(done => { + process.env[testEnvVarName] = 'Hello World'; + + app = express(); + server = app.listen(3000, done); + }); + + afterEach(() => { + server.close(); + }); + + it('has env variable when not excluded', done => { + app.use((req, res, next) => { + setImmediate(() => { + next(new TypeError('Hello World')); + }); + }); + app.use(stack()); + + request('http://localhost:3000/', (error, response, body) => { + expect(response.statusCode).to.equal(500); + + checkBody(body); + checkEnvVarsInclude(body, testEnvVarName); + done(); + }); + }); + + it('miss env variable when excluded', done => { + const excluded = []; + excluded.push(testEnvVarName); + + app.use((req, res, next) => { + setImmediate(() => { + next(new TypeError('Hello World')); + }); + }); + app.use( + stack({ + excludeEnvVariables: excluded + }) + ); + + request('http://localhost:3000/', (error, response, body) => { + expect(response.statusCode).to.equal(500); + + checkBody(body); + checkEnvVarsExclude(body, testEnvVarName); + done(); + }); + }); +}); diff --git a/tests/koa.js b/tests/koa.js index 7300e7e..0c70cad 100644 --- a/tests/koa.js +++ b/tests/koa.js @@ -10,25 +10,37 @@ const stack = require('../index').koa; let app; let server; -beforeEach(done => { - app = new Koa(); +function checkBody(body) { + const html = cheerio.load(body); - app.use(stack()); + expect(html('title').text()).to.contain('Exception Page'); +} - server = app.listen(3000, done); -}); +function checkEnvVarsInclude(body, envVar) { + const html = cheerio.load(body); -afterEach(() => { - server.close(); -}); + expect(html('div.environment').text()).to.contain(envVar); +} -function checkBody(body) { +function checkEnvVarsExclude(body, envVar) { const html = cheerio.load(body); - expect(html('title').text()).to.contain('Exception Page'); + expect(html('div.environment').text()).to.not.contain(envVar); } -describe('#stack', () => { +describe('#stack with default config', () => { + beforeEach(done => { + app = new Koa(); + + app.use(stack()); + + server = app.listen(3000, done); + }); + + afterEach(() => { + server.close(); + }); + it('fails when NODE_ENV="production"', () => { process.env.NODE_ENV = 'production'; @@ -52,3 +64,58 @@ describe('#stack', () => { }); }); }); + +describe('#stack with custom excludeEnvVariables config', () => { + const testEnvVarName = 'TEST_ENV_VAR'; + + beforeEach(done => { + app = new Koa(); + process.env[testEnvVarName] = 'Hello World'; + done(); + }); + + afterEach(() => { + server.close(); + }); + + it('has env variable when not excluded', done => { + app.use(stack()); + server = app.listen(3000); + + app.use(async () => { + throw new TypeError('Hello World'); + }); + + request('http://localhost:3000/', (error, response, body) => { + expect(response.statusCode).to.equal(500); + + checkBody(body); + checkEnvVarsInclude(body, testEnvVarName); + done(); + }); + }); + + it('miss env variable when excluded', done => { + const excluded = []; + excluded.push(testEnvVarName); + + app.use( + stack({ + excludeEnvVariables: excluded + }) + ); + server = app.listen(3000); + + app.use(async () => { + throw new TypeError('Hello World'); + }); + + request('http://localhost:3000/', (error, response, body) => { + expect(response.statusCode).to.equal(500); + + checkBody(body); + checkEnvVarsExclude(body, testEnvVarName); + done(); + }); + }); +});