Name: js-handler/node_modules/restify/node_modules/bunyan/test/serializers.test.js
| 1: | /* |
| 2: | * Copyright (c) 2012 Trent Mick. All rights reserved. |
| 3: | * |
| 4: | * Test the standard serializers in Bunyan. |
| 5: | */ |
| 6: | |
| 7: | var http = require('http'); |
| 8: | |
| 9: | var bunyan = require('../lib/bunyan'); |
| 10: | var verror = require('verror'); |
| 11: | |
| 12: | // node-tap API |
| 13: | if (require.cache[__dirname + '/tap4nodeunit.js']) |
| 14: | delete require.cache[__dirname + '/tap4nodeunit.js']; |
| 15: | var tap4nodeunit = require('./tap4nodeunit.js'); |
| 16: | var after = tap4nodeunit.after; |
| 17: | var before = tap4nodeunit.before; |
| 18: | var test = tap4nodeunit.test; |
| 19: | |
| 20: | |
| 21: | function CapturingStream(recs) { |
| 22: | this.recs = recs; |
| 23: | } |
| 24: | CapturingStream.prototype.write = function (rec) { |
| 25: | this.recs.push(rec); |
| 26: | } |
| 27: | |
| 28: | |
| 29: | test('req serializer', function (t) { |
| 30: | var records = []; |
| 31: | var log = bunyan.createLogger({ |
| 32: | name: 'serializer-test', |
| 33: | streams: [ |
| 34: | { |
| 35: | stream: new CapturingStream(records), |
| 36: | type: 'raw' |
| 37: | } |
| 38: | ], |
| 39: | serializers: { |
| 40: | req: bunyan.stdSerializers.req |
| 41: | } |
| 42: | }); |
| 43: | |
| 44: | // None of these should blow up. |
| 45: | var bogusReqs = [ |
| 46: | undefined, |
| 47: | null, |
| 48: | {}, |
| 49: | 1, |
| 50: | 'string', |
| 51: | [1, 2, 3], |
| 52: | {'foo':'bar'} |
| 53: | ]; |
| 54: | for (var i = 0; i < bogusReqs.length; i++) { |
| 55: | log.info({req: bogusReqs[i]}, 'hi'); |
| 56: | t.equal(records[i].req, bogusReqs[i]); |
| 57: | } |
| 58: | |
| 59: | // Get http request and response objects to play with and test. |
| 60: | var theReq, theRes; |
| 61: | var server = http.createServer(function (req, res) { |
| 62: | theReq = req; |
| 63: | theRes = res; |
| 64: | res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 65: | res.end('Hello World\n'); |
| 66: | }) |
| 67: | server.listen(8765, function () { |
| 68: | http.get({host: '127.0.0.1', port: 8765, path: '/'}, function (res) { |
| 69: | res.resume(); |
| 70: | log.info({req: theReq}, 'the request'); |
| 71: | var lastRecord = records[records.length-1]; |
| 72: | t.equal(lastRecord.req.method, 'GET'); |
| 73: | t.equal(lastRecord.req.url, theReq.url); |
| 74: | t.equal(lastRecord.req.remoteAddress, |
| 75: | theReq.connection.remoteAddress); |
| 76: | t.equal(lastRecord.req.remotePort, theReq.connection.remotePort); |
| 77: | server.close(); |
| 78: | t.end(); |
| 79: | }).on('error', function (err) { |
| 80: | t.ok(false, 'error requesting to our test server: ' + err); |
| 81: | server.close(); |
| 82: | t.end(); |
| 83: | }); |
| 84: | }); |
| 85: | }); |
| 86: | |
| 87: | |
| 88: | test('res serializer', function (t) { |
| 89: | var records = []; |
| 90: | var log = bunyan.createLogger({ |
| 91: | name: 'serializer-test', |
| 92: | streams: [ |
| 93: | { |
| 94: | stream: new CapturingStream(records), |
| 95: | type: 'raw' |
| 96: | } |
| 97: | ], |
| 98: | serializers: { |
| 99: | res: bunyan.stdSerializers.res |
| 100: | } |
| 101: | }); |
| 102: | |
| 103: | // None of these should blow up. |
| 104: | var bogusRess = [ |
| 105: | undefined, |
| 106: | null, |
| 107: | {}, |
| 108: | 1, |
| 109: | 'string', |
| 110: | [1, 2, 3], |
| 111: | {'foo':'bar'} |
| 112: | ]; |
| 113: | for (var i = 0; i < bogusRess.length; i++) { |
| 114: | log.info({res: bogusRess[i]}, 'hi'); |
| 115: | t.equal(records[i].res, bogusRess[i]); |
| 116: | } |
| 117: | |
| 118: | // Get http request and response objects to play with and test. |
| 119: | var theReq, theRes; |
| 120: | var server = http.createServer(function (req, res) { |
| 121: | theReq = req; |
| 122: | theRes = res; |
| 123: | res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 124: | res.end('Hello World\n'); |
| 125: | }) |
| 126: | server.listen(8765, function () { |
| 127: | http.get({host: '127.0.0.1', port: 8765, path: '/'}, function (res) { |
| 128: | res.resume(); |
| 129: | log.info({res: theRes}, 'the response'); |
| 130: | var lastRecord = records[records.length-1]; |
| 131: | t.equal(lastRecord.res.statusCode, theRes.statusCode); |
| 132: | t.equal(lastRecord.res.header, theRes._header); |
| 133: | server.close(); |
| 134: | t.end(); |
| 135: | }).on('error', function (err) { |
| 136: | t.ok(false, 'error requesting to our test server: ' + err); |
| 137: | server.close(); |
| 138: | t.end(); |
| 139: | }); |
| 140: | }); |
| 141: | }); |
| 142: | |
| 143: | |
| 144: | test('err serializer', function (t) { |
| 145: | var records = []; |
| 146: | var log = bunyan.createLogger({ |
| 147: | name: 'serializer-test', |
| 148: | streams: [ |
| 149: | { |
| 150: | stream: new CapturingStream(records), |
| 151: | type: 'raw' |
| 152: | } |
| 153: | ], |
| 154: | serializers: { |
| 155: | err: bunyan.stdSerializers.err |
| 156: | } |
| 157: | }); |
| 158: | |
| 159: | // None of these should blow up. |
| 160: | var bogusErrs = [ |
| 161: | undefined, |
| 162: | null, |
| 163: | {}, |
| 164: | 1, |
| 165: | 'string', |
| 166: | [1, 2, 3], |
| 167: | {'foo':'bar'} |
| 168: | ]; |
| 169: | for (var i = 0; i < bogusErrs.length; i++) { |
| 170: | log.info({err: bogusErrs[i]}, 'hi'); |
| 171: | t.equal(records[i].err, bogusErrs[i]); |
| 172: | } |
| 173: | |
| 174: | var theErr = new TypeError('blah'); |
| 175: | |
| 176: | log.info(theErr, 'the error'); |
| 177: | var lastRecord = records[records.length-1]; |
| 178: | t.equal(lastRecord.err.message, theErr.message); |
| 179: | t.equal(lastRecord.err.name, theErr.name); |
| 180: | t.equal(lastRecord.err.stack, theErr.stack); |
| 181: | t.end(); |
| 182: | }); |
| 183: | |
| 184: | |
| 185: | test('err serializer: long stack', function (t) { |
| 186: | var records = []; |
| 187: | var log = bunyan.createLogger({ |
| 188: | name: 'serializer-test', |
| 189: | streams: [ { |
| 190: | stream: new CapturingStream(records), |
| 191: | type: 'raw' |
| 192: | } ], |
| 193: | serializers: { |
| 194: | err: bunyan.stdSerializers.err |
| 195: | } |
| 196: | }); |
| 197: | |
| 198: | var topErr, midErr, bottomErr; |
| 199: | |
| 200: | // Just a VError. |
| 201: | topErr = new verror.VError('top err'); |
| 202: | log.info(topErr, 'the error'); |
| 203: | var lastRecord = records[records.length-1]; |
| 204: | t.equal(lastRecord.err.message, topErr.message, 'Just a VError'); |
| 205: | t.equal(lastRecord.err.name, topErr.name, 'Just a VError'); |
| 206: | t.equal(lastRecord.err.stack, topErr.stack, 'Just a VError'); |
| 207: | |
| 208: | // Just a WError. |
| 209: | topErr = new verror.WError('top err'); |
| 210: | log.info(topErr, 'the error'); |
| 211: | var lastRecord = records[records.length-1]; |
| 212: | t.equal(lastRecord.err.message, topErr.message, 'Just a WError'); |
| 213: | t.equal(lastRecord.err.name, topErr.name, 'Just a WError'); |
| 214: | t.equal(lastRecord.err.stack, topErr.stack, 'Just a WError'); |
| 215: | |
| 216: | // WError <- TypeError |
| 217: | bottomErr = new TypeError('bottom err'); |
| 218: | topErr = new verror.WError(bottomErr, 'top err'); |
| 219: | log.info(topErr, 'the error'); |
| 220: | var lastRecord = records[records.length-1]; |
| 221: | t.equal(lastRecord.err.message, topErr.message, 'WError <- TypeError'); |
| 222: | t.equal(lastRecord.err.name, topErr.name, 'WError <- TypeError'); |
| 223: | var expectedStack = topErr.stack + '\nCaused by: ' + bottomErr.stack; |
| 224: | t.equal(lastRecord.err.stack, expectedStack, 'WError <- TypeError'); |
| 225: | |
| 226: | // WError <- WError |
| 227: | bottomErr = new verror.WError('bottom err'); |
| 228: | topErr = new verror.WError(bottomErr, 'top err'); |
| 229: | log.info(topErr, 'the error'); |
| 230: | var lastRecord = records[records.length-1]; |
| 231: | t.equal(lastRecord.err.message, topErr.message, 'WError <- WError'); |
| 232: | t.equal(lastRecord.err.name, topErr.name, 'WError <- WError'); |
| 233: | var expectedStack = topErr.stack + '\nCaused by: ' + bottomErr.stack; |
| 234: | t.equal(lastRecord.err.stack, expectedStack, 'WError <- WError'); |
| 235: | |
| 236: | // WError <- WError <- TypeError |
| 237: | bottomErr = new TypeError('bottom err'); |
| 238: | midErr = new verror.WError(bottomErr, 'mid err'); |
| 239: | topErr = new verror.WError(midErr, 'top err'); |
| 240: | log.info(topErr, 'the error'); |
| 241: | var lastRecord = records[records.length-1]; |
| 242: | t.equal(lastRecord.err.message, topErr.message, |
| 243: | 'WError <- WError <- TypeError'); |
| 244: | t.equal(lastRecord.err.name, topErr.name, 'WError <- WError <- TypeError'); |
| 245: | var expectedStack = (topErr.stack |
| 246: | + '\nCaused by: ' + midErr.stack |
| 247: | + '\nCaused by: ' + bottomErr.stack); |
| 248: | t.equal(lastRecord.err.stack, expectedStack, |
| 249: | 'WError <- WError <- TypeError'); |
| 250: | |
| 251: | // WError <- WError <- WError |
| 252: | bottomErr = new verror.WError('bottom err'); |
| 253: | midErr = new verror.WError(bottomErr, 'mid err'); |
| 254: | topErr = new verror.WError(midErr, 'top err'); |
| 255: | log.info(topErr, 'the error'); |
| 256: | var lastRecord = records[records.length-1]; |
| 257: | t.equal(lastRecord.err.message, topErr.message, |
| 258: | 'WError <- WError <- WError'); |
| 259: | t.equal(lastRecord.err.name, topErr.name, 'WError <- WError <- WError'); |
| 260: | var expectedStack = (topErr.stack |
| 261: | + '\nCaused by: ' + midErr.stack |
| 262: | + '\nCaused by: ' + bottomErr.stack); |
| 263: | t.equal(lastRecord.err.stack, expectedStack, 'WError <- WError <- WError'); |
| 264: | |
| 265: | t.end(); |
| 266: | }); |
| 267: | |
| 268: | |
| 269: | // Bunyan 0.18.3 introduced a bug where *all* serializers are applied |
| 270: | // even if the log record doesn't have the associated key. That means |
| 271: | // serializers that don't handle an `undefined` value will blow up. |
| 272: | test('do not apply serializers if no record key', function (t) { |
| 273: | var records = []; |
| 274: | var log = bunyan.createLogger({ |
| 275: | name: 'serializer-test', |
| 276: | streams: [ { |
| 277: | stream: new CapturingStream(records), |
| 278: | type: 'raw' |
| 279: | } ], |
| 280: | serializers: { |
| 281: | err: bunyan.stdSerializers.err, |
| 282: | boom: function (value) { |
| 283: | throw new Error('boom'); |
| 284: | } |
| 285: | } |
| 286: | }); |
| 287: | |
| 288: | log.info({foo: 'bar'}, 'record one'); |
| 289: | log.info({err: new Error('record two err')}, 'record two'); |
| 290: | |
| 291: | t.equal(records[0].boom, undefined); |
| 292: | t.equal(records[0].foo, 'bar'); |
| 293: | t.equal(records[1].boom, undefined); |
| 294: | t.equal(records[1].err.message, 'record two err'); |
| 295: | |
| 296: | t.end(); |
| 297: | }); |
