res.jsonp.js
This file
Path
test/res.jsonp.js
347 lines · 9.1 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–347why, one step back12var express = require('../')3 , request = require('supertest')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 .expect('Content-Type', 'text/javascript; charset=utf-8')18 .expect(200, /something\(\{"count":1\}\);/, done);19 })2021 it('should use first callback parameter with jsonp', function(done){22 var app = express();2324 app.use(function(req, res){25 res.jsonp({ count: 1 });26 });2728 request(app)29 .get('/?callback=something&callback=somethingelse')30 .expect('Content-Type', 'text/javascript; charset=utf-8')31 .expect(200, /something\(\{"count":1\}\);/, done);32 })3334 it('should ignore object callback parameter with jsonp', function(done){35 var app = express();3637 app.use(function(req, res){38 res.jsonp({ count: 1 });39 });4041 request(app)42 .get('/?callback[a]=something')43 .expect('Content-Type', 'application/json; charset=utf-8')44 .expect(200, '{"count":1}', done)45 })4647 it('should allow renaming callback', function(done){48 var app = express();4950 app.set('jsonp callback name', 'clb');5152 app.use(function(req, res){53 res.jsonp({ count: 1 });54 });5556 request(app)57 .get('/?clb=something')58 .expect('Content-Type', 'text/javascript; charset=utf-8')59 .expect(200, /something\(\{"count":1\}\);/, done);60 })6162 it('should allow []', function(done){63 var app = express();6465 app.use(function(req, res){66 res.jsonp({ count: 1 });67 });6869 request(app)70 .get('/?callback=callbacks[123]')71 .expect('Content-Type', 'text/javascript; charset=utf-8')72 .expect(200, /callbacks\[123\]\(\{"count":1\}\);/, done);73 })7475 it('should disallow arbitrary js', function(done){76 var app = express();7778 app.use(function(req, res){79 res.jsonp({});80 });8182 request(app)83 .get('/?callback=foo;bar()')84 .expect('Content-Type', 'text/javascript; charset=utf-8')85 .expect(200, /foobar\(\{\}\);/, done);86 })8788 it('should escape utf whitespace', function(done){89 var app = express();9091 app.use(function(req, res){92 res.jsonp({ str: '\u2028 \u2029 woot' });93 });9495 request(app)96 .get('/?callback=foo')97 .expect('Content-Type', 'text/javascript; charset=utf-8')98 .expect(200, /foo\(\{"str":"\\u2028 \\u2029 woot"\}\);/, done);99 });100101 it('should not escape utf whitespace for json fallback', function(done){102 var app = express();103104 app.use(function(req, res){105 res.jsonp({ str: '\u2028 \u2029 woot' });106 });107108 request(app)109 .get('/')110 .expect('Content-Type', 'application/json; charset=utf-8')111 .expect(200, '{"str":"\u2028 \u2029 woot"}', done);112 });113114 it('should include security header and prologue', function (done) {115 var app = express();116117 app.use(function(req, res){118 res.jsonp({ count: 1 });119 });120121 request(app)122 .get('/?callback=something')123 .expect('Content-Type', 'text/javascript; charset=utf-8')124 .expect('X-Content-Type-Options', 'nosniff')125 .expect(200, /^\/\*\*\//, done);126 })127128 it('should not override previous Content-Types with no callback', function(done){129 var app = express();130131 app.get('/', function(req, res){132 res.type('application/vnd.example+json');133 res.jsonp({ hello: 'world' });134 });135136 request(app)137 .get('/')138 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')139 .expect(200, '{"hello":"world"}', function (err, res) {140 if (err) return done(err);141 res.headers.should.not.have.property('x-content-type-options');142 done();143 });144 })145146 it('should override previous Content-Types with callback', function(done){147 var app = express();148149 app.get('/', function(req, res){150 res.type('application/vnd.example+json');151 res.jsonp({ hello: 'world' });152 });153154 request(app)155 .get('/?callback=cb')156 .expect('Content-Type', 'text/javascript; charset=utf-8')157 .expect('X-Content-Type-Options', 'nosniff')158 .expect(200, /cb\(\{"hello":"world"\}\);$/, done);159 })160161 describe('when given primitives', function(){162 it('should respond with json', function(done){163 var app = express();164165 app.use(function(req, res){166 res.jsonp(null);167 });168169 request(app)170 .get('/')171 .expect('Content-Type', 'application/json; charset=utf-8')172 .expect(200, 'null', done)173 })174 })175176 describe('when given an array', function(){177 it('should respond with json', function(done){178 var app = express();179180 app.use(function(req, res){181 res.jsonp(['foo', 'bar', 'baz']);182 });183184 request(app)185 .get('/')186 .expect('Content-Type', 'application/json; charset=utf-8')187 .expect(200, '["foo","bar","baz"]', done)188 })189 })190191 describe('when given an object', function(){192 it('should respond with json', function(done){193 var app = express();194195 app.use(function(req, res){196 res.jsonp({ name: 'tobi' });197 });198199 request(app)200 .get('/')201 .expect('Content-Type', 'application/json; charset=utf-8')202 .expect(200, '{"name":"tobi"}', done)203 })204 })205206 describe('when given primitives', function(){207 it('should respond with json for null', function(done){208 var app = express();209210 app.use(function(req, res){211 res.jsonp(null);212 });213214 request(app)215 .get('/')216 .expect('Content-Type', 'application/json; charset=utf-8')217 .expect(200, 'null', done)218 })219220 it('should respond with json for Number', function(done){221 var app = express();222223 app.use(function(req, res){224 res.jsonp(300);225 });226227 request(app)228 .get('/')229 .expect('Content-Type', 'application/json; charset=utf-8')230 .expect(200, '300', done)231 })232233 it('should respond with json for String', function(done){234 var app = express();235236 app.use(function(req, res){237 res.jsonp('str');238 });239240 request(app)241 .get('/')242 .expect('Content-Type', 'application/json; charset=utf-8')243 .expect(200, '"str"', done)244 })245 })246247 describe('"json replacer" setting', function(){248 it('should be passed to JSON.stringify()', function(done){249 var app = express();250251 app.set('json replacer', function(key, val){252 return '_' == key[0]253 ? undefined254 : val;255 });256257 app.use(function(req, res){258 res.jsonp({ name: 'tobi', _id: 12345 });259 });260261 request(app)262 .get('/')263 .expect('Content-Type', 'application/json; charset=utf-8')264 .expect(200, '{"name":"tobi"}', done)265 })266 })267268 describe('"json spaces" setting', function(){269 it('should be undefined by default', function(){270 var app = express();271 assert(undefined === app.get('json spaces'));272 })273274 it('should be passed to JSON.stringify()', function(done){275 var app = express();276277 app.set('json spaces', 2);278279 app.use(function(req, res){280 res.jsonp({ name: 'tobi', age: 2 });281 });282283 request(app)284 .get('/')285 .expect('Content-Type', 'application/json; charset=utf-8')286 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)287 })288 })289 })290291 describe('.jsonp(status, object)', function(){292 it('should respond with json and set the .statusCode', function(done){293 var app = express();294295 app.use(function(req, res){296 res.jsonp(201, { id: 1 });297 });298299 request(app)300 .get('/')301 .expect('Content-Type', 'application/json; charset=utf-8')302 .expect(201, '{"id":1}', done)303 })304 })305306 describe('.jsonp(object, status)', function(){307 it('should respond with json and set the .statusCode for backwards compat', function(done){308 var app = express();309310 app.use(function(req, res){311 res.jsonp({ id: 1 }, 201);312 });313314 request(app)315 .get('/')316 .expect('Content-Type', 'application/json; charset=utf-8')317 .expect(201, '{"id":1}', done)318 })319320 it('should use status as second number for backwards compat', function(done){321 var app = express();322323 app.use(function(req, res){324 res.jsonp(200, 201);325 });326327 request(app)328 .get('/')329 .expect('Content-Type', 'application/json; charset=utf-8')330 .expect(201, '200', done)331 })332 })333334 it('should not override previous Content-Types', function(done){335 var app = express();336337 app.get('/', function(req, res){338 res.type('application/vnd.example+json');339 res.jsonp({ hello: 'world' });340 });341342 request(app)343 .get('/')344 .expect('content-type', 'application/vnd.example+json; charset=utf-8')345 .expect(200, '{"hello":"world"}', done)346 })347})