-
Notifications
You must be signed in to change notification settings - Fork 171
/
httpServerTestFixture.js
385 lines (348 loc) · 11.3 KB
/
httpServerTestFixture.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright 2020 The OpenZipkin Authors; licensed to You under the Apache License, Version 2.0.
const {InetAddress} = require('zipkin');
const fetch = require('node-fetch');
const https = require('https');
const fs = require('fs');
const fsPath = require('path');
const {setupTestTracer} = require('./testFixture');
class TestServer {
// This doesn't eagerly reference the server URL, as it isn't available until "before all"
constructor({
app,
testTracer,
routeBasedSpanName,
addTag
}) {
this._app = app;
this._testTracer = testTracer;
this._routeBasedSpanName = routeBasedSpanName;
this._addTag = addTag;
this._localServiceName = testTracer.tracer().localEndpoint.serviceName;
this._ipv4 = InetAddress.getLocalAddress().ipv4();
}
_spanName(route) {
if (!this._routeBasedSpanName) return 'get';
if (route === '/weather/peking') return 'get redirected';
if (route === '/pathno') return 'get not_found';
return `get ${route}`.toLowerCase();
}
successSpan({path, city, status = 200}) {
return ({
name: this._spanName(path),
kind: 'SERVER',
localEndpoint: {serviceName: this._localServiceName, ipv4: this._ipv4},
tags: {
'http.path': path,
'http.status_code': status.toString(),
city
}
});
}
errorSpan({path, city, status}) {
const result = {
name: this._spanName(path),
kind: 'SERVER',
localEndpoint: {serviceName: this._localServiceName, ipv4: this._ipv4},
tags: {
'http.path': path,
'http.status_code': status.toString(),
error: status.toString(), // TODO: better error message especially on 500
}
};
if (city) result.tags.city = city;
return result;
}
baseURL() {
return this._baseURL;
}
tracer() {
return this._testTracer;
}
}
// Sets up an http server test fixture which runs basic tests.
//
// Installation should be like this:
//
// ```javascript
// const serverFixture = require('../../../test/httpServerTestFixture');
//
// serverFixture.setupAllHttpServerTests({middlewareFunction});
// ```
//
// If some tests fail, you can instead install only the basic tests first.
//
// ```javascript
// const serverFixture = require('../../../test/httpServerTestFixture');
//
// serverFixture.setupBasicHttpServerTests({middlewareFunction});
// ```
//
// ## Implementing the middleware function
//
// The middlewareFunction takes options of {tracer, routes} and returns middleware object that
// serves routes corresponding to the inputs.
//
// Ex. for express
// ```javascript
// function middlewareFunction({tracer, routes}) {
// const app = express();
// app.use(middleware({tracer}));
// routes.forEach((route) => {
// app.get(route.path, (req, res) => route.handle(req, ({redirect, body, code}) => {
// if (redirect) {
// return res.redirect(redirect);
// } else if (body) {
// return res.json(body);
// } else if (code) {
// return res.send(code);
// }
// return res.send();
// }));
// });
// return app;
// }
// ```
//
// ### Not found (404) handling
//
// Not all instrumentation hooks into unmatched paths (ex http status 404). If yours does, configure
// like so:
//
// Ex.
// ```javascript
// const testServer = serverFixture.setupBasicHttpServerTests({middlewareFunction});
// serverFixture.setupNotFoundTest(testServer);
// ```
//
// ### Https
//
// If your middleware supports usage via 'https', configure like so:
//
// Ex.
// ```javascript
// const testServer = serverFixture.setupBasicHttpServerTests({middlewareFunction});
// serverFixture.setupHttpsServerTest(testServer);
// ```
//
// ## Composition approach
//
// Approach to compose tests is from https://github.com/mochajs/mocha/wiki/Shared-Behaviours
function setupBasicHttpServerTests({
middlewareFunction,
routeBasedSpanName = false,
serverFunction = (app, onListen) => {
const server = app.listen(0, () => onListen(server.address().port));
return server;
},
addTag = (tracer, request, key, value) => tracer.recordBinary(key, value)
}) {
const serviceName = 'weather-api';
const testTracer = setupTestTracer({localServiceName: serviceName});
const tracer = testTracer.tracer();
const routes = [];
const wuhan = {
city: 'wuhan',
path: '/weather/wuhan',
handle: (request, responseCallback) => {
addTag(tracer, request, 'city', 'wuhan');
return responseCallback({body: request.headers});
}
};
routes.push(wuhan);
const beijing = {
city: 'beijing',
path: '/weather/beijing',
handle: (request, responseCallback) => {
addTag(tracer, request, 'city', 'beijing');
return responseCallback({body: request.headers});
}
};
routes.push(beijing);
const peking = {
city: 'peking',
path: '/weather/peking',
handle: (request, responseCallback) => {
addTag(tracer, request, 'city', 'peking');
return responseCallback({redirect: beijing.path});
}
};
routes.push(peking);
const shenzhen = {
city: 'shenzhen',
path: '/weather/shenzhen',
handle: (request, responseCallback) => new Promise(done => setTimeout(() => {
tracer.letId(request._trace_id, () => {
addTag(tracer, request, 'city', 'shenzhen');
done();
});
}, 10)).then(() => responseCallback({}))
};
routes.push(shenzhen);
const tieling = {
city: 'tieling',
path: '/weather/tieling',
handle: (request, responseCallback) => new Promise(done => setTimeout(() => {
responseCallback({body: done()});
}, 4))
};
routes.push(tieling);
const securedTown = {
city: 'securedTown',
path: '/weather/securedTown',
handle: (request, responseCallback) => {
addTag(tracer, request, 'city', 'securedTown');
return responseCallback({code: 401});
}
};
routes.push(securedTown);
const bagCity = {
city: 'bagCity',
path: '/weather/bagCity',
handle: (request) => {
addTag(tracer, request, 'city', 'bagCity');
throw new Error('service is dead');
}
};
routes.push(bagCity);
const app = middlewareFunction({tracer, routes});
const testServer = new TestServer({
app,
testTracer,
routeBasedSpanName,
addTag
});
let server;
let baseURL;
before((done) => { // eslint-disable-line no-undef
server = serverFunction(app, (listenPort) => {
baseURL = `http://127.0.0.1:${listenPort}`;
testServer._baseURL = baseURL;
done();
});
});
after(() => { // eslint-disable-line no-undef
if (server) server.close();
});
it('should start a new trace', () => {
const {path, city} = wuhan;
fetch(`${baseURL}${path}`)
.then(() => testTracer.expectNextSpanToEqual(testServer.successSpan({path, city})));
});
it('should record a reasonably accurate span duration', () => {
const {path} = tieling;
return fetch(`${baseURL}${path}`).then(() => {
// 50 years ago, Changchun, the capital of Jilin province, had only one railway to south.
// Tieling (铁岭站) is the city at third stop station, hence stopping 3ms.
expect(testTracer.popSpan().duration / 1000.0).to.be.greaterThan(3);
});
});
it('http.path tag should not include query parameters', () => {
const {path} = wuhan;
return fetch(`${baseURL}${path}?index=10&count=300`)
.then(() => expect(testTracer.popSpan().tags['http.path']).to.equal(path));
});
// Until there is a CLS hooked implementation here, we need to be explicit with trace IDs.
// See https://github.com/openzipkin/zipkin-js/issues/88
it('should add _trace_id to request for explicit instrumentation', () => {
const {path, city} = shenzhen;
return fetch(`${baseURL}${path}`)
.then(() => testTracer.expectNextSpanToEqual(testServer.successSpan({path, city})));
});
it('should continue a trace from the client', () => {
const {path, city} = wuhan;
return fetch(`${baseURL}${path}`, {
method: 'get',
headers: {
'X-B3-TraceId': '863ac35c9f6413ad',
'X-B3-SpanId': '48485a3953bb6124',
'X-B3-Flags': '1'
}
}).then(() => {
const baseSpan = testServer.successSpan({path, city});
const span = testTracer.expectNextSpanToEqual({...baseSpan, ...{debug: true, shared: true}});
expect(span.traceId).to.equal('863ac35c9f6413ad');
expect(span.id).to.equal('48485a3953bb6124');
});
});
it('should accept a 128bit X-B3-TraceId', () => {
const traceId = '863ac35c9f6413ad48485a3953bb6124';
const {path} = wuhan;
return fetch(`${baseURL}${path}`, {
method: 'get',
headers: {
'X-B3-TraceId': traceId,
'X-B3-SpanId': '48485a3953bb6124',
'X-B3-Sampled': '1'
}
}).then(() => expect(testTracer.popSpan().traceId).to.equal(traceId));
});
it('should report 302 in tags, but not as error', () => {
const {path, city} = peking;
const expectedSpan = testServer.successSpan({path, city, status: 302});
return fetch(`${baseURL}${path}`, {redirect: 'manual'})
.then(() => testTracer.expectNextSpanToEqual(expectedSpan));
});
it('should report 401 in tags', () => {
const {path, city} = securedTown;
const expectedSpan = testServer.errorSpan({path, city, status: 401});
return fetch(`${baseURL}${path}`).then(() => testTracer.expectNextSpanToEqual(expectedSpan));
});
it('should report 500 in tags', () => {
const {path, city} = bagCity;
const expectedSpan = testServer.errorSpan({path, city, status: 500});
return fetch(`${baseURL}${path}`).then(() => testTracer.expectNextSpanToEqual(expectedSpan));
});
return testServer;
}
function setupNotFoundTest(testServer) {
it('should report 404 in tags', () => {
const path = '/pathno';
const expectedSpan = testServer.errorSpan({path, status: 404});
return fetch(`${testServer._baseURL}${path}`)
.then(() => testServer.tracer().expectNextSpanToEqual(expectedSpan));
});
}
function setupHttpsServerTest({
testServer,
httpsServerFunction = (options, app, onListen) => {
const httpsServer = https.createServer(options, app)
.listen(0, () => onListen(httpsServer.address().port));
return httpsServer;
}
}) {
const testTracer = testServer.tracer();
let httpsServer;
let baseHttpsURL;
before((done) => { // eslint-disable-line no-undef
const options = {
rejectUnauthorized: false,
key: fs.readFileSync(fsPath.join(__dirname, 'keys', 'server.key'), 'utf8'),
cert: fs.readFileSync(fsPath.join(__dirname, 'keys', 'server.crt'), 'utf8'),
};
httpsServer = httpsServerFunction(options, testServer._app, (listenPort) => {
baseHttpsURL = `https://localhost:${listenPort}`;
done();
});
});
after(() => { // eslint-disable-line no-undef
if (httpsServer) httpsServer.close();
});
it('should work with https', () => {
const path = '/weather/wuhan';
return fetch(`${baseHttpsURL}${path}`, {
agent: new https.Agent({rejectUnauthorized: false})
}).then(() => testTracer.expectNextSpanToEqual(testServer.successSpan({path, city: 'wuhan'})));
});
}
function setupAllHttpServerTests(options) {
const testServer = setupBasicHttpServerTests(options);
setupHttpsServerTest({...{testServer}, ...options});
setupNotFoundTest(testServer);
return testServer;
}
module.exports = {
setupAllHttpServerTests,
setupBasicHttpServerTests,
setupNotFoundTest,
setupHttpsServerTest
};