diff --git a/lib/filters/index.js b/lib/filters/index.js index 3557f9183..6f692f345 100644 --- a/lib/filters/index.js +++ b/lib/filters/index.js @@ -5,7 +5,6 @@ const replace = require('../replace-vars'); // reads config that defines module.exports = ruleSource => { const debug = require('debug')('broker:' + (process.env.BROKER_TYPE || 'filter')); - debug('loading new rules'); let rules = []; const config = require('../config'); @@ -21,10 +20,13 @@ module.exports = ruleSource => { } } + if (!Array.isArray(rules)) { throw new Error(`unsupported type (${typeof rules}) for filter rules`); } + debug('loading %s new rules', rules.length); + // array of entries with const tests = rules.map(entry => { const keys = []; diff --git a/lib/index.js b/lib/index.js index c1b09bee1..b459c4a87 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,9 @@ const app = module.exports = { function main({ port, client } = {}) { // note: the config is loaded in the main function to allow us to mock in tests + if (process.env.TAP) { + delete require.cache[require.resolve('./config.js')]; + } const config = require('./config'); if (client === undefined) { client = !!config.brokerUrl; @@ -17,9 +20,13 @@ function main({ port, client } = {}) { const method = client ? 'client' : 'server'; process.env.BROKER_TYPE = method; + const debug = require('debug')(`broker:${method}`); + + debug(config.accept); + let filters = {}; if (config.accept) { - require('debug')(`broker:${method}`)('loading rules from %s', config.accept); + debug('loading rules from %s', config.accept); filters = require(path.resolve(process.cwd(), config.accept)); } diff --git a/lib/relay.js b/lib/relay.js index 76db631ea..c043e85cd 100644 --- a/lib/relay.js +++ b/lib/relay.js @@ -51,7 +51,7 @@ function responseHandler(filterRules) { if (error) { debug('blocked %s %s', method, url); return emit({ - status: 400, + status: 401, body: error.message, }); } @@ -88,9 +88,13 @@ function responseHandler(filterRules) { } } - // TEST remove x-forwarded headers - delete headers['x-forwarded-for']; - delete headers['x-forwarded-proto']; + // remove headers that we don't want to relay + // (because they corrupt the request) + [ + 'x-forwarded-for', + 'x-forwarded-proto', + 'content-length', + ].map(_ => delete headers[_]); request({ url: result, diff --git a/test/fixtures/server/.keep b/test/fixtures/server/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/functional/client-server.test.js b/test/functional/client-server.test.js index f3a7dcc56..e8de55771 100644 --- a/test/functional/client-server.test.js +++ b/test/functional/client-server.test.js @@ -6,7 +6,7 @@ const request = require('request'); const app = require('../../lib'); const root = __dirname; -const { port, localPort: servicePort, resetConfig } = require('../utils')(tap); +const { port, localPort: servicePort } = require('../utils')(tap); test('internal sends request through client', t => { @@ -22,10 +22,9 @@ test('internal sends request through client', t => { process.env.BROKER_URL = `http://localhost:${serverPort}`; process.env.BROKER_ID = '12345'; process.env.BROKER_TYPE = 'client'; - const localPort = port(); + const clientPort = port(); // invalidate the config require - resetConfig(); - const client = app.main({ port: localPort }); + const client = app.main({ port: clientPort }); // wait for the client to successfully connect to the server and identify itself server.io.once('connection', socket => { @@ -33,7 +32,7 @@ test('internal sends request through client', t => { t.plan(2); t.test('client can forward requests FROM internal service', t => { - const url = `http://localhost:${localPort}/service/test1`; + const url = `http://localhost:${clientPort}/service/test1`; request({ url, method: 'get', json: true }, (err, res) => { t.equal(res.statusCode, 200, '200 statusCode'); t.equal(res.body, 'test1', 'body correct'); diff --git a/test/functional/index.test.js b/test/functional/index.test.js index 6161a1e14..74d7a1c78 100644 --- a/test/functional/index.test.js +++ b/test/functional/index.test.js @@ -30,8 +30,6 @@ test('simple end to end proxying', t => { process.env.BROKER_URL = `http://localhost:${serverPort}`; process.env.BROKER_ID = '12345'; process.env.BROKER_TYPE = 'client'; - // invalidate the config require - delete require.cache[require.resolve(__dirname + '/../../lib/config.js')]; const client = app.main({ port: port() }); // wait for the client to successfully connect to the server and identify itself @@ -53,7 +51,7 @@ test('simple end to end proxying', t => { const url = `http://localhost:${serverPort}/broker/${id}/magic-path/x/random.json`; request({ url, 'method': 'post', json: true }, (err, res, body) => { t.equal(res.statusCode, 401, '401 statusCode'); - t.match(body, /blocked/, '"blocked" body: ' + body); + t.equal(body, 'blocked', '"blocked" body: ' + body); t.end(); }); });