Skip to content
Myr

res.jsonp.js

This file

Path

test/res.jsonp.js

236 lines · 6.2 KiB · JavaScript

Last changed by

fix jsonp whitespace escape. Closes #1132Guillermo Rauchbefore adoption64a2349

History of this file

Take it

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 1–18why, one step back
1
2var 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');

add cb && cb(payload) to `res.jsonp()`. Closes #1374TJ Holowaychukbefore adoptionb3936b9

line 19why, one step back
19 res.text.should.equal('something && something({"count":1});');

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 20–36why, one step back
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');

add cb && cb(payload) to `res.jsonp()`. Closes #1374TJ Holowaychukbefore adoptionb3936b9

line 37why, one step back
37 res.text.should.equal('something && something({"count":1});');

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 38–52why, one step back
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');

add cb && cb(payload) to `res.jsonp()`. Closes #1374TJ Holowaychukbefore adoptionb3936b9

line 53why, one step back
53 res.text.should.equal('callbacks[123] && callbacks[123]({"count":1});');

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 54–68why, one step back
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');

add cb && cb(payload) to `res.jsonp()`. Closes #1374TJ Holowaychukbefore adoptionb3936b9

line 69why, one step back
69 res.text.should.equal('foobar && foobar({});');

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 70–73why, one step back
70 done();
71 })72 })73

fix jsonp whitespace escape. Closes #1132Guillermo Rauchbefore adoption64a2349

lines 74–89why, one step back
74 it('should escape utf whitespace', function(done){
75 var app = express();7677 app.use(function(req, res){78 res.jsonp({ str: '\u2028 \u2029 woot' });79 });8081 request(app)82 .get('/?callback=foo')83 .end(function(err, res){84 res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');85 res.text.should.equal('foo && foo({"str":"\\u2028 \\u2029 woot"});');86 done();87 });88 });89

add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26

lines 90–236why, one step back
90 describe('when given primitives', function(){
91 it('should respond with json', function(done){92 var app = express();9394 app.use(function(req, res){95 res.jsonp(null);96 });9798 request(app)99 .get('/')100 .end(function(err, res){101 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');102 res.text.should.equal('null');103 done();104 })105 })106 })107108 describe('when given an array', function(){109 it('should respond with json', function(done){110 var app = express();111112 app.use(function(req, res){113 res.jsonp(['foo', 'bar', 'baz']);114 });115116 request(app)117 .get('/')118 .end(function(err, res){119 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');120 res.text.should.equal('["foo","bar","baz"]');121 done();122 })123 })124 })125 126 describe('when given an object', function(){127 it('should respond with json', function(done){128 var app = express();129130 app.use(function(req, res){131 res.jsonp({ name: 'tobi' });132 });133134 request(app)135 .get('/')136 .end(function(err, res){137 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');138 res.text.should.equal('{"name":"tobi"}');139 done();140 })141 })142 })143144 describe('"json replacer" setting', function(){145 it('should be passed to JSON.stringify()', function(done){146 var app = express();147148 app.set('json replacer', function(key, val){149 return '_' == key[0]150 ? undefined151 : val;152 });153154 app.use(function(req, res){155 res.jsonp({ name: 'tobi', _id: 12345 });156 });157158 request(app)159 .get('/')160 .end(function(err, res){161 res.text.should.equal('{"name":"tobi"}');162 done();163 });164 })165 })166167 describe('"json spaces" setting', function(){168 it('should default to 2 in development', function(){169 process.env.NODE_ENV = 'development';170 var app = express();171 app.get('json spaces').should.equal(2);172 process.env.NODE_ENV = 'test';173 })174175 it('should be undefined otherwise', function(){176 var app = express();177 assert(undefined === app.get('json spaces'));178 })179180 it('should be passed to JSON.stringify()', function(done){181 var app = express();182183 app.set('json spaces', 2);184185 app.use(function(req, res){186 res.jsonp({ name: 'tobi', age: 2 });187 });188189 request(app)190 .get('/')191 .end(function(err, res){192 res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');193 done();194 });195 })196 })197 })198 199 describe('.json(status, object)', function(){200 it('should respond with json and set the .statusCode', function(done){201 var app = express();202203 app.use(function(req, res){204 res.jsonp(201, { id: 1 });205 });206207 request(app)208 .get('/')209 .end(function(err, res){210 res.statusCode.should.equal(201);211 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');212 res.text.should.equal('{"id":1}');213 done();214 })215 })216 })217218 describe('.json(object, status)', function(){219 it('should respond with json and set the .statusCode for backwards compat', function(done){220 var app = express();221222 app.use(function(req, res){223 res.jsonp({ id: 1 }, 201);224 });225226 request(app)227 .get('/')228 .end(function(err, res){229 res.statusCode.should.equal(201);230 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');231 res.text.should.equal('{"id":1}');232 done();233 })234 })235 })236})