Skip to content

Commit

Permalink
Add support for testing complete callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Feb 27, 2017
1 parent 473225b commit 356b473
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/test.js
Expand Up @@ -134,10 +134,11 @@ Test.prototype.authorize = function() {
*
* @api public
*/
Test.prototype.decide = function(err) {
Test.prototype.decide = function(complete) {
var self = this
, txn = { protocol: 'oauth2' }
, before = this._txn;
, before = this._txn
, complete = complete || function(cb){ return cb(); }

function ready() {
var res = new Response(function() {
Expand All @@ -153,7 +154,12 @@ Test.prototype.decide = function(err) {
}

var fn = self._mod.response;
fn(txn, res, next);
var arity = fn.length;
if (arity == 4) {
fn(txn, res, complete, next);
} else {
fn(txn, res, next);
}
}

if (before && before.length == 2) {
Expand Down
54 changes: 54 additions & 0 deletions test/test.end.test.js
Expand Up @@ -39,6 +39,60 @@ describe('test grant that calls end', function() {
});


describe('test grant that calls complete callback and then end', function() {

var grant = {};
grant.request = function(req) {};
grant.response = function(txn, res, complete, next) {
complete(function() {
res.end('Hello');
});
};

describe('with an complete callback', function() {
var res, completed;

before(function(done) {
var test = new Test(grant);
test.end(function(r) {
res = r;
done();
}).decide(function(cb) {
completed = true;
cb();
});
});

it('should call complete', function() {
expect(completed).to.be.true;
});

it('should call end callback', function() {
expect(res.statusCode).to.be.equal(200);
expect(res.body).to.be.equal('Hello');
});
});

describe('without an complete callback', function() {
var res, completed;

before(function(done) {
var test = new Test(grant);
test.end(function(r) {
res = r;
done();
}).decide();
});

it('should call end callback', function() {
expect(res.statusCode).to.be.equal(200);
expect(res.body).to.be.equal('Hello');
});
});

});


describe('test grant that calls end after error', function() {

var grant = {};
Expand Down

0 comments on commit 356b473

Please sign in to comment.