/
res.json.js
154 lines (128 loc) · 3.97 KB
/
res.json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var express = require('../')
, request = require('./support/http')
, assert = require('assert');
describe('res', function(){
describe('.json(object)', function(){
describe('when "jsonp callback" is enabled', function(){
it('should respond with jsonp', function(done){
var app = express();
// app.enable('jsonp callback');
app.use(function(req, res){
res.json({ count: 1 });
});
request(app)
.get('/?callback=something')
.end(function(res){
res.headers.should.have.property('content-type', 'text/javascript; charset=utf-8');
res.body.should.equal('something({"count":1});');
done();
})
})
})
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 array', function(){
it('should respond with json', function(done){
var app = express();
app.use(function(req, res){
res.json(['foo', 'bar', 'baz']);
});
request(app)
.get('/')
.end(function(res){
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
res.body.should.equal('["foo","bar","baz"]');
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();
})
})
})
})