forked from tmijs/tmi.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.js
64 lines (55 loc) · 1.4 KB
/
authentication.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
var WebSocketServer = require('ws').Server;
var tmi = require('../index.js');
var noop = function() {};
var catchConnectError = err => {
if(!['Connection closed.', 'Login unsuccessful.', 'Error logging in.', 'Invalid NICK.'].includes(err)) {
console.error(err);
}
};
var tests = [
':tmi.twitch.tv NOTICE #schmoopiie :Login unsuccessful.',
':tmi.twitch.tv NOTICE #schmoopiie :Error logging in.',
':tmi.twitch.tv NOTICE #schmoopiie :Invalid NICK.'
];
describe('handling authentication', function() {
beforeEach(function() {
// Initialize websocket server
this.server = new WebSocketServer({port: 7000});
this.client = new tmi.client({
logger: {
error: noop,
info: noop
},
connection: {
server: 'localhost',
port: 7000,
timeout: 1
}
});
});
afterEach(function() {
// Shut down websocket server
this.server.close();
this.client = null;
});
tests.forEach(function(test) {
it(`handle ${test}`, function(cb) {
var client = this.client;
var server = this.server;
var parts = test.split(':');
var message = parts[parts.length - 1].trim();
server.on('connection', function(ws) {
ws.on('message', function(message) {
if(!message.indexOf('NICK')) {
ws.send(test);
}
});
});
client.on('disconnected', function(reason) {
reason.should.eql(message);
cb();
});
client.connect().catch(catchConnectError);
});
});
});