Skip to content

Commit

Permalink
Bugfix regarding scope
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Sep 21, 2015
1 parent 8594b93 commit 13c666b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/server/core/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Rule.prototype._formatScope = function (scope) {
var scopeStrings = [];

for ( var i = 0; i < this.options.scope.length; i+=1 ) {
scopeStrings.push(String(scope[this.options.scope[i]]).replace(/\|/g, '||'));
scopeStrings.push(String(scope ? scope[this.options.scope[i]] : undefined).replace(/\|/g, '||'));
}

return scopeStrings.join('|');
Expand Down
88 changes: 88 additions & 0 deletions test/spec/local-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,41 @@ describe('Local Server', function () {

});

it('with 1 rule and scope parameter variants', function () {

var quotaManager = new quota.Manager();
quotaManager.addRule({
limit: 2,
throttling: 'limit-absolute',
scope: 'id'
});

var quotaServer = new quota.Server();
quotaServer.addManager('test', quotaManager);

var quotaClient = new quota.Client(quotaServer);

return BPromise.resolve()
.then(function () {
return quotaClient.requestQuota('test', undefined);
})
.then(function () {
return quotaClient.requestQuota('test', { });
})
.then(function () {

return quotaClient.requestQuota('test', { id: undefined })
.then(function () {
throw new Error('Expected OutOfQuotaError');
})
.catch(quota.OutOfQuotaError, function (err) {
return; // Expected
});

});

});

it('with 1 rule and complex scope', function () {

var quotaManager = new quota.Manager();
Expand Down Expand Up @@ -496,4 +531,57 @@ describe('Local Server', function () {

});

describe('with two managers', function () {

it('should find the right manager', function () {

var quotaManager1 = new quota.Manager();
quotaManager1.addRule({
limit: 1,
throttling: 'limit-absolute'
});

var quotaManager2 = new quota.Manager();
quotaManager2.addRule({
limit: 1,
throttling: 'limit-absolute'
});

var quotaServer = new quota.Server();
quotaServer.addManager('man1', quotaManager1);
quotaServer.addManager('man2', quotaManager2);

var quotaClient = new quota.Client(quotaServer);

return BPromise.resolve()
.then(function () {
return quotaClient.requestQuota('man1');
})
.then(function () {
return quotaClient.requestQuota('man1')
.then(function () {
throw new Error('Expected OutOfQuotaError');
})
.catch(quota.OutOfQuotaError, function (err) {
return; // Expected
});
})
.then(function () {
return quotaClient.requestQuota('man2');
})
.then(function () {
return quotaClient.requestQuota('man2')
.then(function () {
throw new Error('Expected OutOfQuotaError');
})
.catch(quota.OutOfQuotaError, function (err) {
return; // Expected
});
});


});

});

});

0 comments on commit 13c666b

Please sign in to comment.