Skip to content
Myr

res.json.js

This file

Path

test/res.json.js

212 lines · 5.4 KiB · JavaScript

Last changed by

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

History of this file

Take it

The walk stopped after 200 commits: lines older than a10f695 point at it.

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 1–2why, one step back
1
2var express = require('../')

remove unnecessary test/support/httpJonathan Ongbefore adoption328c6d3

line 3why, one step back
3 , request = require('supertest')

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 4–19why, one step back
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 })19

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 20–33why, one step back
20 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 })33

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

line 34why, one step back
34 describe('when given primitives', function(){

tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a

line 35why, one step back
35 it('should respond with json for null', function(done){

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 36–43why, one step back
36 var app = express();
3738 app.use(function(req, res){39 res.json(null);40 });4142 request(app)43 .get('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 44–45why, one step back
44 .expect('Content-Type', 'application/json; charset=utf-8')
45 .expect(200, 'null', done)

tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a

lines 46–56why, one step back
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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 57–58why, one step back
57 .expect('Content-Type', 'application/json; charset=utf-8')
58 .expect(200, '300', done)

tests: add more res.jsonp? testsDouglas Christopher Wilsonbefore adoption920f46a

lines 59–69why, one step back
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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 70–71why, one step back
70 .expect('Content-Type', 'application/json; charset=utf-8')
71 .expect(200, '"str"', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 72–84why, one step back
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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 85–86why, one step back
85 .expect('Content-Type', 'application/json; charset=utf-8')
86 .expect(200, '["foo","bar","baz"]', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 87–88why, one step back
87 })
88 })

Revert "remove charset from json responses. Closes #1631"TJ Holowaychukbefore adoption6cf6c8b

line 89why, one step back
89

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 90–99why, one step back
90 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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 100–101why, one step back
100 .expect('Content-Type', 'application/json; charset=utf-8')
101 .expect(200, '{"name":"tobi"}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 102–120why, one step back
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('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 121–122why, one step back
121 .expect('Content-Type', 'application/json; charset=utf-8')
122 .expect(200, '{"name":"tobi"}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 123–149why, one step back
123 })
124 })125126 describe('"json spaces" setting', function(){127 it('should default to 2 in development', function(){128 process.env.NODE_ENV = 'development';129 var app = express();130 app.get('json spaces').should.equal(2);131 process.env.NODE_ENV = 'test';132 })133134 it('should be undefined otherwise', function(){135 var app = express();136 assert(undefined === app.get('json spaces'));137 })138139 it('should be passed to JSON.stringify()', function(done){140 var app = express();141142 app.set('json spaces', 2);143144 app.use(function(req, res){145 res.json({ name: 'tobi', age: 2 });146 });147148 request(app)149 .get('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 150–151why, one step back
150 .expect('Content-Type', 'application/json; charset=utf-8')
151 .expect(200, '{\n "name": "tobi",\n "age": 2\n}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 152–154why, one step back
152 })
153 })154 })

Revert "remove charset from json responses. Closes #1631"TJ Holowaychukbefore adoption6cf6c8b

line 155why, one step back
155

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 156–165why, one step back
156 describe('.json(status, object)', function(){
157 it('should respond with json and set the .statusCode', function(done){158 var app = express();159160 app.use(function(req, res){161 res.json(201, { id: 1 });162 });163164 request(app)165 .get('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 166–167why, one step back
166 .expect('Content-Type', 'application/json; charset=utf-8')
167 .expect(201, '{"id":1}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 168–180why, one step back
168 })
169 })170171 describe('.json(object, status)', function(){172 it('should respond with json and set the .statusCode for backwards compat', function(done){173 var app = express();174175 app.use(function(req, res){176 res.json({ id: 1 }, 201);177 });178179 request(app)180 .get('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 181–182why, one step back
181 .expect('Content-Type', 'application/json; charset=utf-8')
182 .expect(201, '{"id":1}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

line 183why, one step back
183 })

deprecate res.json(obj, status)Douglas Christopher Wilsonbefore adoption4e1e252

lines 184–193why, one step back
184
185 it('should use status as second number for backwards compat', function(done){186 var app = express();187188 app.use(function(req, res){189 res.json(200, 201);190 });191192 request(app)193 .get('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 194–195why, one step back
194 .expect('Content-Type', 'application/json; charset=utf-8')
195 .expect(201, '200', done)

deprecate res.json(obj, status)Douglas Christopher Wilsonbefore adoption4e1e252

line 196why, one step back
196 })

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 197–208why, one step back
197 })
198199 it('should not override previous Content-Types', function(done){200 var app = express();201202 app.get('/', function(req, res){203 res.type('application/vnd.example+json');204 res.json({ hello: 'world' });205 });206207 request(app)208 .get('/')

set proper charset in content-type for res.sendDouglas Christopher Wilsonbefore adoptionf14e39d

lines 209–210why, one step back
209 .expect('content-type', 'application/vnd.example+json; charset=utf-8')
210 .expect(200, '{"hello":"world"}', done)

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 211–212why, one step back
211 })
212})