Skip to content

Commit fda6efe

Browse files
committed
Added .end() callback arity check to node side
1 parent 6ca7800 commit fda6efe

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/node/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function Request(method, url) {
121121
this._buffer = true;
122122
this.attachments = [];
123123
this.on('response', function(res){
124-
self.callback(res);
124+
self.callback(null, res);
125125
});
126126
}
127127

@@ -380,7 +380,7 @@ Request.prototype.redirect = function(res){
380380
this._data = null;
381381
this.url = url;
382382
this.emit('redirect', res);
383-
this.end(this.callback);
383+
this.end(this._callback);
384384
return this;
385385
};
386386

@@ -468,6 +468,21 @@ Request.prototype.request = function(){
468468
return req;
469469
};
470470

471+
/**
472+
* Invoke the callback with `err` and `res`
473+
* and handle arity check.
474+
*
475+
* @param {Error} err
476+
* @param {Response} res
477+
* @api private
478+
*/
479+
480+
Request.prototype.callback = function(err, res){
481+
var fn = this._callback;
482+
if (2 == fn.length) return fn(err, res);
483+
fn(res); // TODO: emit error
484+
};
485+
471486
/**
472487
* Initiate request, invoking callback `fn(err, res)`
473488
* with an instanceof `Response`.
@@ -485,7 +500,7 @@ Request.prototype.end = function(fn){
485500
, method = this.method;
486501

487502
// store callback
488-
this.callback = fn || noop;
503+
this._callback = fn || noop;
489504

490505
// body
491506
switch (method) {
@@ -536,7 +551,7 @@ Request.prototype.end = function(fn){
536551
response.body = fields;
537552
response.files = files;
538553
self.emit('end');
539-
self.callback(response);
554+
self.callback(null, response);
540555
});
541556
return;
542557
}

test/node/basic.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,17 @@ describe('request', function(){
284284
})
285285
})
286286
})
287+
288+
describe('.end(fn)', function(){
289+
it('should check arity', function(done){
290+
request
291+
.post('http://localhost:3000/echo')
292+
.send({ name: 'tobi' })
293+
.end(function(err, res){
294+
assert(null == err);
295+
res.text.should.equal('{"name":"tobi"}');
296+
done();
297+
});
298+
})
299+
})
287300
})

0 commit comments

Comments
 (0)