log4js-node/test/connect-logger-test.js

296 lines
8.5 KiB
JavaScript
Raw Permalink Normal View History

2013-06-18 06:47:18 +08:00
/* jshint maxparams:7 */
2013-05-29 06:42:09 +08:00
"use strict";
var vows = require('vows')
, assert = require('assert')
2014-11-05 17:49:21 +08:00
, util = require('util')
, EE = require('events').EventEmitter
, levels = require('../lib/levels');
2011-04-07 08:19:18 +08:00
function MockLogger() {
2013-05-29 06:42:09 +08:00
var that = this;
this.messages = [];
2013-05-29 06:42:09 +08:00
this.log = function(level, message, exception) {
that.messages.push({ level: level, message: message });
};
2011-04-07 08:19:18 +08:00
2013-05-29 06:42:09 +08:00
this.isLevelEnabled = function(level) {
return level.isGreaterThanOrEqualTo(that.level);
};
2013-05-29 06:42:09 +08:00
this.level = levels.TRACE;
2011-04-07 08:19:18 +08:00
}
2013-06-18 06:47:18 +08:00
function MockRequest(remoteAddr, method, originalUrl, headers) {
2011-04-07 08:19:18 +08:00
2013-05-29 06:42:09 +08:00
this.socket = { remoteAddress: remoteAddr };
this.originalUrl = originalUrl;
this.method = method;
this.httpVersionMajor = '5';
this.httpVersionMinor = '0';
2013-06-18 06:47:18 +08:00
this.headers = headers || {};
2011-04-07 08:19:18 +08:00
2013-06-18 06:47:18 +08:00
var self = this;
Object.keys(this.headers).forEach(function(key) {
self.headers[key.toLowerCase()] = self.headers[key];
});
2011-04-07 08:19:18 +08:00
}
2013-06-17 14:01:22 +08:00
function MockResponse() {
2014-11-05 17:49:21 +08:00
var r = this;
this.end = function(chunk, encoding) {
2014-11-05 17:58:09 +08:00
r.emit('finish');
2013-05-29 06:42:09 +08:00
};
2011-04-07 08:19:18 +08:00
2013-06-17 14:01:22 +08:00
this.writeHead = function(code, headers) {
2014-11-05 17:49:21 +08:00
this.statusCode = code;
this._headers = headers;
2013-06-17 14:01:22 +08:00
};
}
2014-11-05 17:49:21 +08:00
util.inherits(MockResponse, EE);
2013-06-18 06:47:18 +08:00
function request(cl, method, url, code, reqHeaders, resHeaders) {
var req = new MockRequest('my.remote.addr', method, url, reqHeaders);
2013-06-17 14:01:22 +08:00
var res = new MockResponse();
cl(req, res, function() {});
2013-06-18 06:47:18 +08:00
res.writeHead(code, resHeaders);
2013-06-17 14:01:22 +08:00
res.end('chunk','encoding');
2011-04-07 08:19:18 +08:00
}
vows.describe('log4js connect logger').addBatch({
2013-05-29 06:42:09 +08:00
'getConnectLoggerModule': {
topic: function() {
var clm = require('../lib/connect-logger');
return clm;
},
2013-05-29 06:42:09 +08:00
'should return a "connect logger" factory' : function(clm) {
assert.isObject(clm);
},
'take a log4js logger and return a "connect logger"' : {
topic: function(clm) {
var ml = new MockLogger();
var cl = clm.connectLogger(ml);
return cl;
},
2013-05-29 06:42:09 +08:00
'should return a "connect logger"': function(cl) {
assert.isFunction(cl);
}
},
2013-05-29 06:42:09 +08:00
'log events' : {
topic: function(clm) {
var ml = new MockLogger();
var cl = clm.connectLogger(ml);
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-06-17 14:01:22 +08:00
request(cl, 'GET', 'http://url', 200);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10);
2013-05-29 06:42:09 +08:00
},
'check message': function(messages) {
assert.isArray(messages);
assert.equal(messages.length, 1);
assert.ok(levels.INFO.isEqualTo(messages[0].level));
assert.include(messages[0].message, 'GET');
assert.include(messages[0].message, 'http://url');
assert.include(messages[0].message, 'my.remote.addr');
assert.include(messages[0].message, '200');
}
},
'log events with level below logging level' : {
topic: function(clm) {
var ml = new MockLogger();
ml.level = levels.FATAL;
var cl = clm.connectLogger(ml);
2013-06-17 14:01:22 +08:00
request(cl, 'GET', 'http://url', 200);
2013-05-29 06:42:09 +08:00
return ml.messages;
},
2013-05-29 06:42:09 +08:00
'check message': function(messages) {
assert.isArray(messages);
assert.isEmpty(messages);
}
},
'log events with non-default level and custom format' : {
topic: function(clm) {
var ml = new MockLogger();
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-05-29 06:42:09 +08:00
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, { level: levels.INFO, format: ':method :url' } );
2013-06-17 14:01:22 +08:00
request(cl, 'GET', 'http://url', 200);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10); },
2013-05-29 06:42:09 +08:00
'check message': function(messages) {
assert.isArray(messages);
assert.equal(messages.length, 1);
assert.ok(levels.INFO.isEqualTo(messages[0].level));
assert.equal(messages[0].message, 'GET http://url');
}
2013-06-17 14:01:22 +08:00
},
'logger with options as string': {
topic: function(clm) {
var ml = new MockLogger();
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-06-17 14:01:22 +08:00
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, ':method :url');
request(cl, 'POST', 'http://meh', 200);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10);
2013-06-17 14:01:22 +08:00
},
'should use the passed in format': function(messages) {
assert.equal(messages[0].message, 'POST http://meh');
}
},
'auto log levels': {
topic: function(clm) {
var ml = new MockLogger();
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-06-17 14:01:22 +08:00
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, { level: 'auto', format: ':method :url' });
request(cl, 'GET', 'http://meh', 200);
request(cl, 'GET', 'http://meh', 201);
request(cl, 'GET', 'http://meh', 302);
request(cl, 'GET', 'http://meh', 404);
request(cl, 'GET', 'http://meh', 500);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10);
2013-06-17 14:01:22 +08:00
},
'should use INFO for 2xx': function(messages) {
assert.ok(levels.INFO.isEqualTo(messages[0].level));
assert.ok(levels.INFO.isEqualTo(messages[1].level));
},
'should use WARN for 3xx': function(messages) {
assert.ok(levels.WARN.isEqualTo(messages[2].level));
},
'should use ERROR for 4xx': function(messages) {
assert.ok(levels.ERROR.isEqualTo(messages[3].level));
},
'should use ERROR for 5xx': function(messages) {
assert.ok(levels.ERROR.isEqualTo(messages[4].level));
}
},
'format using a function': {
topic: function(clm) {
var ml = new MockLogger();
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-06-17 14:01:22 +08:00
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, function(req, res, formatFn) { return "I was called"; });
request(cl, 'GET', 'http://blah', 200);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10);
2013-06-17 14:01:22 +08:00
},
'should call the format function': function(messages) {
assert.equal(messages[0].message, 'I was called');
}
2013-06-18 06:47:18 +08:00
},
'format that includes request headers': {
topic: function(clm) {
var ml = new MockLogger();
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-06-18 06:47:18 +08:00
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, ':req[Content-Type]');
request(
cl,
'GET', 'http://blah', 200,
2013-06-18 06:47:18 +08:00
{ 'Content-Type': 'application/json' }
);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10);
2013-06-18 06:47:18 +08:00
},
'should output the request header': function(messages) {
assert.equal(messages[0].message, 'application/json');
}
},
'format that includes response headers': {
topic: function(clm) {
var ml = new MockLogger();
2014-11-05 17:49:21 +08:00
var cb = this.callback;
2013-06-18 06:47:18 +08:00
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, ':res[Content-Type]');
request(
cl,
'GET', 'http://blah', 200,
null,
{ 'Content-Type': 'application/cheese' }
);
2014-11-05 17:49:21 +08:00
setTimeout(function() {
cb(null, ml.messages);
},10);
2013-06-18 06:47:18 +08:00
},
'should output the response header': function(messages) {
assert.equal(messages[0].message, 'application/cheese');
}
},
'log events with custom token' : {
topic: function(clm) {
var ml = new MockLogger();
var cb = this.callback;
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, { level: levels.INFO, format: ':method :url :custom_string', tokens: [{
token: ':custom_string', replacement: 'fooBAR'
}] } );
request(cl, 'GET', 'http://url', 200);
setTimeout(function() {
cb(null, ml.messages);
},10);
},
'check message': function(messages) {
assert.isArray(messages);
assert.equal(messages.length, 1);
assert.ok(levels.INFO.isEqualTo(messages[0].level));
assert.equal(messages[0].message, 'GET http://url fooBAR');
}
},
'log events with custom override token' : {
topic: function(clm) {
var ml = new MockLogger();
var cb = this.callback;
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, { level: levels.INFO, format: ':method :url :date', tokens: [{
token: ':date', replacement: "20150310"
}] } );
request(cl, 'GET', 'http://url', 200);
setTimeout(function() {
cb(null, ml.messages);
},10);
},
'check message': function(messages) {
assert.isArray(messages);
assert.equal(messages.length, 1);
assert.ok(levels.INFO.isEqualTo(messages[0].level));
assert.equal(messages[0].message, 'GET http://url 20150310');
}
}
2013-05-29 06:42:09 +08:00
}
2011-04-07 08:19:18 +08:00
}).export(module);