From e218377a3d9d5c37790823f4e346a4f83b733e7d Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Tue, 15 Oct 2013 12:39:32 -0700 Subject: [PATCH] check existence of jsonp callback --- lib/response.js | 4 ++-- test/res.jsonp.js | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/response.js b/lib/response.js index ba6ff524..d37afd7a 100644 --- a/lib/response.js +++ b/lib/response.js @@ -242,7 +242,7 @@ res.jsonp = function(obj){ if (Array.isArray(callback)) callback = callback[0]; this.set('Content-Type', 'text/javascript'); var cb = callback.replace(/[^\[\]\w$.]/g, ''); - body = cb + ' && ' + cb + '(' + body + ');'; + body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');'; } return this.send(body); @@ -750,7 +750,7 @@ res.vary = function(field){ var vary = this.get('Vary'); - // append + // append if (vary) { vary = vary.split(/ *, */); if (!~vary.indexOf(field)) vary.push(field); diff --git a/test/res.jsonp.js b/test/res.jsonp.js index 30e6c01d..76f07c1f 100644 --- a/test/res.jsonp.js +++ b/test/res.jsonp.js @@ -16,7 +16,7 @@ describe('res', function(){ .get('/?callback=something') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.text.should.equal('something && something({"count":1});'); + res.text.should.equal('typeof something === \'function\' && something({"count":1});'); done(); }) }) @@ -32,7 +32,7 @@ describe('res', function(){ .get('/?callback=something&callback=somethingelse') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.text.should.equal('something && something({"count":1});'); + res.text.should.equal('typeof something === \'function\' && something({"count":1});'); done(); }) }) @@ -50,10 +50,10 @@ describe('res', function(){ .get('/?clb=something') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.text.should.equal('something && something({"count":1});'); + res.text.should.equal('typeof something === \'function\' && something({"count":1});'); done(); }) - }) + }) it('should allow []', function(done){ var app = express(); @@ -66,7 +66,7 @@ describe('res', function(){ .get('/?callback=callbacks[123]') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.text.should.equal('callbacks[123] && callbacks[123]({"count":1});'); + res.text.should.equal('typeof callbacks[123] === \'function\' && callbacks[123]({"count":1});'); done(); }) }) @@ -82,7 +82,7 @@ describe('res', function(){ .get('/?callback=foo;bar()') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); - res.text.should.equal('foobar && foobar({});'); + res.text.should.equal('typeof foobar === \'function\' && foobar({});'); done(); }) }) @@ -98,7 +98,7 @@ describe('res', function(){ .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"});'); + res.text.should.equal('typeof foo === \'function\' && foo({"str":"\\u2028 \\u2029 woot"});'); done(); }); }); @@ -138,7 +138,7 @@ describe('res', function(){ }) }) }) - + describe('when given an object', function(){ it('should respond with json', function(done){ var app = express(); @@ -211,7 +211,7 @@ describe('res', function(){ }) }) }) - + describe('.json(status, object)', function(){ it('should respond with json and set the .statusCode', function(done){ var app = express();