From a9175dad58507059fa8702f2e60bc2b937f95c2e Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 8 Sep 2015 21:06:57 -0700 Subject: [PATCH] [transport] before failing to send a request, ensure the return value is defined --- grunt/tasks.js | 9 ++------- src/lib/transport.js | 25 ++++++++++++------------- test/unit/specs/transport.js | 23 ++++++++++++++++++++++- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/grunt/tasks.js b/grunt/tasks.js index 3370b9a5a..b985a209c 100644 --- a/grunt/tasks.js +++ b/grunt/tasks.js @@ -32,12 +32,7 @@ module.exports = function (grunt) { grunt.task.run(tasks); }); - grunt.registerTask('unit_test', [ - 'jshint', - 'run:generate', - 'mochacov:unit', - ]); - + grunt.registerTask('unit_test', 'mochacov:unit'); grunt.registerTask('coverage', [ 'mochacov:make_coverage_html', 'open:coverage' @@ -84,4 +79,4 @@ module.exports = function (grunt) { }) .nodeify(this.async()); }); -}; \ No newline at end of file +}; diff --git a/src/lib/transport.js b/src/lib/transport.js index 2c557a778..8ee6f41dc 100644 --- a/src/lib/transport.js +++ b/src/lib/transport.js @@ -131,9 +131,20 @@ Transport.prototype.request = function (params, cb) { self.log.debug('starting request', params); + // determine the response based on the presense of a callback + if (typeof cb === 'function') { + ret = { + abort: abortRequest + }; + } else { + defer = this.defer(); + ret = defer.promise; + ret.abort = abortRequest; + } + if (params.body && params.method === 'GET') { _.nextTick(respond, new TypeError('Body can not be sent with method "GET"')); - return; + return ret; } // serialize the body @@ -287,18 +298,6 @@ Transport.prototype.request = function (params, cb) { }, requestTimeout); } - // determine the response based on the presense of a callback - if (typeof cb === 'function') { - ret = { - abort: abortRequest - }; - } else { - defer = this.defer(); - ret = defer.promise; - ret.abort = abortRequest; - } - - if (connection) { sendReqWithConnection(void 0, connection); } else { diff --git a/test/unit/specs/transport.js b/test/unit/specs/transport.js index 1e5ca4746..7501306f9 100644 --- a/test/unit/specs/transport.js +++ b/test/unit/specs/transport.js @@ -360,7 +360,8 @@ describe('Transport Class', function () { done(); }); }); - it('rejects get requests with bodies', function (done) { + + it('rejects GET requests with a body (callback)', function (done) { var trans = new Transport(); stub(trans.log, 'debug'); stub(trans.connectionPool, 'select', function (cb) { @@ -377,6 +378,26 @@ describe('Transport Class', function () { }); }); + it('rejects GET requests with a body (promise)', function (done) { + var trans = new Transport(); + stub(trans.log, 'debug'); + stub(trans.connectionPool, 'select', function (cb) { + // simulate "no connections" + process.nextTick(cb); + }); + trans.request({ + body: 'JSON!!', + method: 'GET' + }) + .then(function () { + done(new Error('expected the request to fail!')); + }, function (err) { + expect(err).to.be.a(TypeError); + expect(err.message).to.match(/body.*method.*get/i); + done(); + }); + }); + describe('gets a body', function () { it('serializes it', function (done) { var trans = new Transport({