Skip to content
Myr

Fix res.jsonp error if callback param is object

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes #2104

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent c99fa6a · Browse files · 3 files+25 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+25 added−4 removed

  • tests:+18 added−2 removed
  • other:+7 added−2 removed

3 files+25 added−4 removed

modified:lib/response.js+6 added-2 removed
235 unmodified lines
236 // content-type237 this.charset = this.charset || 'utf-8';238 this.set('Content-Type', 'application/json');239240 // fixup callback241 if (Array.isArray(callback)) {242 callback = callback[0];243 }244245 // jsonp241 if (callback) {242 if (Array.isArray(callback)) callback = callback[0];246 if (callback && 'string' === typeof callback) {247 this.set('Content-Type', 'text/javascript');248 var cb = callback.replace(/[^\[\]\w$.]/g, '');249 body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';250 }
553 unmodified lines
modified:test/res.jsonp.js+18 added-2 removed
35 unmodified lines
36 done();37 })38 })3940 it('should ignore object callback parameter with jsonp', function(done){41 var app = express();4243 app.use(function(req, res){44 res.jsonp({ count: 1 });45 });4647 request(app)48 .get('/?callback[a]=something')49 .end(function(err, res){50 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');51 res.text.should.equal('{"count":1}');52 done();53 })54 })5556 it('should allow renaming callback', function(done){57 var app = express();5859 app.set('jsonp callback name', 'clb');
167 unmodified lines
227 })228 })229 })230215 describe('.json(status, object)', function(){231 describe('.jsonp(status, object)', function(){232 it('should respond with json and set the .statusCode', function(done){233 var app = express();234235 app.use(function(req, res){
10 unmodified lines
246 })247 })248 })249234 describe('.json(object, status)', function(){250 describe('.jsonp(object, status)', function(){251 it('should respond with json and set the .statusCode for backwards compat', function(done){252 var app = express();253254 app.use(function(req, res){
14 unmodified lines
modified:History.md+1 added
13.5.x2=====34 * fix `req.host` for IPv6 literals5 * fix `res.jsonp` error if callback param is object673.5.2 / 2014-04-248==================9
1274 unmodified lines