-
Notifications
You must be signed in to change notification settings - Fork 982
/
Copy pathplugins.test.js
344 lines (304 loc) · 11.1 KB
/
plugins.test.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2012 Mark Cavage, Inc. All rights reserved.
'use strict';
/* eslint-disable func-names */
// external requires
var pino = require('pino');
var assert = require('chai').assert;
var restify = require('../../lib/index.js');
var restifyClients = require('restify-clients');
var sanitizePath = require('../../lib/plugins/pre/prePath.js');
// local files
var helper = require('../lib/helper');
// local globals
var PORT = process.env.UNIT_TEST_PORT || 0;
var CLIENT;
var SERVER;
describe('all other plugins', function() {
beforeEach(function(done) {
SERVER = restify.createServer({
dtrace: helper.dtrace,
log: helper.getLog('server'),
version: ['2.0.0', '0.5.4', '1.4.3']
});
SERVER.listen(PORT, '127.0.0.1', function() {
PORT = SERVER.address().port;
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
dtrace: helper.dtrace,
retry: false
});
done();
});
});
afterEach(function(done) {
CLIENT.close();
SERVER.close(done);
});
describe('date parser', function() {
it('should reject expired request', function(done) {
SERVER.use(restify.plugins.dateParser());
SERVER.get('/', function respond(req, res, next) {
res.send();
next();
});
var opts = {
path: '/',
headers: {
date: 'Tue, 15 Nov 1994 08:12:31 GMT'
}
};
CLIENT.get(opts, function(err, _, res) {
assert.ok(err);
assert.ok(/Date header .+ is too old/.test(err.message));
assert.equal(res.statusCode, 400);
done();
});
});
});
describe('request logger', function() {
it('tests the requestLoggers extra header properties', function(done) {
var key = 'x-request-uuid';
var badKey = 'x-foo-bar';
var getPath = '/requestLogger/extraHeaders';
var headers = [key, badKey];
SERVER.use(restify.plugins.requestLogger({ headers: headers }));
SERVER.get(getPath, function(req, res, next) {
var childings = req.log[pino.symbols.chindingsSym];
assert.match(childings, /"x-request-uuid":"foo-for-eva"/);
assert.notMatch(childings, /x-foo-bar/);
res.send();
next();
});
var obj = {
path: getPath,
headers: {}
};
obj.headers[key] = 'foo-for-eva';
CLIENT.get(obj, function(err, _, res) {
assert.equal(res.statusCode, 200);
assert.ifError(err);
done();
});
});
it('adds the request id to logs', function(done) {
SERVER.use(restify.plugins.requestLogger());
SERVER.get('/requestLogger/test', function(req, res, next) {
var childings = req.log[pino.symbols.chindingsSym];
assert.match(childings, /"req_id":"[0-9A-F-]+"/i);
res.send();
next();
});
CLIENT.get('/requestLogger/test', function(err, _, res) {
assert.equal(res.statusCode, 200);
assert.ifError(err);
done();
});
});
it('adds the request id with a custom field name', function(done) {
SERVER.use(
restify.plugins.requestLogger({ requestIdFieldName: 'traceId' })
);
SERVER.get('/requestLogger/test', function(req, res, next) {
var childings = req.log[pino.symbols.chindingsSym];
assert.match(childings, /"traceId":"[0-9A-F-]+"/i);
assert.notMatch(childings, /"req_id"/);
res.send();
next();
});
CLIENT.get('/requestLogger/test', function(err, _, res) {
assert.equal(res.statusCode, 200);
assert.ifError(err);
done();
});
});
});
describe('full response', function() {
it('full response', function(done) {
SERVER.use(restify.plugins.fullResponse());
SERVER.get('/bar/:id', function tester2(req, res, next) {
assert.ok(req.params);
assert.equal(req.params.id, 'bar');
res.send();
next();
});
CLIENT.get('/bar/bar', function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
var headers = res.headers;
assert.ok(headers, 'headers ok');
assert.ok(headers.date);
assert.ok(headers['request-id']);
assert.ok(headers['response-time'] >= 0);
assert.equal(headers.server, 'restify');
assert.equal(headers.connection, 'Keep-Alive');
done();
});
});
});
describe('context', function() {
it('set and get request context', function(done) {
SERVER.pre(restify.plugins.pre.context());
var asserted = false;
var expectedData = {
pink: 'floyd'
};
SERVER.get('/context', [
function(req, res, next) {
req.set('pink', 'floyd');
return next();
},
function(req, res, next) {
assert.equal('floyd', req.get('pink'));
assert.deepEqual(expectedData, req.getAll());
asserted = true;
res.send(200);
return next();
}
]);
CLIENT.get('/context', function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.ok(asserted);
done();
});
});
it('should throw if set key is not string', function(done) {
SERVER.pre(restify.plugins.pre.context());
var asserted = false;
SERVER.get('/context', [
function(req, res, next) {
try {
req.set({}, 'floyd');
} catch (e) {
asserted = true;
res.send(200);
}
return next();
}
]);
CLIENT.get('/context', function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.ok(asserted);
done();
});
});
it('should throw if set key is empty string', function(done) {
SERVER.pre(restify.plugins.pre.context());
var asserted = false;
SERVER.get('/context', [
function(req, res, next) {
try {
req.set('', 'floyd');
} catch (e) {
asserted = true;
res.send(200);
}
return next();
}
]);
CLIENT.get('/context', function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.ok(asserted);
done();
});
});
it('should throw if get key is not string', function(done) {
SERVER.pre(restify.plugins.pre.context());
var asserted = false;
SERVER.get('/context', [
function(req, res, next) {
try {
req.get({});
} catch (e) {
asserted = true;
res.send(200);
}
return next();
}
]);
CLIENT.get('/context', function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.ok(asserted);
done();
});
});
it('should throw if get key is empty string', function(done) {
SERVER.pre(restify.plugins.pre.context());
var asserted = false;
SERVER.get('/context', [
function(req, res, next) {
try {
req.get('');
} catch (e) {
asserted = true;
res.send(200);
}
return next();
}
]);
CLIENT.get('/context', function(err, _, res) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert.ok(asserted);
done();
});
});
});
describe('sanitizePath', function() {
// Ensure it santizies potential edge cases correctly
var tests = {
input: [
'/', // basic path
'///', // excess on basic path
'////foo////', //excess padding on both ends
'bar/foo/', // trailing slash
'bar/foo/////', // multiple trailing slashes
'foo////bar', // multiple slashes inbetween
'////foo', // multiple at beginning
'/foo/bar', // don't mutate
'/?test=1', // basic with query
'///foo///?test=1' // excess with query
],
output: [
'/',
'/',
'/foo',
'bar/foo',
'bar/foo',
'foo/bar',
'/foo',
'/foo/bar',
'/?test=1',
'/foo?test=1'
],
description: [
'should give the root as a slash',
'dont give empty string for excess on root',
'should clean excess padding on both ends',
'should clean trailing slash',
'should clean multiple trailing slashes',
'should clean multiple slashes inbetween',
'should clean multiple at beginning',
'dont mutate correct urls',
'preserve query string with basic path',
'preserve query string with excess slashes'
]
};
for (var i = 0; i < tests.input.length; i++) {
// eslint-disable-next-line wrap-iife
(function() {
var index = i;
it(tests.description[index], function(done) {
var req = { url: tests.input[index] };
sanitizePath()(req, null, function() {
assert.equal(req.url, tests.output[index]);
done();
});
});
})();
}
});
});