Skip to content

Commit

Permalink
fix jsonp whitespace escape. Closes #1132
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg authored and tj committed Dec 28, 2012
1 parent 33eaa83 commit 64a2349
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/response.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ res.jsonp = function(obj){
var app = this.app; var app = this.app;
var replacer = app.get('json replacer'); var replacer = app.get('json replacer');
var spaces = app.get('json spaces'); var spaces = app.get('json spaces');
var body = JSON.stringify(obj, replacer, spaces); var body = JSON.stringify(obj, replacer, spaces)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
var callback = this.req.query[app.get('jsonp callback name')]; var callback = this.req.query[app.get('jsonp callback name')];


// content-type // content-type
Expand Down
16 changes: 16 additions & 0 deletions test/res.jsonp.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ describe('res', function(){
}) })
}) })


it('should escape utf whitespace', function(done){
var app = express();

app.use(function(req, res){
res.jsonp({ str: '\u2028 \u2029 woot' });
});

request(app)
.get('/?callback=foo')
.end(function(err, res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.text.should.equal('foo && foo({"str":"\\u2028 \\u2029 woot"});');
done();
});
});

describe('when given primitives', function(){ describe('when given primitives', function(){
it('should respond with json', function(done){ it('should respond with json', function(done){
var app = express(); var app = express();
Expand Down

0 comments on commit 64a2349

Please sign in to comment.