-
Notifications
You must be signed in to change notification settings - Fork 982
Expand file tree
/
Copy pathbodyReader.test.js
More file actions
263 lines (222 loc) · 7.85 KB
/
bodyReader.test.js
File metadata and controls
263 lines (222 loc) · 7.85 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
'use strict';
/* eslint-disable func-names */
// core requires
var http = require('http');
var zlib = require('zlib');
// external requires
var assert = require('chai').assert;
var restify = require('../../lib/index.js');
var restifyClients = require('restify-clients');
// local files
var helper = require('../lib/helper');
// local globals
var SERVER;
var CLIENT;
var PORT;
describe('body reader', function() {
describe('gzip content encoding', function() {
beforeEach(function(done) {
SERVER = restify.createServer({
dtrace: helper.dtrace,
log: helper.getLog('server')
});
SERVER.listen(0, '127.0.0.1', function() {
PORT = SERVER.address().port;
done();
});
});
afterEach(function(done) {
CLIENT.close();
SERVER.close(done);
});
it('should parse gzip encoded content', function(done) {
SERVER.use(restify.plugins.bodyParser());
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
retry: false,
gzip: {}
});
SERVER.post('/compressed', function(req, res, next) {
assert.equal(req.body.apple, 'red');
res.send();
next();
});
CLIENT.post(
'/compressed',
{
apple: 'red'
},
function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
done();
}
);
});
it('should return 400 if body is not a valid gzip', function(done) {
SERVER.use(restify.plugins.bodyParser());
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
retry: false,
headers: {
'content-encoding': 'gzip'
}
});
SERVER.post('/compressed', function(req, res, next) {
res.send();
next();
});
CLIENT.post(
'/compressed',
{
apple: 'red'
},
function(err, _, res) {
assert.isOk(err, 'should fail');
assert.equal(res.statusCode, 400);
done();
}
);
});
describe('when maxBodySize given should', function() {
var body = { apple: 'red' };
var bodyTooLarge = { apple: 'red', lemon: 'yellow' };
var compressedBodySize = zlib.gzipSync(JSON.stringify(body))
.byteLength;
beforeEach(function() {
SERVER.use(
restify.plugins.bodyParser({
maxBodySize: compressedBodySize
})
);
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
retry: false,
gzip: {}
});
SERVER.post('/compressed', function(req, res, next) {
res.send();
next();
});
});
it('return 200 when body size under the limit', function(done) {
CLIENT.post('/compressed', body, function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
done();
});
});
it('return 413 when body size over the limit', function(done) {
CLIENT.post('/compressed', bodyTooLarge, function(err, _, res) {
assert.isOk(err, 'should fail');
assert.equal(res.statusCode, 413);
done();
});
});
});
it('should not accept unsupported content encoding', function(done) {
SERVER.use(restify.plugins.bodyParser());
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
retry: false,
headers: {
'content-encoding': 'unsupported'
}
});
SERVER.post('/compressed', function(req, res, next) {
assert.equal(req.body.apple, 'red');
res.send();
next();
});
CLIENT.post(
'/compressed',
{
apple: 'red'
},
function(err, _, res) {
assert.isOk(err, 'should fail');
assert.equal(res.statusCode, 415);
assert.equal(res.headers['accept-encoding'], 'gzip');
done();
}
);
});
it('should parse unencoded content', function(done) {
SERVER.use(restify.plugins.bodyParser());
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
retry: false
});
SERVER.post('/compressed', function(req, res, next) {
assert.equal(req.body.apple, 'red');
res.send();
next();
});
CLIENT.post(
'/compressed',
{
apple: 'red'
},
function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
done();
}
);
});
it('should handle client timeout', function(done) {
SERVER.use(restify.plugins.bodyParser());
SERVER.post('/compressed', function(req, res, next) {
res.send('ok');
next();
});
// set timeout to 100ms so test runs faster, when client stops
// sending POST data
SERVER.on('connection', function(socket) {
socket.setTimeout(100);
});
var postData = 'hello world';
var options = {
hostname: '127.0.0.1',
port: PORT,
path: '/compressed?v=1',
method: 'POST',
headers: {
'Content-Type': 'application/json',
// report postData + 1 so that request isn't sent
'Content-Length': Buffer.byteLength(postData) + 1
}
};
var req = http.request(options, function(res) {
// should never receive a response
assert.isNotOk(res);
});
SERVER.on('after', function(req2) {
if (req2.href() === '/compressed?v=2') {
assert.equal(SERVER.inflightRequests(), 0);
done();
}
});
// will get a req error after 100ms timeout
req.on('error', function(e) {
// make another request to verify in flight request is only 1
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
retry: false
});
CLIENT.post(
'/compressed?v=2',
{
apple: 'red'
},
function(err, _, res, obj) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
}
);
});
// write data to request body, but don't req.send()
req.write(postData);
});
});
});