From 356b4735bb51d9d2201e9eb9c670d187ca5b70bb Mon Sep 17 00:00:00 2001 From: Jared Hanson Date: Mon, 27 Feb 2017 14:06:44 -0800 Subject: [PATCH] Add support for testing complete callback. --- lib/test.js | 12 +++++++--- test/test.end.test.js | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/lib/test.js b/lib/test.js index 1b7e6fc..467a8d2 100644 --- a/lib/test.js +++ b/lib/test.js @@ -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() { @@ -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) { diff --git a/test/test.end.test.js b/test/test.end.test.js index 3d9dc39..a4b7b65 100644 --- a/test/test.end.test.js +++ b/test/test.end.test.js @@ -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 = {};