Skip to content

Commit

Permalink
Added: allow [] in jsonp callback. Closes expressjs#1128
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed May 9, 2012
1 parent 4de95c0 commit df2584c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ res.json = function(obj){

if (callback && jsonp) {
this.set('Content-Type', 'text/javascript');
body = callback.replace(/[^\w$.]/g, '') + '(' + body + ');';
body = callback.replace(/[^[]\w$.]/g, '') + '(' + body + ');';
}

return this.send(body);
Expand Down
17 changes: 16 additions & 1 deletion test/res.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('res', function(){
it('should respond with jsonp', function(done){
var app = express();

// app.enable('jsonp callback');
app.use(function(req, res){
res.json({ count: 1 });
});
Expand All @@ -22,6 +21,22 @@ describe('res', function(){
done();
})
})

it('should allow []', function(done){
var app = express();

app.use(function(req, res){
res.json({ count: 1 });
});

request(app)
.get('/?callback=callbacks[123]')
.end(function(res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.body.should.equal('callbacks[123]({"count":1});');
done();
})
})
})

describe('when given primitives', function(){
Expand Down

0 comments on commit df2584c

Please sign in to comment.