res.json.js
This file
Path
test/res.json.js
117 lines · 2.9 KiB · JavaScript
Last changed by
Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]TJ Holowaychukbefore adoptionff92afa
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
var express = require('../') , request = require('./support/http') , assert = require('assert');
describe('res', function(){ describe('.json(object)', function(){ describe('when given primitives', function(){ it('should respond with json', function(done){ var app = express();
app.use(function(req, res){ res.json(null); });
request(app) .get('/') .end(function(res){ res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); res.body.should.equal('null'); done(); }) }) }) describe('when given an object', function(){ it('should respond with json', function(done){ var app = express();
app.use(function(req, res){ res.json({ name: 'tobi' }); });
request(app) .get('/') .end(function(res){ res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); res.body.should.equal('{"name":"tobi"}'); 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.json({ name: 'tobi', _id: 12345 }); });
request(app) .get('/') .end(function(res){ res.body.should.equal('{"name":"tobi"}'); done(); }); }) })
describe('"json spaces" setting', function(){ it('should default to 2 in development', function(){ process.env.NODE_ENV = 'development'; var app = express(); app.get('json spaces').should.equal(2); process.env.NODE_ENV = 'test'; })
it('should be undefined otherwise', 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.json({ name: 'tobi', age: 2 }); });
request(app) .get('/') .end(function(res){ res.body.should.equal('{\n "name": "tobi",\n "age": 2\n}'); done(); }); }) }) }) describe('.json(status, object)', function(){ it('should respond with json and set the .statusCode', function(done){ var app = express();
app.use(function(req, res){ res.json(201, { id: 1 }); });
request(app) .get('/') .end(function(res){ res.statusCode.should.equal(201); res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); res.body.should.equal('{"id":1}'); done(); }) }) })})