forked from martynsmith/node-irc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
69 lines (56 loc) · 1.74 KB
/
helpers.js
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
/* Mock irc server */
var path = require('path');
var fs = require('fs');
var net = require('net');
var tls = require('tls');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var MockIrcd = function(port, encoding, isSecure) {
var self = this;
var connectionClass;
var options = {};
if (isSecure) {
connectionClass = tls;
options = {
key: fs.readFileSync(path.resolve(__dirname, 'data/ircd.key')),
cert: fs.readFileSync(path.resolve(__dirname, 'data/ircd.pem'))
};
} else {
connectionClass = net;
}
this.port = port || (isSecure ? 6697 : 6667);
this.encoding = encoding || 'utf-8';
this.incoming = [];
this.outgoing = [];
this.server = connectionClass.createServer(options, function(c) {
c.on('data', function(data) {
var msg = data.toString(self.encoding).split('\r\n').filter(function(m) { return m; });
self.incoming = self.incoming.concat(msg);
});
self.on('send', function(data) {
self.outgoing.push(data);
c.write(data);
});
c.on('end', function() {
self.emit('end');
});
});
this.server.listen(this.port);
};
util.inherits(MockIrcd, EventEmitter);
MockIrcd.prototype.send = function(data) {
this.emit('send', data);
};
MockIrcd.prototype.close = function() {
this.server.close();
};
MockIrcd.prototype.getIncomingMsgs = function() {
return this.incoming;
};
var fixtures = require('./data/fixtures');
module.exports.getFixtures = function(testSuite) {
return fixtures[testSuite];
};
module.exports.MockIrcd = function(port, encoding, isSecure) {
return new MockIrcd(port, encoding, isSecure);
};