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

186 lines
5.1 KiB
JavaScript
Raw Normal View History

2013-05-29 06:42:09 +08:00
"use strict";
var vows = require('vows')
, assert = require('assert')
, 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 = [];
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);
};
this.level = levels.TRACE;
2011-04-07 08:19:18 +08:00
}
function MockRequest(remoteAddr, method, originalUrl) {
2013-05-29 06:42:09 +08:00
this.socket = { remoteAddress: remoteAddr };
this.originalUrl = originalUrl;
this.method = method;
this.httpVersionMajor = '5';
this.httpVersionMinor = '0';
this.headers = {};
2011-04-07 08:19:18 +08:00
}
2013-06-17 14:01:22 +08:00
function MockResponse() {
2013-05-29 06:42:09 +08:00
2013-06-17 14:01:22 +08:00
this.end = function(chunk, encoding) {
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) {
};
}
function request(cl, method, url, code) {
var req = new MockRequest('my.remote.addr', method, url);
var res = new MockResponse();
cl(req, res, function() {});
res.writeHead(code);
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;
},
'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;
},
'should return a "connect logger"': function(cl) {
assert.isFunction(cl);
}
},
'log events' : {
topic: function(clm) {
var ml = new MockLogger();
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;
},
'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;
},
'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();
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);
2013-05-29 06:42:09 +08:00
return ml.messages;
},
'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();
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, ':method :url');
request(cl, 'POST', 'http://meh', 200);
return ml.messages;
},
'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();
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);
return ml.messages;
},
'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();
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, function(req, res, formatFn) { return "I was called"; });
request(cl, 'GET', 'http://blah', 200);
return ml.messages;
},
'should call the format function': function(messages) {
assert.equal(messages[0].message, 'I was called');
}
2011-04-07 08:19:18 +08:00
}
2013-05-29 06:42:09 +08:00
}
2011-04-07 08:19:18 +08:00
}).export(module);