Merge pull request #1779 from visionmedia/jsonp-typeof-callback
Before adoption: this commit came in with the history the project adopted, so no landing records it.
check existence of jsonp callback
authored by Jonathan Ong · committed by Jonathan Ong · parents 7d1aed4, e218377 · Browse files · 2 files·+11 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+11 added−11 removed
- tests:+9 added−9 removed
- other:+2 added−2 removed
Provenance
- Commit
e8373d3- Tree
6f545ec- Author
Jonathan Ong <jonathanrichardong@gmail.com>- Committer
Jonathan Ong <jonathanrichardong@gmail.com>- Parent 1
7d1aed4- Parent 2
e218377
2 files changed+11 added−11 removed
modified:lib/response.js+2 added-2 removed
240 unmodified lines241 if (callback) {242 if (Array.isArray(callback)) callback = callback[0];243 this.set('Content-Type', 'text/javascript');244 var cb = callback.replace(/[^\[\]\w$.]/g, '');245- body = cb + ' && ' + cb + '(' + body + ');';245+ body = 'typeof ' + cb + ' === \'function\' && ' + cb + '(' + body + ');';246 }247 248 return this.send(body);249 };499 unmodified lines749 }750 751 var vary = this.get('Vary');752 753- // append 753+ // append754 if (vary) {755 vary = vary.split(/ *, */);756 if (!~vary.indexOf(field)) vary.push(field);757 this.set('Vary', vary.join(', '));47 unmodified linesmodified:test/res.jsonp.js+9 added-9 removed
14 unmodified lines15 request(app)16 .get('/?callback=something')17 .end(function(err, res){18 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');19- res.text.should.equal('something && something({"count":1});');19+ res.text.should.equal('typeof something === \'function\' && something({"count":1});');20 done();21 })22 })23 7 unmodified lines31 request(app)32 .get('/?callback=something&callback=somethingelse')33 .end(function(err, res){34 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');35- res.text.should.equal('something && something({"count":1});');35+ res.text.should.equal('typeof something === \'function\' && something({"count":1});');36 done();37 })38 })39 9 unmodified lines49 request(app)50 .get('/?clb=something')51 .end(function(err, res){52 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');53- res.text.should.equal('something && something({"count":1});');53+ res.text.should.equal('typeof something === \'function\' && something({"count":1});');54 done();55 })56- }) 56+ })57 58 it('should allow []', function(done){59 var app = express();60 4 unmodified lines65 request(app)66 .get('/?callback=callbacks[123]')67 .end(function(err, res){68 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');69- res.text.should.equal('callbacks[123] && callbacks[123]({"count":1});');69+ res.text.should.equal('typeof callbacks[123] === \'function\' && callbacks[123]({"count":1});');70 done();71 })72 })73 7 unmodified lines81 request(app)82 .get('/?callback=foo;bar()')83 .end(function(err, res){84 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');85- res.text.should.equal('foobar && foobar({});');85+ res.text.should.equal('typeof foobar === \'function\' && foobar({});');86 done();87 })88 })89 7 unmodified lines97 request(app)98 .get('/?callback=foo')99 .end(function(err, res){100 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');101- res.text.should.equal('foo && foo({"str":"\\u2028 \\u2029 woot"});');101+ res.text.should.equal('typeof foo === \'function\' && foo({"str":"\\u2028 \\u2029 woot"});');102 done();103 });104 });105 31 unmodified lines137 done();138 })139 })140 })141- 141+142 describe('when given an object', function(){143 it('should respond with json', function(done){144 var app = express();145 64 unmodified lines210 });211 })212 })213 })214- 214+215 describe('.json(status, object)', function(){216 it('should respond with json and set the .statusCode', function(done){217 var app = express();218 34 unmodified lines