diff --git a/index.js b/index.js index ab8675b..cd477ee 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ module.exports = function (app, db) { } var lookups = opts.lookup.map(function (item) { return item + ':' + item.split('.').reduce(function (prev, cur) { - return prev[cur] + return prev ? prev[cur] : undefined }, req) }).join(':') var path = opts.path || req.path diff --git a/tests/index.js b/tests/index.js index a439dd4..7c3f240 100644 --- a/tests/index.js +++ b/tests/index.js @@ -153,6 +153,25 @@ describe('rate-limiter', function () { done(e) }) }) + + it('should work with undefined lookups', function (done) { + limiter({ + path: '/route', + method: 'get', + lookup: ['doesNotExist.remoteAddress', 'connection.doesNotExist'], + total: 0, + expire: 1000 * 60 * 60, + skipHeaders: true + }) + + app.get('/route', function (req, res) { + res.send(200, 'hello') + }) + + request(app) + .get('/route') + .expect(429, done) + }) }) context('direct middleware', function () {