-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathformat-test.js
More file actions
183 lines (167 loc) · 5.31 KB
/
Copy pathformat-test.js
File metadata and controls
183 lines (167 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const vows = require('vows');
const assert = require('assert');
const Syslog = require('../lib/winston-syslog.js').Syslog;
const dgram = require('dgram');
const parser = require('@myndzi/glossy').Parse;
const PORT = 11229;
let server;
let transport;
const { MESSAGE, LEVEL } = require('triple-beam');
vows
.describe('syslog messages')
.addBatch({
'opening fake syslog server': {
'topic': function () {
const self = this;
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});
server.bind(PORT);
},
'default format': {
'topic': function () {
const self = this;
server.once('message', function (msg) {
parser.parse(msg, function (d) {
self.callback(null, d);
});
});
transport = new Syslog({
port: PORT
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping' }, function (err) {
assert.ifError(err);
});
},
'should have host field set to localhost': function (msg) {
assert.equal(msg.host, 'localhost');
transport.close();
},
'setting locahost option to a different falsy value (null)': {
'topic': function () {
const self = this;
server.once('message', function (msg) {
parser.parse(msg, function (d) {
self.callback(null, d);
});
});
transport = new Syslog({
port: PORT,
localhost: null
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping2' }, function (
err
) {
assert.ifError(err);
});
},
'should have host different from localhost': function (msg) {
assert.notEqual(msg.host, 'localhost');
transport.close();
},
'setting appName option to hello and type to 5424': {
'topic': function () {
const self = this;
server.once('message', function (msg) {
parser.parse(msg, function (d) {
self.callback(null, d);
});
});
transport = new Syslog({
port: PORT,
type: '5424',
appName: 'hello'
});
transport.log(
{ [LEVEL]: 'debug', [MESSAGE]: 'app name test' },
function (err) {
assert.ifError(err);
}
);
},
'should have appName field set to hello': function (msg) {
assert.equal(msg.appName, 'hello');
},
'should have proper time zone': function (msg) {
const now = new Date();
assert.equal((Math.abs((now - msg.time) / 1000) < 300), true);
},
'should have proper milliseconds format': function (msg) {
const milliseconds = msg.originalMessage.split(' ')[1].slice(20, 23);
assert.equal(isNaN(milliseconds), false);
transport.close();
},
'setting app_name option to hello': {
'topic': function () {
const self = this;
server.once('message', function (msg) {
parser.parse(msg, function (d) {
self.callback(null, d);
});
});
transport = new Syslog({
port: PORT,
type: '5424',
app_name: 'hello'
});
transport.log(
{ [LEVEL]: 'debug', [MESSAGE]: 'app name test' },
function (err) {
assert.ifError(err);
}
);
},
'should have appName field set to hello': function (msg) {
assert.equal(msg.appName, 'hello');
transport.close();
}
}
}
}
},
'teardown': function () {
server.close();
}
}
})
.addBatch({
'opening fake syslog server': {
'topic': function () {
var self = this;
server = dgram.createSocket('udp4');
server.on('listening', function () {
self.callback();
});
server.bind(PORT);
},
'Custom producer': {
'topic': function () {
var self = this;
server.once('message', function (msg) {
self.callback(null, msg.toString());
});
function CustomProducer() {}
CustomProducer.prototype.produce = function (opts) {
return 'test ' + opts.severity + ': ' + opts.message;
};
transport = new Syslog({
port: PORT,
customProducer: CustomProducer
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: 'ping' }, function (err) {
assert.ifError(err);
});
},
'should apply custom syslog format': function (msg) {
assert.equal(msg, 'test debug: ping');
transport.close();
}
},
'teardown': function () {
server.close();
}
}
})
.export(module);