2013-05-26 15:15:10 +08:00
|
|
|
"use strict";
|
2011-10-29 10:57:28 +08:00
|
|
|
var vows = require('vows')
|
|
|
|
, assert = require('assert')
|
|
|
|
, levels = require('../lib/levels');
|
|
|
|
|
|
|
|
function MockLogger() {
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
this.messages = [];
|
|
|
|
|
|
|
|
this.log = function(level, message, exception) {
|
2013-05-26 15:15:10 +08:00
|
|
|
that.messages.push({ level: level, message: message });
|
2011-10-29 10:57:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.isLevelEnabled = function(level) {
|
|
|
|
return level.isGreaterThanOrEqualTo(that.level);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.level = levels.TRACE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function MockRequest(remoteAddr, method, originalUrl) {
|
2013-05-26 15:15:10 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
this.socket = { remoteAddress: remoteAddr };
|
|
|
|
this.originalUrl = originalUrl;
|
|
|
|
this.method = method;
|
|
|
|
this.httpVersionMajor = '5';
|
|
|
|
this.httpVersionMinor = '0';
|
|
|
|
this.headers = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function MockResponse(statusCode) {
|
|
|
|
|
|
|
|
this.statusCode = statusCode;
|
|
|
|
|
|
|
|
this.end = function(chunk, encoding) {
|
2013-05-26 15:15:10 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
vows.describe('log4js connect logger').addBatch({
|
|
|
|
'getConnectLoggerModule': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function() {
|
|
|
|
var clm = require('../lib/connect-logger');
|
|
|
|
return clm;
|
|
|
|
},
|
2011-10-29 10:57:28 +08:00
|
|
|
|
2013-05-26 15:15:10 +08:00
|
|
|
'should return a "connect logger" factory' : function(clm) {
|
|
|
|
assert.isObject(clm);
|
|
|
|
},
|
2011-10-29 10:57:28 +08:00
|
|
|
|
2013-05-26 15:15:10 +08:00
|
|
|
'nolog String' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
|
|
|
var cl = clm.connectLogger(ml, { nolog: "\\.gif" });
|
|
|
|
return {cl: cl, ml: ml};
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2013-05-26 15:15:10 +08:00
|
|
|
'check unmatch url request': {
|
|
|
|
topic: function(d){
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.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');
|
|
|
|
messages.pop();
|
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2013-05-26 15:15:10 +08:00
|
|
|
'check match url request': {
|
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
}
|
2011-10-29 10:57:28 +08:00
|
|
|
},
|
|
|
|
|
2013-05-26 15:15:10 +08:00
|
|
|
'nolog Strings' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
|
|
|
var cl = clm.connectLogger(ml, {nolog: "\\.gif|\\.jpe?g"});
|
|
|
|
return {cl: cl, ml: ml};
|
|
|
|
},
|
|
|
|
|
|
|
|
'check unmatch url request (png)': {
|
|
|
|
topic: function(d){
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.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');
|
|
|
|
messages.pop();
|
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
'check match url request (gif)': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +08:00
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
},
|
2011-10-29 10:57:28 +08:00
|
|
|
'check match url request (jpeg)': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.jpeg'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +08:00
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
}
|
2011-10-29 10:57:28 +08:00
|
|
|
},
|
2013-05-26 15:15:10 +08:00
|
|
|
'nolog Array<String>' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
|
|
|
var cl = clm.connectLogger(ml, {nolog: ["\\.gif", "\\.jpe?g"]});
|
2011-10-29 10:57:28 +08:00
|
|
|
return {cl: cl, ml: ml};
|
|
|
|
},
|
2013-05-26 15:15:10 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
'check unmatch url request (png)': {
|
|
|
|
topic: function(d){
|
2013-05-26 15:15:10 +08:00
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +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');
|
2011-10-29 10:57:28 +08:00
|
|
|
messages.pop();
|
2013-05-26 15:15:10 +08:00
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
'check match url request (gif)': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +08:00
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
'check match url request (jpeg)': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.jpeg'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +08:00
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
},
|
2011-10-29 10:57:28 +08:00
|
|
|
},
|
2013-05-26 15:15:10 +08:00
|
|
|
'nolog RegExp' : {
|
|
|
|
topic: function(clm) {
|
|
|
|
var ml = new MockLogger();
|
|
|
|
var cl = clm.connectLogger(ml, {nolog: /\.gif|\.jpe?g/});
|
2011-10-29 10:57:28 +08:00
|
|
|
return {cl: cl, ml: ml};
|
|
|
|
},
|
|
|
|
|
|
|
|
'check unmatch url request (png)': {
|
|
|
|
topic: function(d){
|
2013-05-26 15:15:10 +08:00
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.png'); // not gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +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');
|
2011-10-29 10:57:28 +08:00
|
|
|
messages.pop();
|
2013-05-26 15:15:10 +08:00
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
'check match url request (gif)': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.gif'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +08:00
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:40:27 +08:00
|
|
|
|
2011-10-29 10:57:28 +08:00
|
|
|
'check match url request (jpeg)': {
|
2013-05-26 15:15:10 +08:00
|
|
|
topic: function(d) {
|
|
|
|
var req = new MockRequest('my.remote.addr', 'GET', 'http://url/hoge.jpeg'); // gif
|
|
|
|
var res = new MockResponse(200);
|
|
|
|
d.cl(req, res, function() { });
|
2011-10-29 10:57:28 +08:00
|
|
|
res.end('chunk', 'encoding');
|
|
|
|
return d.ml.messages;
|
2013-05-26 15:15:10 +08:00
|
|
|
},
|
|
|
|
'check message': function(messages) {
|
|
|
|
assert.isArray(messages);
|
|
|
|
assert.equal(messages.length, 0);
|
|
|
|
}
|
|
|
|
}
|
2011-10-29 10:57:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}).export(module);
|