add res.jsonp(). Closes #1307
Before adoption: this commit came in with the history the project adopted, so no landing records it.
this also removes the jsonp
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent cdb3e9d · Browse files · 4 files·+287 added−76 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+287 added−76 removed
- tests:+240 added−69 removed
- other:+47 added−7 removed
Provenance
- Commit
a6caa26- Tree
2106b8d- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
cdb3e9d
4 files changed+287 added−76 removed
modified:lib/response.js+47 added-7 removed
181 unmodified lines182 }183 }184 185 // settings186- var app = this.app187- , jsonp = app.get('jsonp callback')188- , replacer = app.get('json replacer')189- , spaces = app.get('json spaces')190- , body = JSON.stringify(obj, replacer, spaces)191- , callback = this.req.query[app.get('jsonp callback name')];186+ var app = this.app;187+ var replacer = app.get('json replacer');188+ var spaces = app.get('json spaces');189+ var body = JSON.stringify(obj, replacer, spaces);190 191 // content-type192 this.charset = this.charset || 'utf-8';193 this.set('Content-Type', 'application/json');194 195+ return this.send(body);196+};197+198+/**199+ * Send JSON response with JSONP callback support.200+ *201+ * Examples:202+ *203+ * res.jsonp(null);204+ * res.jsonp({ user: 'tj' });205+ * res.jsonp(500, 'oh noes!');206+ * res.jsonp(404, 'I dont have that');207+ *208+ * @param {Mixed} obj or status209+ * @param {Mixed} obj210+ * @return {ServerResponse}211+ * @api public212+ */213+214+res.jsonp = function(obj){215+ // allow status / body216+ if (2 == arguments.length) {217+ // res.json(body, status) backwards compat218+ if ('number' == typeof arguments[1]) {219+ this.statusCode = arguments[1];220+ } else {221+ this.statusCode = obj;222+ obj = arguments[1];223+ }224+ }225+226+ // settings227+ var app = this.app;228+ var replacer = app.get('json replacer');229+ var spaces = app.get('json spaces');230+ var body = JSON.stringify(obj, replacer, spaces);231+ var callback = this.req.query[app.get('jsonp callback name')];232+233+ // content-type234+ this.charset = this.charset || 'utf-8';235+ this.set('Content-Type', 'application/json');236+ 237 // jsonp198- if (callback && jsonp) {238+ if (callback) {239 this.set('Content-Type', 'text/javascript');240 body = callback.replace(/[^\[\]\w$.]/g, '') + '(' + body + ');';241 }242 477 unmodified linesmodified:test/res.json.js+8 added-69 removed
3 unmodified lines4567891089101211121314141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273151617747576777818192021145 unmodified linesExpand all3 unmodified lines , assert = require('assert');
describe('res', function(){ describe('.json(object)', function(){ describe('when "jsonp callback" is enabled', function(){ it('should respond with jsonp', function(done){ var app = express(); it('should not support jsonp callbacks', function(done){ var app = express();
app.enable('jsonp callback'); app.use(function(req, res){ res.json({ foo: 'bar' }); });
app.use(function(req, res){ res.json({ count: 1 }); });
request(app) .get('/?callback=something') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); res.text.should.equal('something({"count":1});'); done(); }) })
it('should allow renaming callback', function(done){ var app = express();
app.enable('jsonp callback'); app.set('jsonp callback name', 'clb');
app.use(function(req, res){ res.json({ count: 1 }); });
request(app) .get('/?clb=something') .end(function(err, res){ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); res.text.should.equal('something({"count":1});'); done(); }) }) it('should allow []', function(done){ var app = express();
app.enable('jsonp callback'); app.use(function(req, res){ res.json({ count: 1 }); });
request(app) .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]({"count":1});'); done(); }) })
it('should disallow arbitrary js', function(done){ var app = express();
app.enable('jsonp callback'); app.use(function(req, res){ res.json({}); });
request(app) .get('/?callback=foo;bar()') .end(function(err, res){ request(app) .get('/?callback=foo') .expect('{"foo":"bar"}', done); res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8'); res.text.should.equal('foobar({});'); done(); }) }) })
describe('when given primitives', function(){ it('should respond with json', function(done){145 unmodified linesExpand alladded:test/res.jsonp.js+220 added
1+2+var express = require('../')3+ , request = require('./support/http')4+ , assert = require('assert');5+6+describe('res', function(){7+ describe('.jsonp(object)', function(){8+ it('should respond with jsonp', function(done){9+ var app = express();10+11+ app.use(function(req, res){12+ res.jsonp({ count: 1 });13+ });14+15+ 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({"count":1});');20+ done();21+ })22+ })23+24+ it('should allow renaming callback', function(done){25+ var app = express();26+27+ app.set('jsonp callback name', 'clb');28+29+ app.use(function(req, res){30+ res.jsonp({ count: 1 });31+ });32+33+ request(app)34+ .get('/?clb=something')35+ .end(function(err, res){36+ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');37+ res.text.should.equal('something({"count":1});');38+ done();39+ })40+ }) 41+42+ it('should allow []', function(done){43+ var app = express();44+45+ app.use(function(req, res){46+ res.jsonp({ count: 1 });47+ });48+49+ request(app)50+ .get('/?callback=callbacks[123]')51+ .end(function(err, res){52+ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');53+ res.text.should.equal('callbacks[123]({"count":1});');54+ done();55+ })56+ })57+58+ it('should disallow arbitrary js', function(done){59+ var app = express();60+61+ app.use(function(req, res){62+ res.jsonp({});63+ });64+65+ request(app)66+ .get('/?callback=foo;bar()')67+ .end(function(err, res){68+ res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');69+ res.text.should.equal('foobar({});');70+ done();71+ })72+ })73+74+ describe('when given primitives', function(){75+ it('should respond with json', function(done){76+ var app = express();77+78+ app.use(function(req, res){79+ res.jsonp(null);80+ });81+82+ request(app)83+ .get('/')84+ .end(function(err, res){85+ res.headers.should.have.property('content-type', 'application/json; charset=utf-8');86+ res.text.should.equal('null');87+ done();88+ })89+ })90+ })91+92+ describe('when given an array', function(){93+ it('should respond with json', function(done){94+ var app = express();95+96+ app.use(function(req, res){97+ res.jsonp(['foo', 'bar', 'baz']);98+ });99+100+ request(app)101+ .get('/')102+ .end(function(err, res){103+ res.headers.should.have.property('content-type', 'application/json; charset=utf-8');104+ res.text.should.equal('["foo","bar","baz"]');105+ done();106+ })107+ })108+ })109+ 110+ describe('when given an object', function(){111+ it('should respond with json', function(done){112+ var app = express();113+114+ app.use(function(req, res){115+ res.jsonp({ name: 'tobi' });116+ });117+118+ request(app)119+ .get('/')120+ .end(function(err, res){121+ res.headers.should.have.property('content-type', 'application/json; charset=utf-8');122+ res.text.should.equal('{"name":"tobi"}');123+ done();124+ })125+ })126+ })127+128+ describe('"json replacer" setting', function(){129+ it('should be passed to JSON.stringify()', function(done){130+ var app = express();131+132+ app.set('json replacer', function(key, val){133+ return '_' == key[0]134+ ? undefined135+ : val;136+ });137+138+ app.use(function(req, res){139+ res.jsonp({ name: 'tobi', _id: 12345 });140+ });141+142+ request(app)143+ .get('/')144+ .end(function(err, res){145+ res.text.should.equal('{"name":"tobi"}');146+ done();147+ });148+ })149+ })150+151+ describe('"json spaces" setting', function(){152+ it('should default to 2 in development', function(){153+ process.env.NODE_ENV = 'development';154+ var app = express();155+ app.get('json spaces').should.equal(2);156+ process.env.NODE_ENV = 'test';157+ })158+159+ it('should be undefined otherwise', function(){160+ var app = express();161+ assert(undefined === app.get('json spaces'));162+ })163+164+ it('should be passed to JSON.stringify()', function(done){165+ var app = express();166+167+ app.set('json spaces', 2);168+169+ app.use(function(req, res){170+ res.jsonp({ name: 'tobi', age: 2 });171+ });172+173+ request(app)174+ .get('/')175+ .end(function(err, res){176+ res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');177+ done();178+ });179+ })180+ })181+ })182+ 183+ describe('.json(status, object)', function(){184+ it('should respond with json and set the .statusCode', function(done){185+ var app = express();186+187+ app.use(function(req, res){188+ res.jsonp(201, { id: 1 });189+ });190+191+ request(app)192+ .get('/')193+ .end(function(err, res){194+ res.statusCode.should.equal(201);195+ res.headers.should.have.property('content-type', 'application/json; charset=utf-8');196+ res.text.should.equal('{"id":1}');197+ done();198+ })199+ })200+ })201+202+ describe('.json(object, status)', function(){203+ it('should respond with json and set the .statusCode for backwards compat', function(done){204+ var app = express();205+206+ app.use(function(req, res){207+ res.jsonp({ id: 1 }, 201);208+ });209+210+ request(app)211+ .get('/')212+ .end(function(err, res){213+ res.statusCode.should.equal(201);214+ res.headers.should.have.property('content-type', 'application/json; charset=utf-8');215+ res.text.should.equal('{"id":1}');216+ done();217+ })218+ })219+ })220+})modified:test/res.send.js+12 added
320 unmodified lines321 .set('If-None-Match', 'asdf')322 .expect('hey')323 .expect(500, done);324 })325+326+ it('should not support jsonp callbacks', function(done){327+ var app = express();328+329+ app.use(function(req, res){330+ res.send({ foo: 'bar' });331+ });332+333+ request(app)334+ .get('/?callback=foo')335+ .expect('{"foo":"bar"}', done);336+ })337 })