diff --git a/lib/configproxy.js b/lib/configproxy.js index 07fa08a8..38be5470 100644 --- a/lib/configproxy.js +++ b/lib/configproxy.js @@ -208,9 +208,9 @@ ConfigurableProxy.prototype.remove_route = function (path, cb) { var routes = this._routes; routes.hasRoute(path, function (result) { - if (result) { - routes.remove(path, cb); - } + if (result) { + routes.remove(path, cb); + } }); }; @@ -221,7 +221,7 @@ ConfigurableProxy.prototype.get_routes = function (req, res) { var inactive_since = null; if (parsed.query) { var query = querystring.parse(parsed.query); - + if (query.inactive_since !== undefined) { var timestamp = Date.parse(query.inactive_since); if (isFinite(timestamp)) { @@ -238,16 +238,15 @@ ConfigurableProxy.prototype.get_routes = function (req, res) { var results = {}; if (inactive_since) { - var keys = Object.keys(routes).filter(function (key) { - return routes[key].last_activity < inactive_since; + Object.keys(routes).forEach(function (path) { + if (routes[path].last_activity < inactive_since) { + results[path] = routes[path]; + } }); - - keys.forEach(function (key) { results[key] = routes[key]; }); } else { results = routes; } - res.write(JSON.stringify(results)); res.end(); that.statsd.increment('api.route.get', 1); @@ -300,8 +299,15 @@ ConfigurableProxy.prototype.target_for_req = function (req, cb) { this._routes.getTarget(base_path + decodeURIComponent(req.url), function (route) { timer.stop(); - var result = route ? { prefix: route.prefix, target: route.data.target } : null; - cb(result); + if (route) { + cb({ + prefix: route.prefix, + target: route.data.target + }); + return; + } + + cb(null); }); }; diff --git a/lib/store.js b/lib/store.js index 870f453f..d99777af 100644 --- a/lib/store.js +++ b/lib/store.js @@ -62,12 +62,7 @@ function MemoryStore () { }, update: { value: function (path, data, cb) { - for (var key in data) { - if (data.hasOwnProperty(key)) { - routes[path][key] = data[key]; - } - } - + Object.assign(routes[path], data); this.notify(cb); } }, diff --git a/test/api_spec.js b/test/api_spec.js index b8258362..60ce9f30 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -181,16 +181,31 @@ describe("API Tests", function () { var port = 8998; var path = '/yesterday'; - var now = new Date(); - var yesterday = new Date(now.getTime() - (24 * 3.6e6)); - var long_ago = new Date(1); - var hour_ago = new Date(now.getTime() - 3.6e6); + var now = new Date(); + var yesterday = new Date(now.getTime() - (24 * 3.6e6)); + var long_ago = new Date(1); + var hour_ago = new Date(now.getTime() - 3.6e6); var hour_from_now = new Date(now.getTime() + 3.6e6); var tests = [ - { name: 'long ago', since: long_ago, expected: {} }, - { name: 'an hour ago', since: hour_ago, expected: { '/yesterday': true } }, - { name: 'the future', since: hour_from_now, expected: { '/yesterday': true, '/today': true } } + { + name: 'long ago', + since: long_ago, + expected: {} + }, + { + name: 'an hour ago', + since: hour_ago, + expected: {'/yesterday': true} + }, + { + name: 'the future', + since: hour_from_now, + expected: { + '/yesterday': true, + '/today': true + } + } ]; var seen = 0;