Permalink
Browse files

fix jsonp whitespace escape. Closes #1132

  • Loading branch information...
1 parent 33eaa83 commit 64a234958a9704bb68793fbed30b2d2a00a4285a @rauchg rauchg committed with tj Dec 28, 2012
Showing with 19 additions and 1 deletion.
  1. +3 −1 lib/response.js
  2. +16 −0 test/res.jsonp.js
View
@@ -223,7 +223,9 @@ res.jsonp = function(obj){
var app = this.app;
var replacer = app.get('json replacer');
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')];
// content-type
View
@@ -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(){
it('should respond with json', function(done){
var app = express();

0 comments on commit 64a2349

Please sign in to comment.