add cb && cb(payload) to res.jsonp(). Closes #1374

This commit is contained in:
TJ Holowaychuk
2012-10-16 11:57:37 -07:00
parent 44d0625e91
commit b3936b96e5
2 changed files with 6 additions and 5 deletions

View File

@@ -234,7 +234,8 @@ res.jsonp = function(obj){
// jsonp
if (callback) {
this.set('Content-Type', 'text/javascript');
body = callback.replace(/[^\[\]\w$.]/g, '') + '(' + body + ');';
var cb = callback.replace(/[^\[\]\w$.]/g, '');
body = cb + ' && ' + cb + '(' + body + ');';
}
return this.send(body);

View File

@@ -16,7 +16,7 @@ describe('res', function(){
.get('/?callback=something')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.text.should.equal('something({"count":1});');
res.text.should.equal('something && something({"count":1});');
done();
})
})
@@ -34,7 +34,7 @@ describe('res', function(){
.get('/?clb=something')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.text.should.equal('something({"count":1});');
res.text.should.equal('something && something({"count":1});');
done();
})
})
@@ -50,7 +50,7 @@ describe('res', function(){
.get('/?callback=callbacks[123]')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.text.should.equal('callbacks[123]({"count":1});');
res.text.should.equal('callbacks[123] && callbacks[123]({"count":1});');
done();
})
})
@@ -66,7 +66,7 @@ describe('res', function(){
.get('/?callback=foo;bar()')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.text.should.equal('foobar({});');
res.text.should.equal('foobar && foobar({});');
done();
})
})