Fix res.jsonp error if callback param is object

fixes #2104
This commit is contained in:
Douglas Christopher Wilson
2014-05-08 13:42:19 -04:00
parent c99fa6a192
commit d58ca520c8
3 changed files with 25 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
=====
* fix `req.host` for IPv6 literals
* fix `res.jsonp` error if callback param is object
3.5.2 / 2014-04-24
==================

View File

@@ -237,9 +237,13 @@ res.jsonp = function(obj){
this.charset = this.charset || 'utf-8';
this.set('Content-Type', 'application/json');
// fixup callback
if (Array.isArray(callback)) {
callback = callback[0];
}
// jsonp
if (callback) {
if (Array.isArray(callback)) callback = callback[0];
if (callback && 'string' === typeof callback) {
this.set('Content-Type', 'text/javascript');
var cb = callback.replace(/[^\[\]\w$.]/g, '');
body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';

View File

@@ -37,6 +37,22 @@ describe('res', function(){
})
})
it('should ignore object callback parameter with jsonp', function(done){
var app = express();
app.use(function(req, res){
res.jsonp({ count: 1 });
});
request(app)
.get('/?callback[a]=something')
.end(function(err, res){
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
res.text.should.equal('{"count":1}');
done();
})
})
it('should allow renaming callback', function(done){
var app = express();
@@ -212,7 +228,7 @@ describe('res', function(){
})
})
describe('.json(status, object)', function(){
describe('.jsonp(status, object)', function(){
it('should respond with json and set the .statusCode', function(done){
var app = express();
@@ -231,7 +247,7 @@ describe('res', function(){
})
})
describe('.json(object, status)', function(){
describe('.jsonp(object, status)', function(){
it('should respond with json and set the .statusCode for backwards compat', function(done){
var app = express();