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

227 lines
6.4 KiB
JavaScript
Raw 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')
, 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
}
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() {
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) {
};
}
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;
},
'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');
}
2013-06-18 06:47:18 +08:00
},
'format that includes request headers': {
topic: function(clm) {
var ml = new MockLogger();
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, ':req[Content-Type]');
request(
cl,
'GET', 'http://blah', 200,
{ 'Content-Type': 'application/json' }
);
return ml.messages;
},
'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();
ml.level = levels.INFO;
var cl = clm.connectLogger(ml, ':res[Content-Type]');
request(
cl,
'GET', 'http://blah', 200,
null,
{ 'Content-Type': 'application/cheese' }
);
return ml.messages;
},
'should output the response header': function(messages) {
assert.equal(messages[0].message, 'application/cheese');
}
}
2013-05-29 06:42:09 +08:00
}
2011-04-07 08:19:18 +08:00
}).export(module);