Skip to content

Commit

Permalink
[tests] Added tests for send calling setRatelimitParser.
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmonkey committed May 3, 2018
1 parent dbf5845 commit 3e5b8e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
19 changes: 13 additions & 6 deletions index.js
Expand Up @@ -27,6 +27,7 @@ function Mana() {
this.fuse();

this.fnqueue = Object.create(null); // Callback queue.
this.ratelimitParser = null; // Optional function for parsing the rate limit information.
this.authHeader = 'Authorization'; // Default auth header
this.remaining = 0; // How many API calls are remaining.
this.ratelimit = 0; // The amount of API calls allowed.
Expand All @@ -39,6 +40,8 @@ function Mana() {
//
this.debug = diagnostics('mana:'+ this.name);

this.setRatelimit = this.setRatelimit.bind(this);

if ('function' === this.type(this.initialise)) this.initialise.apply(this, arguments);

//
Expand Down Expand Up @@ -549,7 +552,7 @@ Mana.prototype.setRatelimit = function setRatelimit(ratereset, ratelimit, remain
this.remaining = remaining;
this.debug('Only %d API request remaining', remaining);
}
}
};

/**
* Helper function for registering a rate limit parser
Expand All @@ -560,7 +563,7 @@ Mana.prototype.setRatelimitParser = function setRatelimitParser(ratelimitParser)
if(typeof ratelimitParser === 'function'){
this.ratelimitParser = ratelimitParser;
}
}
};

/**
* Gets the rate limit information from the response headers and sets it
Expand All @@ -574,7 +577,7 @@ Mana.prototype.ratelimitHeader = function ratelimitHeader(headers) {
, remaining = +headers['x-ratelimit-remaining'];

this.setRatelimit(ratereset, ratelimit, remaining);
}
};

/**
* Query against a given API endpoint.
Expand Down Expand Up @@ -727,6 +730,8 @@ Mana.prototype.send = function send(args) {
* @api private
*/
function parse(err, res, body) {
var hasRatelimitParser = mana.ratelimitParser && typeof mana.ratelimitParser === 'function';

mana.debug('Response headers %j', res && res.headers || {});
assign.emit('headers', res && res.headers || {});

Expand All @@ -750,9 +755,7 @@ Mana.prototype.send = function send(args) {
// rate limit, so make sure we parse that out before we start handling
// potential errors.
//
if(mana.ratelimitParser && typeof mana.ratelimitParser === 'function') {
mana.ratelimitParser(res, body, mana.setRatelimit.bind(this));
} else {
if (!hasRatelimitParser) {
mana.ratelimitHeader(res.headers);
}

Expand Down Expand Up @@ -857,6 +860,10 @@ Mana.prototype.send = function send(args) {
}
}

if (hasRatelimitParser) {
mana.ratelimitParser(res, data, mana.setRatelimit);
}

//
// Special case for 404 requests, it's technically not an error, but
// it's also not a valid response from the server so if we're going to
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -37,7 +37,9 @@
},
"devDependencies": {
"assume": "1.4.x",
"assume-sinon": "^1.0.0",
"mocha": "2.5.x",
"pre-commit": "1.1.x"
"pre-commit": "1.1.x",
"sinon": "^5.0.3"
}
}
32 changes: 32 additions & 0 deletions test.js
Expand Up @@ -2,12 +2,20 @@ describe('mana', function () {
'use strict';

var assume = require('assume')
, sinon = require('sinon')
, Mana = require('./')
, Token = Mana.Token;

assume.use(require('assume-sinon'));

var sandbox = sinon.sandbox.create();
var mana = new Mana();

describe('construction', function () {
afterEach(function () {
sandbox.reset();
});

it('is exposed as a function', function() {
assume(Mana).is.a('function');
});
Expand Down Expand Up @@ -180,6 +188,30 @@ describe('mana', function () {
});
});

describe('#send', function () {
var sendMana;

this.timeout(500);

beforeEach(function () {
sendMana = new Mana();
});

it('calls ratelimitParser with res, body, and setRatelimit', function (done) {
sendMana.setRatelimitParser(sandbox.stub());
sendMana.send(
['users', 'octocat', 'orgs'],
{ api: 'https://api.github.com/' },
function handler(err, body) {
if (err) return done(err);

assume(sendMana.ratelimitParser).is.calledWith(sinon.match.has('headers'), body, sendMana.setRatelimit);

done();
});
});
});

describe('tokenizer', function () {
it('prefixes tokens with a custom prefix', function () {
var NaNa = Mana.extend({
Expand Down

0 comments on commit 3e5b8e5

Please sign in to comment.