-
Notifications
You must be signed in to change notification settings - Fork 25
/
json.js
186 lines (166 loc) · 6.07 KB
/
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'use strict';
let should = require('should'),
express = require('express'),
request = require('supertest'),
mung = require('../');
describe ('mung json', () => {
function noop (json, req, res) {
}
function inspect (json, req, res) {
json.inspected_by = 'me'
}
function remove (json, req, res) {
return null;
}
function reduce (json, req, res) {
return json.a;
}
function life (json, req, res) {
return 42;
}
function error(json, req, res) {
json.foo.bar.hopefully.fails();
}
it('should return the munged JSON result', done => {
let server = express()
.use(mung.json(inspect))
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(200)
.expect(res => {
let expected = {a : 'a', 'inspected_by': 'me'};
res.body.should.eql(expected);
res.headers['content-length'].should.equal(JSON.stringify(expected).length.toString())
})
.end(done);
});
it('should not mung an error response (by default)', done => {
let server = express()
.use(mung.json(inspect))
.get('/', (req, res) => res.status(404).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(404)
.expect(res => {
res.body.should.not.have.property('inspected_by');
})
.end(done);
});
it('should mung an error response when told to', done => {
let server = express()
.use(mung.json(inspect, { mungError: true }))
.get('/', (req, res) => res.status(404).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(404)
.expect(res => {
let expected = {a : 'a', 'inspected_by': 'me'};
res.body.should.eql(expected);
res.headers['content-length'].should.equal(JSON.stringify(expected).length.toString())
})
.end(done);
});
it('should return 204 on null JSON result', done => {
let server = express()
.use(mung.json(remove))
.get('/', (req, res) => res.status(200).json({ a: 'a' }));
request(server)
.get('/')
.expect(204)
.end(done);
});
it('should return the munged JSON result from a res.send', done => {
let server = express()
.use(mung.json(inspect))
.get('/', (req, res) => res.status(200).send({ a: 'a' }).end());
request(server)
.get('/')
.expect(200)
.expect(res => {
let expected = {a : 'a', 'inspected_by': 'me'};
res.body.should.eql(expected);
res.headers['content-length'].should.equal(JSON.stringify(expected).length.toString())
})
.end(done);
});
it('should return a munged scalar result as text/plain', done => {
let server = express()
.use(mung.json(reduce))
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(200)
.expect(res => {
res.text.should.equal('a');
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');
})
.end(done);
});
it('should return a munged number as text/plain', done => {
let server = express()
.use(mung.json(life))
.get('/', (req, res) => res.status(200).json("the meaning of life").end());
request(server)
.get('/')
.expect(200)
.expect(res => {
res.text.should.equal('42');
res.headers.should.have.property('content-type', 'text/plain; charset=utf-8');
})
.end(done);
});
it('should return a number as application/json', done => {
let server = express()
.use(mung.json(noop))
.get('/', (req, res) => res.status(200).json(42).end());
request(server)
.get('/')
.expect(200)
.expect(res => {
res.text.should.equal('42');
res.headers.should.have.property('content-type', 'application/json; charset=utf-8');
})
.end(done);
});
it('should abort if a response is sent', done => {
function error (json, req, res) {
res.status(403).send('no permissions')
}
let server = express()
.use(mung.json(error))
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(403)
.expect(res => {
res.text.should.equal('no permissions');
res.headers.should.have.property('content-type', 'text/html; charset=utf-8');
})
.end(done);
});
it('should 500 on a synchronous exception', done => {
let server = express()
.use((err, req, res, next) => res.status(500).send(err.message).end())
.use(mung.json(error))
.get('/', (req, res) => res.status(200).json({ a: 'a' }).end());
request(server)
.get('/')
.expect(500)
.end(done);
});
it('should 500 on an asynchronous exception', done => {
let server = express()
.use((err, req, res, next) => res.status(500).send(err.message).end())
.use(mung.json(error))
.get('/', (req, res) => {
process.nextTick(() => {
res.status(200).json({ a: 'a' }).end();
});
});
request(server)
.get('/')
.expect(500)
.end(done);
});
})