res.json.js
This file
Path
test/res.json.js
184 lines · 4.6 KiB · JavaScript
Last changed by
remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
History of this fileTake it
The walk stopped after 200 commits: lines older than 5ed1544 point at it.
remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 1–7why, one step back12var express = require('../')3 , request = require('./support/http')4 , assert = require('assert');56describe('res', function(){7 describe('.json(object)', function(){add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26
lines 8–9why, one step back8 it('should not support jsonp callbacks', function(done){9 var app = express();remove generated docsTJ Holowaychukbefore adoption5ed1544
line 10why, one step back10add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26
lines 11–13why, one step back11 app.use(function(req, res){12 res.json({ foo: 'bar' });13 });change jsonp callback default to false. Closes #1304TJ Holowaychukbefore adoptionf2d7bbe
line 14why, one step back14add res.jsonp(). Closes #1307TJ Holowaychukbefore adoptiona6caa26
lines 15–17why, one step back15 request(app)16 .get('/?callback=foo')17 .expect('{"foo":"bar"}', done);remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 18–30why, one step back18 })1920 describe('when given primitives', function(){21 it('should respond with json', function(done){22 var app = express();2324 app.use(function(req, res){25 res.json(null);26 });2728 request(app)29 .get('/')30 .end(function(err, res){remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 31why, one step back31 res.headers.should.have.property('content-type', 'application/json');remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 32–48why, one step back32 res.text.should.equal('null');33 done();34 })35 })36 })3738 describe('when given an array', function(){39 it('should respond with json', function(done){40 var app = express();4142 app.use(function(req, res){43 res.json(['foo', 'bar', 'baz']);44 });4546 request(app)47 .get('/')48 .end(function(err, res){remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 49why, one step back49 res.headers.should.have.property('content-type', 'application/json');remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 50–54why, one step back50 res.text.should.equal('["foo","bar","baz"]');51 done();52 })53 })54 })remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 55why, one step back55remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 56–66why, one step back56 describe('when given an object', function(){57 it('should respond with json', function(done){58 var app = express();5960 app.use(function(req, res){61 res.json({ name: 'tobi' });62 });6364 request(app)65 .get('/')66 .end(function(err, res){remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 67why, one step back67 res.headers.should.have.property('content-type', 'application/json');remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 68–127why, one step back68 res.text.should.equal('{"name":"tobi"}');69 done();70 })71 })72 })7374 describe('"json replacer" setting', function(){75 it('should be passed to JSON.stringify()', function(done){76 var app = express();7778 app.set('json replacer', function(key, val){79 return '_' == key[0]80 ? undefined81 : val;82 });8384 app.use(function(req, res){85 res.json({ name: 'tobi', _id: 12345 });86 });8788 request(app)89 .get('/')90 .end(function(err, res){91 res.text.should.equal('{"name":"tobi"}');92 done();93 });94 })95 })9697 describe('"json spaces" setting', function(){98 it('should default to 2 in development', function(){99 process.env.NODE_ENV = 'development';100 var app = express();101 app.get('json spaces').should.equal(2);102 process.env.NODE_ENV = 'test';103 })104105 it('should be undefined otherwise', function(){106 var app = express();107 assert(undefined === app.get('json spaces'));108 })109110 it('should be passed to JSON.stringify()', function(done){111 var app = express();112113 app.set('json spaces', 2);114115 app.use(function(req, res){116 res.json({ name: 'tobi', age: 2 });117 });118119 request(app)120 .get('/')121 .end(function(err, res){122 res.text.should.equal('{\n "name": "tobi",\n "age": 2\n}');123 done();124 });125 })126 })127 })remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 128why, one step back128remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 129–140why, one step back129 describe('.json(status, object)', function(){130 it('should respond with json and set the .statusCode', function(done){131 var app = express();132133 app.use(function(req, res){134 res.json(201, { id: 1 });135 });136137 request(app)138 .get('/')139 .end(function(err, res){140 res.statusCode.should.equal(201);remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 141why, one step back141 res.headers.should.have.property('content-type', 'application/json');remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 142–159why, one step back142 res.text.should.equal('{"id":1}');143 done();144 })145 })146 })147148 describe('.json(object, status)', function(){149 it('should respond with json and set the .statusCode for backwards compat', function(done){150 var app = express();151152 app.use(function(req, res){153 res.json({ id: 1 }, 201);154 });155156 request(app)157 .get('/')158 .end(function(err, res){159 res.statusCode.should.equal(201);remove charset from json responses. Closes #1631TJ Holowaychukbefore adoption138d74a
line 160why, one step back160 res.headers.should.have.property('content-type', 'application/json');remove generated docsTJ Holowaychukbefore adoption5ed1544
lines 161–165why, one step back161 res.text.should.equal('{"id":1}');162 done();163 })164 })165 })add support for `res.json()` to retain previously defined Content-Types. Closes #1349TJ Holowaychukbefore adoption0d3a637
lines 166–183why, one step back166167 it('should not override previous Content-Types', function(done){168 var app = express();169170 app.get('/', function(req, res){171 res.type('application/vnd.example+json');172 res.json({ hello: 'world' });173 });174175 request(app)176 .get('/')177 .end(function(err, res){178 res.statusCode.should.equal(200);179 res.headers.should.have.property('content-type', 'application/vnd.example+json');180 res.text.should.equal('{"hello":"world"}');181 done();182 })183 })remove generated docsTJ Holowaychukbefore adoption5ed1544
line 184why, one step back184})