diff --git a/lib/response.js b/lib/response.js index 8b999b0b..d9e8d131 100644 --- a/lib/response.js +++ b/lib/response.js @@ -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); diff --git a/test/res.jsonp.js b/test/res.jsonp.js index 32261f52..6efd5f94 100644 --- a/test/res.jsonp.js +++ b/test/res.jsonp.js @@ -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(); }) })