res.json.js
This file
Path
test/res.json.js
191 lines · 4.8 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–191why, one step back12var express = require('../')3 , request = require('supertest')4 , assert = require('assert');56describe('res', function(){7 describe('.json(object)', function(){8 it('should not support jsonp callbacks', function(done){9 var app = express();1011 app.use(function(req, res){12 res.json({ foo: 'bar' });13 });1415 request(app)16 .get('/?callback=foo')17 .expect('{"foo":"bar"}', done);18 })1920 it('should not override previous Content-Types', function(done){21 var app = express();2223 app.get('/', function(req, res){24 res.type('application/vnd.example+json');25 res.json({ hello: 'world' });26 });2728 request(app)29 .get('/')30 .expect('Content-Type', 'application/vnd.example+json; charset=utf-8')31 .expect(200, '{"hello":"world"}', done);32 })3334 describe('when given primitives', function(){35 it('should respond with json for null', function(done){36 var app = express();3738 app.use(function(req, res){39 res.json(null);40 });4142 request(app)43 .get('/')44 .expect('Content-Type', 'application/json; charset=utf-8')45 .expect(200, 'null', done)46 })4748 it('should respond with json for Number', function(done){49 var app = express();5051 app.use(function(req, res){52 res.json(300);53 });5455 request(app)56 .get('/')57 .expect('Content-Type', 'application/json; charset=utf-8')58 .expect(200, '300', done)59 })6061 it('should respond with json for String', function(done){62 var app = express();6364 app.use(function(req, res){65 res.json('str');66 });6768 request(app)69 .get('/')70 .expect('Content-Type', 'application/json; charset=utf-8')71 .expect(200, '"str"', done)72 })73 })7475 describe('when given an array', function(){76 it('should respond with json', function(done){77 var app = express();7879 app.use(function(req, res){80 res.json(['foo', 'bar', 'baz']);81 });8283 request(app)84 .get('/')85 .expect('Content-Type', 'application/json; charset=utf-8')86 .expect(200, '["foo","bar","baz"]', done)87 })88 })8990 describe('when given an object', function(){91 it('should respond with json', function(done){92 var app = express();9394 app.use(function(req, res){95 res.json({ name: 'tobi' });96 });9798 request(app)99 .get('/')100 .expect('Content-Type', 'application/json; charset=utf-8')101 .expect(200, '{"name":"tobi"}', done)102 })103 })104105 describe('"json replacer" setting', function(){106 it('should be passed to JSON.stringify()', function(done){107 var app = express();108109 app.set('json replacer', function(key, val){110 return '_' == key[0]111 ? undefined112 : val;113 });114115 app.use(function(req, res){116 res.json({ name: 'tobi', _id: 12345 });117 });118119 request(app)120 .get('/')121 .expect('Content-Type', 'application/json; charset=utf-8')122 .expect(200, '{"name":"tobi"}', done)123 })124 })125126 describe('"json spaces" setting', function(){127 it('should be undefined by default', function(){128 var app = express();129 assert(undefined === app.get('json spaces'));130 })131132 it('should be passed to JSON.stringify()', function(done){133 var app = express();134135 app.set('json spaces', 2);136137 app.use(function(req, res){138 res.json({ name: 'tobi', age: 2 });139 });140141 request(app)142 .get('/')143 .expect('Content-Type', 'application/json; charset=utf-8')144 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)145 })146 })147 })148149 describe('.json(status, object)', function(){150 it('should respond with json and set the .statusCode', function(done){151 var app = express();152153 app.use(function(req, res){154 res.json(201, { id: 1 });155 });156157 request(app)158 .get('/')159 .expect('Content-Type', 'application/json; charset=utf-8')160 .expect(201, '{"id":1}', done)161 })162 })163164 describe('.json(object, status)', function(){165 it('should respond with json and set the .statusCode for backwards compat', function(done){166 var app = express();167168 app.use(function(req, res){169 res.json({ id: 1 }, 201);170 });171172 request(app)173 .get('/')174 .expect('Content-Type', 'application/json; charset=utf-8')175 .expect(201, '{"id":1}', done)176 })177178 it('should use status as second number for backwards compat', function(done){179 var app = express();180181 app.use(function(req, res){182 res.json(200, 201);183 });184185 request(app)186 .get('/')187 .expect('Content-Type', 'application/json; charset=utf-8')188 .expect(201, '200', done)189 })190 })191})