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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
var express = require('../') , request = require('supertest') , assert = require('assert');
describe('res', function(){ describe('.jsonp(object)', function(){ it('should respond with jsonp', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ count: 1 }); });
request(app) .get('/?callback=something') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect(200, /something\(\{"count":1\}\);/, done); })
it('should use first callback parameter with jsonp', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ count: 1 }); });
request(app) .get('/?callback=something&callback=somethingelse') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect(200, /something\(\{"count":1\}\);/, done); })
it('should ignore object callback parameter with jsonp', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ count: 1 }); });
request(app) .get('/?callback[a]=something') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '{"count":1}', done) })
it('should allow renaming callback', function(done){ var app = express();
app.set('jsonp callback name', 'clb');
app.use(function(req, res){ res.jsonp({ count: 1 }); });
request(app) .get('/?clb=something') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect(200, /something\(\{"count":1\}\);/, done); })
it('should allow []', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ count: 1 }); });
request(app) .get('/?callback=callbacks[123]') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect(200, /callbacks\[123\]\(\{"count":1\}\);/, done); })
it('should disallow arbitrary js', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({}); });
request(app) .get('/?callback=foo;bar()') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect(200, /foobar\(\{\}\);/, done); })
it('should escape utf whitespace', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ str: '\u2028 \u2029 woot' }); });
request(app) .get('/?callback=foo') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect(200, /foo\(\{"str":"\\u2028 \\u2029 woot"\}\);/, done); });
it('should not escape utf whitespace for json fallback', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ str: '\u2028 \u2029 woot' }); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '{"str":"\u2028 \u2029 woot"}', done); });
it('should include security header and prologue', function (done) { var app = express();
app.use(function(req, res){ res.jsonp({ count: 1 }); });
request(app) .get('/?callback=something') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect('X-Content-Type-Options', 'nosniff') .expect(200, /^\/\*\*\//, done); })
it('should not override previous Content-Types with no callback', function(done){ var app = express();
app.get('/', function(req, res){ res.type('application/vnd.example+json'); res.jsonp({ hello: 'world' }); });
request(app) .get('/') .expect('Content-Type', 'application/vnd.example+json; charset=utf-8') .expect(200, '{"hello":"world"}', function (err, res) { if (err) return done(err); res.headers.should.not.have.property('x-content-type-options'); done(); }); })
it('should override previous Content-Types with callback', function(done){ var app = express();
app.get('/', function(req, res){ res.type('application/vnd.example+json'); res.jsonp({ hello: 'world' }); });
request(app) .get('/?callback=cb') .expect('Content-Type', 'text/javascript; charset=utf-8') .expect('X-Content-Type-Options', 'nosniff') .expect(200, /cb\(\{"hello":"world"\}\);$/, done); })
describe('when given primitives', function(){ it('should respond with json', function(done){ var app = express();
app.use(function(req, res){ res.jsonp(null); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, 'null', done) }) })
describe('when given an array', function(){ it('should respond with json', function(done){ var app = express();
app.use(function(req, res){ res.jsonp(['foo', 'bar', 'baz']); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '["foo","bar","baz"]', done) }) })
describe('when given an object', function(){ it('should respond with json', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ name: 'tobi' }); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '{"name":"tobi"}', done) }) })
describe('when given primitives', function(){ it('should respond with json for null', function(done){ var app = express();
app.use(function(req, res){ res.jsonp(null); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, 'null', done) })
it('should respond with json for Number', function(done){ var app = express();
app.use(function(req, res){ res.jsonp(300); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '300', done) })
it('should respond with json for String', function(done){ var app = express();
app.use(function(req, res){ res.jsonp('str'); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '"str"', done) }) })
describe('"json replacer" setting', function(){ it('should be passed to JSON.stringify()', function(done){ var app = express();
app.set('json replacer', function(key, val){ return '_' == key[0] ? undefined : val; });
app.use(function(req, res){ res.jsonp({ name: 'tobi', _id: 12345 }); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '{"name":"tobi"}', done) }) })
describe('"json spaces" setting', function(){ it('should be undefined by default', function(){ var app = express(); assert(undefined === app.get('json spaces')); })
it('should be passed to JSON.stringify()', function(done){ var app = express();
app.set('json spaces', 2);
app.use(function(req, res){ res.jsonp({ name: 'tobi', age: 2 }); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done) }) }) })
describe('.jsonp(status, object)', function(){ it('should respond with json and set the .statusCode', function(done){ var app = express();
app.use(function(req, res){ res.jsonp(201, { id: 1 }); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(201, '{"id":1}', done) }) })
describe('.jsonp(object, status)', function(){ it('should respond with json and set the .statusCode for backwards compat', function(done){ var app = express();
app.use(function(req, res){ res.jsonp({ id: 1 }, 201); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(201, '{"id":1}', done) })
it('should use status as second number for backwards compat', function(done){ var app = express();
app.use(function(req, res){ res.jsonp(200, 201); });
request(app) .get('/') .expect('Content-Type', 'application/json; charset=utf-8') .expect(201, '200', done) }) })
it('should not override previous Content-Types', function(done){ var app = express();
app.get('/', function(req, res){ res.type('application/vnd.example+json'); res.jsonp({ hello: 'world' }); });
request(app) .get('/') .expect('content-type', 'application/vnd.example+json; charset=utf-8') .expect(200, '{"hello":"world"}', done) })})