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
The walk stopped after 200 commits: lines older than f98c112 point at it.
Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 1–2why, one step back12var express = require('../')Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]TJ Holowaychukbefore adoptionff92afa
lines 3–4why, one step back3 , request = require('./support/http')4 , assert = require('assert');Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 5–40why, one step back56describe('res', function(){7 describe('.json(object)', function(){8 describe('when given primitives', function(){9 it('should respond with json', function(done){10 var app = express();1112 app.use(function(req, res){13 res.json(null);14 });1516 request(app)17 .get('/')18 .end(function(res){19 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');20 res.body.should.equal('null');21 done();22 })23 })24 })25 26 describe('when given an object', function(){27 it('should respond with json', function(done){28 var app = express();2930 app.use(function(req, res){31 res.json({ name: 'tobi' });32 });3334 request(app)35 .get('/')36 .end(function(res){37 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');38 res.body.should.equal('{"name":"tobi"}');39 done();40 })Added "json replacer" and "json spaces" settings. Closes #996 [credit to jed]TJ Holowaychukbefore adoptionff92afa
lines 41–94why, one step back41 })42 })4344 describe('"json replacer" setting', function(){45 it('should be passed to JSON.stringify()', function(done){46 var app = express();4748 app.set('json replacer', function(key, val){49 return '_' == key[0]50 ? undefined51 : val;52 });5354 app.use(function(req, res){55 res.json({ name: 'tobi', _id: 12345 });56 });5758 request(app)59 .get('/')60 .end(function(res){61 res.body.should.equal('{"name":"tobi"}');62 done();63 });64 })65 })6667 describe('"json spaces" setting', function(){68 it('should default to 2 in development', function(){69 process.env.NODE_ENV = 'development';70 var app = express();71 app.get('json spaces').should.equal(2);72 process.env.NODE_ENV = 'test';73 })7475 it('should be undefined otherwise', function(){76 var app = express();77 assert(undefined === app.get('json spaces'));78 })7980 it('should be passed to JSON.stringify()', function(done){81 var app = express();8283 app.set('json spaces', 2);8485 app.use(function(req, res){86 res.json({ name: 'tobi', age: 2 });87 });8889 request(app)90 .get('/')91 .end(function(res){92 res.body.should.equal('{\n "name": "tobi",\n "age": 2\n}');93 done();94 });Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 95–117why, one step back95 })96 })97 })98 99 describe('.json(status, object)', function(){100 it('should respond with json and set the .statusCode', function(done){101 var app = express();102103 app.use(function(req, res){104 res.json(201, { id: 1 });105 });106107 request(app)108 .get('/')109 .end(function(res){110 res.statusCode.should.equal(201);111 res.headers.should.have.property('content-type', 'application/json; charset=utf-8');112 res.body.should.equal('{"id":1}');113 done();114 })115 })116 })117})