res.jsonp.js
This file
Path
test/res.jsonp.js
340 lines · 9.1 KiB · JavaScript
Last changed by
set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
History of this fileTake it
The walk stopped after 200 commits: lines older than a10f695 point at it.
pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 1–2why, one step back12var express = require('../')remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3
line 3why, one step back3 , request = require('supertest')pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 4–18why, one step back4 , 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});');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
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 })39Merge branch '3.5.x' into 3.xDouglas Christopher Wilsonbefore adoption66b38b5
lines 40–48why, one step back40 it('should ignore object callback parameter with jsonp', function(done){41 var app = express();4243 app.use(function(req, res){44 res.jsonp({ count: 1 });45 });4647 request(app)48 .get('/?callback[a]=something')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 49–50why, one step back49 .expect('Content-Type', 'application/json; charset=utf-8')50 .expect(200, '{"count":1}', done)Merge branch '3.5.x' into 3.xDouglas Christopher Wilsonbefore adoption66b38b5
lines 51–52why, one step back51 })52pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 53–65why, one step back53 it('should allow renaming callback', function(done){54 var app = express();5556 app.set('jsonp callback name', 'clb');5758 app.use(function(req, res){59 res.jsonp({ count: 1 });60 });6162 request(app)63 .get('/?clb=something')64 .end(function(err, res){65 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 66why, one step back66 res.text.should.equal('typeof something === \'function\' && something({"count":1});');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 67–68why, one step back67 done();68 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 69why, one step back69 })pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 70–81why, one step back7071 it('should allow []', function(done){72 var app = express();7374 app.use(function(req, res){75 res.jsonp({ count: 1 });76 });7778 request(app)79 .get('/?callback=callbacks[123]')80 .end(function(err, res){81 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 82why, one step back82 res.text.should.equal('typeof callbacks[123] === \'function\' && callbacks[123]({"count":1});');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 83–97why, one step back83 done();84 })85 })8687 it('should disallow arbitrary js', function(done){88 var app = express();8990 app.use(function(req, res){91 res.jsonp({});92 });9394 request(app)95 .get('/?callback=foo;bar()')96 .end(function(err, res){97 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 98why, one step back98 res.text.should.equal('typeof foobar === \'function\' && foobar({});');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 99–113why, one step back99 done();100 })101 })102103 it('should escape utf whitespace', function(done){104 var app = express();105106 app.use(function(req, res){107 res.jsonp({ str: '\u2028 \u2029 woot' });108 });109110 request(app)111 .get('/?callback=foo')112 .end(function(err, res){113 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 114why, one step back114 res.text.should.equal('typeof foo === \'function\' && foo({"str":"\\u2028 \\u2029 woot"});');pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 115–118why, one step back115 done();116 });117 });118set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 119–146why, one step back119 it('should not override previous Content-Types with no callback', function(done){120 var app = express();121122 app.get('/', function(req, res){123 res.type('application/vnd.example+json');124 res.jsonp({ hello: 'world' });125 });126127 request(app)128 .get('/')129 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')130 .expect(200, '{"hello":"world"}', done);131 })132133 it('should override previous Content-Types with callback', function(done){134 var app = express();135136 app.get('/', function(req, res){137 res.type('application/vnd.example+json');138 res.jsonp({ hello: 'world' });139 });140141 request(app)142 .get('/?callback=cb')143 .expect('Content-Type', 'text/javascript; charset=utf-8')144 .expect(200, /cb\(\{"hello":"world"\}\);$/, done);145 })146pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 147–156why, one step back147 describe('when given primitives', function(){148 it('should respond with json', function(done){149 var app = express();150151 app.use(function(req, res){152 res.jsonp(null);153 });154155 request(app)156 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 157–158why, one step back157 .expect('Content-Type', 'application/json; charset=utf-8')158 .expect(200, 'null', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 159–171why, one step back159 })160 })161162 describe('when given an array', function(){163 it('should respond with json', function(done){164 var app = express();165166 app.use(function(req, res){167 res.jsonp(['foo', 'bar', 'baz']);168 });169170 request(app)171 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 172–173why, one step back172 .expect('Content-Type', 'application/json; charset=utf-8')173 .expect(200, '["foo","bar","baz"]', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 174–175why, one step back174 })175 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 176why, one step back176pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 177–186why, one step back177 describe('when given an object', function(){178 it('should respond with json', function(done){179 var app = express();180181 app.use(function(req, res){182 res.jsonp({ name: 'tobi' });183 });184185 request(app)186 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 187–188why, one step back187 .expect('Content-Type', 'application/json; charset=utf-8')188 .expect(200, '{"name":"tobi"}', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 189–191why, one step back189 })190 })191tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a
lines 192–201why, one step back192 describe('when given primitives', function(){193 it('should respond with json for null', function(done){194 var app = express();195196 app.use(function(req, res){197 res.jsonp(null);198 });199200 request(app)201 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 202–203why, one step back202 .expect('Content-Type', 'application/json; charset=utf-8')203 .expect(200, 'null', done)tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a
lines 204–214why, one step back204 })205206 it('should respond with json for Number', function(done){207 var app = express();208209 app.use(function(req, res){210 res.jsonp(300);211 });212213 request(app)214 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 215–216why, one step back215 .expect('Content-Type', 'application/json; charset=utf-8')216 .expect(200, '300', done)tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a
lines 217–227why, one step back217 })218219 it('should respond with json for String', function(done){220 var app = express();221222 app.use(function(req, res){223 res.jsonp('str');224 });225226 request(app)227 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 228–229why, one step back228 .expect('Content-Type', 'application/json; charset=utf-8')229 .expect(200, '"str"', done)tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a
lines 230–232why, one step back230 })231 })232pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 233–248why, one step back233 describe('"json replacer" setting', function(){234 it('should be passed to JSON.stringify()', function(done){235 var app = express();236237 app.set('json replacer', function(key, val){238 return '_' == key[0]239 ? undefined240 : val;241 });242243 app.use(function(req, res){244 res.jsonp({ name: 'tobi', _id: 12345 });245 });246247 request(app)248 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 249–250why, one step back249 .expect('Content-Type', 'application/json; charset=utf-8')250 .expect(200, '{"name":"tobi"}', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 251–277why, one step back251 })252 })253254 describe('"json spaces" setting', function(){255 it('should default to 2 in development', function(){256 process.env.NODE_ENV = 'development';257 var app = express();258 app.get('json spaces').should.equal(2);259 process.env.NODE_ENV = 'test';260 })261262 it('should be undefined otherwise', function(){263 var app = express();264 assert(undefined === app.get('json spaces'));265 })266267 it('should be passed to JSON.stringify()', function(done){268 var app = express();269270 app.set('json spaces', 2);271272 app.use(function(req, res){273 res.jsonp({ name: 'tobi', age: 2 });274 });275276 request(app)277 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 278–279why, one step back278 .expect('Content-Type', 'application/json; charset=utf-8')279 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 280–282why, one step back280 })281 })282 })Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionbd8b9f5
line 283why, one step back283Merge branch '3.5.x' into 3.xDouglas Christopher Wilsonbefore adoption66b38b5
line 284why, one step back284 describe('.jsonp(status, object)', function(){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 285–293why, one step back285 it('should respond with json and set the .statusCode', function(done){286 var app = express();287288 app.use(function(req, res){289 res.jsonp(201, { id: 1 });290 });291292 request(app)293 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 294–295why, one step back294 .expect('Content-Type', 'application/json; charset=utf-8')295 .expect(201, '{"id":1}', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 296–298why, one step back296 })297 })298Merge branch '3.5.x' into 3.xDouglas Christopher Wilsonbefore adoption66b38b5
line 299why, one step back299 describe('.jsonp(object, status)', function(){pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
lines 300–308why, one step back300 it('should respond with json and set the .statusCode for backwards compat', function(done){301 var app = express();302303 app.use(function(req, res){304 res.jsonp({ id: 1 }, 201);305 });306307 request(app)308 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 309–310why, one step back309 .expect('Content-Type', 'application/json; charset=utf-8')310 .expect(201, '{"id":1}', done)pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 311why, one step back311 })deprecate res.json(obj, status)Douglas Christopher Wilsonbefore adoption4e1e252
lines 312–321why, one step back312313 it('should use status as second number for backwards compat', function(done){314 var app = express();315316 app.use(function(req, res){317 res.jsonp(200, 201);318 });319320 request(app)321 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 322–323why, one step back322 .expect('Content-Type', 'application/json; charset=utf-8')323 .expect(201, '200', done)deprecate res.json(obj, status)Douglas Christopher Wilsonbefore adoption4e1e252
line 324why, one step back324 })pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 325why, one step back325 })Keep previous Content-Type for res.jsonpAlberto Lealbefore adoption084f5d8
lines 326–336why, one step back326327 it('should not override previous Content-Types', function(done){328 var app = express();329330 app.get('/', function(req, res){331 res.type('application/vnd.example+json');332 res.jsonp({ hello: 'world' });333 });334335 request(app)336 .get('/')set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d
lines 337–338why, one step back337 .expect('content-type', 'application/vnd.example+json; charset=utf-8')338 .expect(200, '{"hello":"world"}', done)Keep previous Content-Type for res.jsonpAlberto Lealbefore adoption084f5d8
line 339why, one step back339 })pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695
line 340why, one step back340})