2012-06-22 00:24:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Trent Mick. All rights reserved.
|
|
|
|
*
|
|
|
|
* Test the standard serializers in Bunyan.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
|
2012-10-11 21:02:48 +00:00
|
|
|
var bunyan = require('../lib/bunyan');
|
|
|
|
var verror = require('verror');
|
|
|
|
|
2013-01-07 19:18:12 +00:00
|
|
|
// node-tap API
|
|
|
|
if (require.cache[__dirname + '/tap4nodeunit.js'])
|
2013-03-29 00:42:32 +00:00
|
|
|
delete require.cache[__dirname + '/tap4nodeunit.js'];
|
2013-01-07 19:18:12 +00:00
|
|
|
var tap4nodeunit = require('./tap4nodeunit.js');
|
|
|
|
var after = tap4nodeunit.after;
|
|
|
|
var before = tap4nodeunit.before;
|
|
|
|
var test = tap4nodeunit.test;
|
|
|
|
|
2012-06-22 00:24:06 +00:00
|
|
|
|
|
|
|
function CapturingStream(recs) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.recs = recs;
|
2012-06-22 00:24:06 +00:00
|
|
|
}
|
|
|
|
CapturingStream.prototype.write = function (rec) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.recs.push(rec);
|
2012-06-22 00:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
test('req serializer', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var records = [];
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'serializer-test',
|
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
stream: new CapturingStream(records),
|
|
|
|
type: 'raw'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
serializers: {
|
|
|
|
req: bunyan.stdSerializers.req
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// None of these should blow up.
|
|
|
|
var bogusReqs = [
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
{},
|
|
|
|
1,
|
|
|
|
'string',
|
|
|
|
[1, 2, 3],
|
|
|
|
{'foo':'bar'}
|
|
|
|
];
|
|
|
|
for (var i = 0; i < bogusReqs.length; i++) {
|
|
|
|
log.info({req: bogusReqs[i]}, 'hi');
|
|
|
|
t.equal(records[i].req, bogusReqs[i]);
|
2012-06-22 00:24:06 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
// Get http request and response objects to play with and test.
|
|
|
|
var theReq, theRes;
|
|
|
|
var server = http.createServer(function (req, res) {
|
|
|
|
theReq = req;
|
|
|
|
theRes = res;
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.end('Hello World\n');
|
|
|
|
})
|
|
|
|
server.listen(8765, function () {
|
|
|
|
http.get({host: '127.0.0.1', port: 8765, path: '/'}, function (res) {
|
|
|
|
res.resume();
|
|
|
|
log.info({req: theReq}, 'the request');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.req.method, 'GET');
|
|
|
|
t.equal(lastRecord.req.url, theReq.url);
|
2013-04-02 00:21:01 +00:00
|
|
|
t.equal(lastRecord.req.remoteAddress,
|
|
|
|
theReq.connection.remoteAddress);
|
2013-03-29 00:42:32 +00:00
|
|
|
t.equal(lastRecord.req.remotePort, theReq.connection.remotePort);
|
|
|
|
server.close();
|
|
|
|
t.end();
|
|
|
|
}).on('error', function (err) {
|
|
|
|
t.ok(false, 'error requesting to our test server: ' + err);
|
|
|
|
server.close();
|
|
|
|
t.end();
|
|
|
|
});
|
2012-06-22 00:24:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('res serializer', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var records = [];
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'serializer-test',
|
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
stream: new CapturingStream(records),
|
|
|
|
type: 'raw'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
serializers: {
|
|
|
|
res: bunyan.stdSerializers.res
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// None of these should blow up.
|
|
|
|
var bogusRess = [
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
{},
|
|
|
|
1,
|
|
|
|
'string',
|
|
|
|
[1, 2, 3],
|
|
|
|
{'foo':'bar'}
|
|
|
|
];
|
|
|
|
for (var i = 0; i < bogusRess.length; i++) {
|
|
|
|
log.info({res: bogusRess[i]}, 'hi');
|
|
|
|
t.equal(records[i].res, bogusRess[i]);
|
2012-06-22 00:24:06 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
// Get http request and response objects to play with and test.
|
|
|
|
var theReq, theRes;
|
|
|
|
var server = http.createServer(function (req, res) {
|
|
|
|
theReq = req;
|
|
|
|
theRes = res;
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.end('Hello World\n');
|
|
|
|
})
|
|
|
|
server.listen(8765, function () {
|
|
|
|
http.get({host: '127.0.0.1', port: 8765, path: '/'}, function (res) {
|
|
|
|
res.resume();
|
|
|
|
log.info({res: theRes}, 'the response');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.res.statusCode, theRes.statusCode);
|
|
|
|
t.equal(lastRecord.res.header, theRes._header);
|
|
|
|
server.close();
|
|
|
|
t.end();
|
|
|
|
}).on('error', function (err) {
|
|
|
|
t.ok(false, 'error requesting to our test server: ' + err);
|
|
|
|
server.close();
|
|
|
|
t.end();
|
|
|
|
});
|
2012-06-22 00:24:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('err serializer', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var records = [];
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'serializer-test',
|
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
stream: new CapturingStream(records),
|
|
|
|
type: 'raw'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
serializers: {
|
|
|
|
err: bunyan.stdSerializers.err
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// None of these should blow up.
|
|
|
|
var bogusErrs = [
|
|
|
|
undefined,
|
|
|
|
null,
|
|
|
|
{},
|
|
|
|
1,
|
|
|
|
'string',
|
|
|
|
[1, 2, 3],
|
|
|
|
{'foo':'bar'}
|
|
|
|
];
|
|
|
|
for (var i = 0; i < bogusErrs.length; i++) {
|
|
|
|
log.info({err: bogusErrs[i]}, 'hi');
|
|
|
|
t.equal(records[i].err, bogusErrs[i]);
|
2012-06-22 00:24:06 +00:00
|
|
|
}
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
var theErr = new TypeError('blah');
|
|
|
|
|
|
|
|
log.info(theErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.err.message, theErr.message);
|
|
|
|
t.equal(lastRecord.err.name, theErr.name);
|
|
|
|
t.equal(lastRecord.err.stack, theErr.stack);
|
|
|
|
t.end();
|
2012-06-22 00:24:06 +00:00
|
|
|
});
|
2012-10-11 21:02:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
test('err serializer: long stack', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var records = [];
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'serializer-test',
|
|
|
|
streams: [ {
|
|
|
|
stream: new CapturingStream(records),
|
|
|
|
type: 'raw'
|
|
|
|
} ],
|
|
|
|
serializers: {
|
|
|
|
err: bunyan.stdSerializers.err
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var topErr, midErr, bottomErr;
|
|
|
|
|
|
|
|
// Just a VError.
|
|
|
|
topErr = new verror.VError('top err');
|
|
|
|
log.info(topErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.err.message, topErr.message, 'Just a VError');
|
|
|
|
t.equal(lastRecord.err.name, topErr.name, 'Just a VError');
|
|
|
|
t.equal(lastRecord.err.stack, topErr.stack, 'Just a VError');
|
|
|
|
|
|
|
|
// Just a WError.
|
|
|
|
topErr = new verror.WError('top err');
|
|
|
|
log.info(topErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.err.message, topErr.message, 'Just a WError');
|
|
|
|
t.equal(lastRecord.err.name, topErr.name, 'Just a WError');
|
|
|
|
t.equal(lastRecord.err.stack, topErr.stack, 'Just a WError');
|
|
|
|
|
|
|
|
// WError <- TypeError
|
|
|
|
bottomErr = new TypeError('bottom err');
|
|
|
|
topErr = new verror.WError(bottomErr, 'top err');
|
|
|
|
log.info(topErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.err.message, topErr.message, 'WError <- TypeError');
|
|
|
|
t.equal(lastRecord.err.name, topErr.name, 'WError <- TypeError');
|
|
|
|
var expectedStack = topErr.stack + '\nCaused by: ' + bottomErr.stack;
|
|
|
|
t.equal(lastRecord.err.stack, expectedStack, 'WError <- TypeError');
|
|
|
|
|
|
|
|
// WError <- WError
|
|
|
|
bottomErr = new verror.WError('bottom err');
|
|
|
|
topErr = new verror.WError(bottomErr, 'top err');
|
|
|
|
log.info(topErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
|
|
|
t.equal(lastRecord.err.message, topErr.message, 'WError <- WError');
|
|
|
|
t.equal(lastRecord.err.name, topErr.name, 'WError <- WError');
|
|
|
|
var expectedStack = topErr.stack + '\nCaused by: ' + bottomErr.stack;
|
|
|
|
t.equal(lastRecord.err.stack, expectedStack, 'WError <- WError');
|
|
|
|
|
|
|
|
// WError <- WError <- TypeError
|
|
|
|
bottomErr = new TypeError('bottom err');
|
|
|
|
midErr = new verror.WError(bottomErr, 'mid err');
|
|
|
|
topErr = new verror.WError(midErr, 'top err');
|
|
|
|
log.info(topErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
2013-04-02 00:21:01 +00:00
|
|
|
t.equal(lastRecord.err.message, topErr.message,
|
|
|
|
'WError <- WError <- TypeError');
|
2013-03-29 00:42:32 +00:00
|
|
|
t.equal(lastRecord.err.name, topErr.name, 'WError <- WError <- TypeError');
|
|
|
|
var expectedStack = (topErr.stack
|
|
|
|
+ '\nCaused by: ' + midErr.stack
|
|
|
|
+ '\nCaused by: ' + bottomErr.stack);
|
2013-04-02 00:21:01 +00:00
|
|
|
t.equal(lastRecord.err.stack, expectedStack,
|
|
|
|
'WError <- WError <- TypeError');
|
2013-03-29 00:42:32 +00:00
|
|
|
|
|
|
|
// WError <- WError <- WError
|
|
|
|
bottomErr = new verror.WError('bottom err');
|
|
|
|
midErr = new verror.WError(bottomErr, 'mid err');
|
|
|
|
topErr = new verror.WError(midErr, 'top err');
|
|
|
|
log.info(topErr, 'the error');
|
|
|
|
var lastRecord = records[records.length-1];
|
2013-04-02 00:21:01 +00:00
|
|
|
t.equal(lastRecord.err.message, topErr.message,
|
|
|
|
'WError <- WError <- WError');
|
2013-03-29 00:42:32 +00:00
|
|
|
t.equal(lastRecord.err.name, topErr.name, 'WError <- WError <- WError');
|
|
|
|
var expectedStack = (topErr.stack
|
|
|
|
+ '\nCaused by: ' + midErr.stack
|
|
|
|
+ '\nCaused by: ' + bottomErr.stack);
|
|
|
|
t.equal(lastRecord.err.stack, expectedStack, 'WError <- WError <- WError');
|
|
|
|
|
|
|
|
t.end();
|
2013-03-15 18:25:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Bunyan 0.18.3 introduced a bug where *all* serializers are applied
|
|
|
|
// even if the log record doesn't have the associated key. That means
|
|
|
|
// serializers that don't handle an `undefined` value will blow up.
|
|
|
|
test('do not apply serializers if no record key', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var records = [];
|
|
|
|
var log = bunyan.createLogger({
|
|
|
|
name: 'serializer-test',
|
|
|
|
streams: [ {
|
|
|
|
stream: new CapturingStream(records),
|
|
|
|
type: 'raw'
|
|
|
|
} ],
|
|
|
|
serializers: {
|
|
|
|
err: bunyan.stdSerializers.err,
|
|
|
|
boom: function (value) {
|
|
|
|
throw new Error('boom');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-03-15 18:25:56 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
log.info({foo: 'bar'}, 'record one');
|
|
|
|
log.info({err: new Error('record two err')}, 'record two');
|
2013-03-15 18:25:56 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.equal(records[0].boom, undefined);
|
|
|
|
t.equal(records[0].foo, 'bar');
|
|
|
|
t.equal(records[1].boom, undefined);
|
|
|
|
t.equal(records[1].err.message, 'record two err');
|
2012-10-11 21:02:48 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.end();
|
2012-10-11 21:02:48 +00:00
|
|
|
});
|