res.jsonp.js
This file
Path
test/res.jsonp.js
245 lines · 6.6 KiB · JavaScript
Last changed by
Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
History of this fileTake it
The walk stopped after 200 commits: lines older than 3ab3021 point at it.
Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 1–18why, one step back12var 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');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 19why, one step back19 res.text.should.equal('typeof something === \'function\' && something({"count":1});');Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 20–23why, one step back20 done();21 })22 })23Merge pull request #1711 from jonjenkins/masterTJ Holowaychukbefore adoption3ea7381
lines 24–34why, one step back24 it('should use first callback parameter with jsonp', function(done){25 var app = express();2627 app.use(function(req, res){28 res.jsonp({ count: 1 });29 });3031 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');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 35why, one step back35 res.text.should.equal('typeof something === \'function\' && something({"count":1});');Merge pull request #1711 from jonjenkins/masterTJ Holowaychukbefore adoption3ea7381
lines 36–39why, one step back36 done();37 })38 })39Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 40–52why, one step back40 it('should allow renaming callback', function(done){41 var app = express();4243 app.set('jsonp callback name', 'clb');4445 app.use(function(req, res){46 res.jsonp({ count: 1 });47 });4849 request(app)50 .get('/?clb=something')51 .end(function(err, res){52 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 53why, one step back53 res.text.should.equal('typeof something === \'function\' && something({"count":1});');Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 54–55why, one step back54 done();55 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 56why, one step back56 })Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 57–68why, one step back5758 it('should allow []', function(done){59 var app = express();6061 app.use(function(req, res){62 res.jsonp({ count: 1 });63 });6465 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');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 69why, one step back69 res.text.should.equal('typeof callbacks[123] === \'function\' && callbacks[123]({"count":1});');Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 70–84why, one step back70 done();71 })72 })7374 it('should disallow arbitrary js', function(done){75 var app = express();7677 app.use(function(req, res){78 res.jsonp({});79 });8081 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');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 85why, one step back85 res.text.should.equal('typeof foobar === \'function\' && foobar({});');Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 86–100why, one step back86 done();87 })88 })8990 it('should escape utf whitespace', function(done){91 var app = express();9293 app.use(function(req, res){94 res.jsonp({ str: '\u2028 \u2029 woot' });95 });9697 request(app)98 .get('/?callback=foo')99 .end(function(err, res){100 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 101why, one step back101 res.text.should.equal('typeof foo === \'function\' && foo({"str":"\\u2028 \\u2029 woot"});');Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 102–140why, one step back102 done();103 });104 });105106 describe('when given primitives', function(){107 it('should respond with json', function(done){108 var app = express();109110 app.use(function(req, res){111 res.jsonp(null);112 });113114 request(app)115 .get('/')116 .end(function(err, res){117 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');118 res.text.should.equal('null');119 done();120 })121 })122 })123124 describe('when given an array', function(){125 it('should respond with json', function(done){126 var app = express();127128 app.use(function(req, res){129 res.jsonp(['foo', 'bar', 'baz']);130 });131132 request(app)133 .get('/')134 .end(function(err, res){135 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');136 res.text.should.equal('["foo","bar","baz"]');137 done();138 })139 })140 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 141why, one step back141Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 142–183why, one step back142 describe('when given an object', function(){143 it('should respond with json', function(done){144 var app = express();145146 app.use(function(req, res){147 res.jsonp({ name: 'tobi' });148 });149150 request(app)151 .get('/')152 .end(function(err, res){153 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');154 res.text.should.equal('{"name":"tobi"}');155 done();156 })157 })158 })159160 describe('"json replacer" setting', function(){161 it('should be passed to JSON.stringify()', function(done){162 var app = express();163164 app.set('json replacer', function(key, val){165 return '_' == key[0]166 ? undefined167 : val;168 });169170 app.use(function(req, res){171 res.jsonp({ name: 'tobi', _id: 12345 });172 });173174 request(app)175 .get('/')176 .end(function(err, res){177 res.text.should.equal('{"name":"tobi"}');178 done();179 });180 })181 })182183 describe('"json spaces" setting', function(){Merge pull request #1899 from visionmedia/remove-configureRoman Shtylmanbefore adoptionac2cbef
line 184why, one step back184 it('should be undefined by default', function(){Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 185–206why, one step back185 var app = express();186 assert(undefined === app.get('json spaces'));187 })188189 it('should be passed to JSON.stringify()', function(done){190 var app = express();191192 app.set('json spaces', 2);193194 app.use(function(req, res){195 res.jsonp({ name: 'tobi', age: 2 });196 });197198 request(app)199 .get('/')200 .end(function(err, res){201 res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');202 done();203 });204 })205 })206 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 207why, one step back207Release 3.0.6TJ Holowaychukbefore adoption3ab3021
lines 208–245why, one step back208 describe('.json(status, object)', function(){209 it('should respond with json and set the .statusCode', function(done){210 var app = express();211212 app.use(function(req, res){213 res.jsonp(201, { id: 1 });214 });215216 request(app)217 .get('/')218 .end(function(err, res){219 res.statusCode.should.equal(201);220 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');221 res.text.should.equal('{"id":1}');222 done();223 })224 })225 })226227 describe('.json(object, status)', function(){228 it('should respond with json and set the .statusCode for backwards compat', function(done){229 var app = express();230231 app.use(function(req, res){232 res.jsonp({ id: 1 }, 201);233 });234235 request(app)236 .get('/')237 .end(function(err, res){238 res.statusCode.should.equal(201);239 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');240 res.text.should.equal('{"id":1}');241 done();242 })243 })244 })245})