Skip to content
Myr

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

4 files+287 added−76 removed

modified:lib/response.js+47 added-7 removed
181 unmodified lines
182 }183 }184185 // 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);190191 // content-type192 this.charset = this.charset || 'utf-8';193 this.set('Content-Type', 'application/json');194 195 return this.send(body);196};197198/**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 */213214res.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 }225226 // 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')];232233 // 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 lines
modified:test/res.json.js+8 added-69 removed
added:test/res.jsonp.js+220 added
12var express = require('../')3 , request = require('./support/http')4 , assert = require('assert');56describe('res', function(){7 describe('.jsonp(object)', function(){8 it('should respond with jsonp', function(done){9 var app = express();1011 app.use(function(req, res){12 res.jsonp({ count: 1 });13 });1415 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 })2324 it('should allow renaming callback', function(done){25 var app = express();2627 app.set('jsonp callback name', 'clb');2829 app.use(function(req, res){30 res.jsonp({ count: 1 });31 });3233 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 }) 4142 it('should allow []', function(done){43 var app = express();4445 app.use(function(req, res){46 res.jsonp({ count: 1 });47 });4849 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 })5758 it('should disallow arbitrary js', function(done){59 var app = express();6061 app.use(function(req, res){62 res.jsonp({});63 });6465 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 })7374 describe('when given primitives', function(){75 it('should respond with json', function(done){76 var app = express();7778 app.use(function(req, res){79 res.jsonp(null);80 });8182 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 })9192 describe('when given an array', function(){93 it('should respond with json', function(done){94 var app = express();9596 app.use(function(req, res){97 res.jsonp(['foo', 'bar', 'baz']);98 });99100 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();113114 app.use(function(req, res){115 res.jsonp({ name: 'tobi' });116 });117118 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 })127128 describe('"json replacer" setting', function(){129 it('should be passed to JSON.stringify()', function(done){130 var app = express();131132 app.set('json replacer', function(key, val){133 return '_' == key[0]134 ? undefined135 : val;136 });137138 app.use(function(req, res){139 res.jsonp({ name: 'tobi', _id: 12345 });140 });141142 request(app)143 .get('/')144 .end(function(err, res){145 res.text.should.equal('{"name":"tobi"}');146 done();147 });148 })149 })150151 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 })158159 it('should be undefined otherwise', function(){160 var app = express();161 assert(undefined === app.get('json spaces'));162 })163164 it('should be passed to JSON.stringify()', function(done){165 var app = express();166167 app.set('json spaces', 2);168169 app.use(function(req, res){170 res.jsonp({ name: 'tobi', age: 2 });171 });172173 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();186187 app.use(function(req, res){188 res.jsonp(201, { id: 1 });189 });190191 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 })201202 describe('.json(object, status)', function(){203 it('should respond with json and set the .statusCode for backwards compat', function(done){204 var app = express();205206 app.use(function(req, res){207 res.jsonp({ id: 1 }, 201);208 });209210 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 lines
321 .set('If-None-Match', 'asdf')322 .expect('hey')323 .expect(500, done);324 })325326 it('should not support jsonp callbacks', function(done){327 var app = express();328329 app.use(function(req, res){330 res.send({ foo: 'bar' });331 });332333 request(app)334 .get('/?callback=foo')335 .expect('{"foo":"bar"}', done);336 })337})