forked from joaquimserafim/superagent-bunyan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
480 lines (401 loc) · 14.7 KB
/
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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
eslint
no-multi-spaces: ["error", {exceptions: {"VariableDeclarator": true}}]
padded-blocks: ["error", {"classes": "always"}]
max-len: ["error", 80]
*/
'use strict'
const express = require('express')
const request = require('superagent')
const bunyan = require('bunyan')
const mocha = require('mocha')
const expect = require('chai').expect
const spy = require('fn-spy')
const EE = require('events')
const it = mocha.it
const describe = mocha.describe
const before = mocha.before
const after = mocha.after
const superagentLogger = require('.')
const app = express()
// keep in memory
const ringbuffer = new bunyan.RingBuffer({ limit: 100 })
const logger = bunyan.createLogger(
{
name: 'superagent-bunyan',
streams: [
{
level: 'trace',
type: 'raw',
stream: ringbuffer
}
]
}
)
var server
describe('superagent-bunyan', () => {
describe('unit', () => {
it('when follows the normal path', (done) => {
const resBody = {statusCode: 200, header: {}, body: 'hello'}
let requestEmitter = new EE()
superagentLogger(logger)(requestEmitter)
let spyEnd = spy.emitter()
let spyResponse = spy.emitter()
let spyError = spy.emitter()
requestEmitter.on('end', spyEnd)
requestEmitter.on('response', spyResponse)
requestEmitter.on('error', spyError)
requestEmitter.emit('end')
requestEmitter.emit('response', resBody)
expect(spyEnd.calledCount()).to.be.deep.equal(1)
expect(spyResponse.calledCount()).to.be.deep.equal(1)
expect(spyError.calledCount()).to.be.deep.equal(0)
expect(spyEnd.calledWith()).to.be.deep.equal([[]])
expect(spyResponse.calledWith()).to.be.deep.equal([[resBody]])
expect(spyError.calledWith()).to.be.deep.equal([])
done()
})
it('when a http error happens', (done) => {
const error = 'We\'re not computers, Sebastian, we\'re physical'
let requestEmitter = new EE()
superagentLogger(logger)(requestEmitter)
let spyEnd = spy.emitter()
let spyResponse = spy.emitter()
let spyError = spy.emitter()
requestEmitter.on('end', spyEnd)
requestEmitter.on('response', spyResponse)
requestEmitter.on('error', spyError)
requestEmitter.emit('end')
requestEmitter.emit('error', error)
expect(spyEnd.calledCount()).to.be.deep.equal(1)
expect(spyResponse.calledCount()).to.be.deep.equal(0)
expect(spyError.calledCount()).to.be.deep.equal(1)
expect(spyEnd.calledWith()).to.be.deep.equal([[]])
expect(spyResponse.calledWith()).to.be.deep.equal([])
expect(spyError.calledWith()).to.be.deep.equal([[error]])
done()
})
it('when an error happens with a http timeout', (done) => {
const error = 'Error: Can the maker repair what he makes?'
let requestEmitter = new EE()
superagentLogger(logger)(requestEmitter)
let spyEnd = spy.emitter()
let spyResponse = spy.emitter()
let spyError = spy.emitter()
requestEmitter.on('end', spyEnd)
requestEmitter.on('response', spyResponse)
requestEmitter.on('error', spyError)
requestEmitter.emit('error', error)
expect(spyEnd.calledCount()).to.be.deep.equal(0)
expect(spyResponse.calledCount()).to.be.deep.equal(0)
expect(spyError.calledCount()).to.be.deep.equal(1)
expect(spyEnd.calledWith()).to.be.deep.equal([])
expect(spyResponse.calledWith()).to.be.deep.equal([])
expect(spyError.calledWith()).to.be.deep.equal([[error]])
done()
})
})
describe('integration', () => {
before((done) => {
// reset `ringbuffer.records`
ringbuffer.records = []
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.post('/', (req, res) => {
res.send('ok')
})
app.get('/whitrequestid', (req, res) => {
expect(req.headers).to.have.deep.property('x-request-id')
res.send('Hello World!')
})
app.get('/json', (req, res) => {
res.send({msg: 'Hello World!'})
})
app.get('/someuuid', (req, res) => {
expect(req.query).to.be.deep.equal({a: '123'})
expect(req.headers).to.have.deep.property('x-request-id')
expect(req.headers['x-request-id'])
.to.be.equal('Quite an experience to live in fear, isn\'t it?')
res.send('Hello World!')
})
app.get('/someuuidbutdontsend', (req, res) => {
expect(req.headers['x-request-id']).to.be.deep.equal(undefined)
res.send('Hello World!')
})
server = app.listen(3000, done)
})
after((done) => {
server.close(done)
})
it('normal request', (done) => {
request
.get('http://localhost:3000')
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.a('null')
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.deep.equal(undefined)
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('request with query string', (done) => {
request
.get('http://localhost:3000')
.query({a: 1, b: 2})
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('object')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('request with raw query string', (done) => {
request
.get('http://localhost:3000')
.query('a=1&b=2')
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.equal('a=1&b=2')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('send a request with payload', (done) => {
request
.post('http://localhost:3000')
.send({msg: 'More human than human is our motto'})
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.deep.equal(undefined)
expect(log1.req.method).to.be.equal('POST')
expect(log1.req.url).to.be
.equal('http://localhost:3000')
expect(log1.req.body).to.be.deep
.equal({msg: 'More human than human is our motto'})
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.deep.equal(200)
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('receive a response with a JSON payload', (done) => {
request
.get('http://localhost:3000/json')
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.body).to.be.deep.equal({msg: 'Hello World!'})
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('undefined')
expect(log1.req.path).to.be.equal('/json')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.body).to.be.deep.equal({msg: 'Hello World!'})
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('pick the X-Request-ID from the headers', (done) => {
request
.get('http://localhost:3000/someuuid')
.query({a: 123})
.set('X-Request-ID', 'Quite an experience to live in fear, isn\'t it?')
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('object')
expect(log1.req.path).to.be.equal('/someuuid')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('passing the request id directly to `superagent-bunyan`', (done) => {
request
.get('http://localhost:3000/someuuidbutdontsend')
.use(
superagentLogger(
logger,
'Quite an experience to live in fear, isn\'t it?'
)
)
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('undefined')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
done()
})
})
})
it('using `extra` to pass extra info in the log entry', (done) => {
request
.get('http://localhost:3000/someuuidbutdontsend')
.use(
superagentLogger(
logger,
{extra: 'But then again, who does?'}
)
)
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('undefined')
expect(log1.extra).to.be.equal('But then again, who does?')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.extra).to.be.equal('But then again, who does?')
done()
})
})
})
it('passing the request id and using `extra` arg', (done) => {
request
.get('http://localhost:3000/someuuidbutdontsend')
.use(
superagentLogger(
logger,
1234,
{extra: 'But then again, who does?'}
)
)
.end((err, res) => {
expect(err).to.be.deep.equal(null)
expect(res).to.be.an('object')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.an('undefined')
expect(log1.extra).to.be.equal('But then again, who does?')
expect(log2.err).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(200)
expect(log2.res.headers).to.be.an('object')
expect(log2.extra).to.be.equal('But then again, who does?')
done()
})
})
})
it('request with a http error', (done) => {
request
.get('http://localhost:3000/not_found')
.use(superagentLogger(logger))
.end((err, res) => {
expect(err).to.be.an('error')
expect(res.opDuration).to.be.a('number')
testLogRecords(getRecords(3), (log1, log2) => {
expect(log1.req.qs).to.be.deep.equal(undefined)
expect(log2.err).to.be.an('object')
expect(log2.res).to.be.an('object')
expect(log2.res.statusCode).to.be.equal(404)
expect(log2.res.headers).to.be.an('object')
expect(log2.err.name).to.be.equal('Error')
done()
})
})
})
it('request with connection error', (done) => {
request
.get('http://localhost:3001')
.use(superagentLogger(logger))
.end((err) => {
expect(err).to.be.an('error')
expect(err.opDuration).to.be.a('number')
testLogRecords(getRecords(), (log1, log2) => {
expect(log1.req.qs).to.be.deep.equal(undefined)
expect(log2.res).to.be.an('object')
expect(log2.err).to.be.an('object')
expect(log2.err.name).to.be.equal('Error')
done()
})
})
})
})
})
//
// test some of the props that should exist with the `log`
//
function testLogRecords (records, runExtraExpections) {
records.forEach(each)
runExtraExpections(records[0], records[1])
function each (record, index) {
// default values for both entries
expect(record.name).to.be.equal('superagent-bunyan')
expect(record.origin).to.be.equal('superagent')
expect(record).to.have.deep.property('req_id')
expect(record).to.have.deep.property('hostname')
expect(record).to.have.deep.property('pid')
expect(record).to.have.deep.property('level')
expect(record).to.have.deep.property('time')
// the initial request
if (!index) {
expect(record.msg).to.be.equal('start of the request')
expect(record.req).to.be.an('object')
expect(record.req.method).to.be.a('string')
expect(record.req.url).to.be.a('string')
expect(record.req.headers).to.be.an('object')
} else { // the response
expect(record.msg).to.be.equal('end of the request')
expect(record.duration).to.be.a('number')
}
}
}
//
// should return an array with the two entries
// 'start of the request and 'end of the request'
//
function getRecords (n) {
n = n || 2
return ringbuffer.records.splice(0, n).map(parse)
function parse (record) {
return JSON.parse(JSON.stringify(record))
}
}