/
WebsocketServer.test.js
147 lines (120 loc) · 3.62 KB
/
WebsocketServer.test.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
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
'use strict';
const http = require('http');
const express = require('express');
const ws = require('ws');
const WebsocketServer = require('../../../lib/servers/WebsocketServer');
const port = require('../../ports-map').WebsocketServer;
describe('WebsocketServer', () => {
let socketServer;
let listeningApp;
beforeAll((done) => {
// eslint-disable-next-line new-cap
const app = new express();
listeningApp = http.createServer(app);
listeningApp.listen(port, 'localhost', () => {
const server = {
log: {
error: () => {},
debug: () => {},
},
sockPath: '/ws-server',
listeningApp,
};
socketServer = new WebsocketServer(server);
done();
});
});
describe('server', () => {
it('should recieve connection, send message, and close client', (done) => {
const data = [];
let headers;
socketServer.onConnection((connection, h) => {
headers = h;
data.push('open');
socketServer.send(connection, 'hello world');
setTimeout(() => {
// the server closes the connection with the client
socketServer.close(connection);
}, 1000);
});
// eslint-disable-next-line new-cap
const client = new ws(`http://localhost:${port}/ws-server`);
client.onmessage = (e) => {
data.push(e.data);
};
client.onclose = () => {
data.push('close');
};
setTimeout(() => {
expect(headers.host).toMatchSnapshot();
expect(data).toMatchSnapshot();
done();
}, 3000);
});
it('should receive client close event', (done) => {
let receivedClientClose = false;
socketServer.onConnection((connection) => {
socketServer.onConnectionClose(connection, () => {
receivedClientClose = true;
});
});
// eslint-disable-next-line new-cap
const client = new ws(`http://localhost:${port}/ws-server`);
setTimeout(() => {
// the client closes itself, the server does not close it
client.close();
}, 1000);
setTimeout(() => {
expect(receivedClientClose).toBeTruthy();
done();
}, 3000);
});
it('should match sockPath', (done) => {
let receivedConnection = false;
socketServer.onConnection((connection) => {
receivedConnection = true;
connection.close(4000);
});
// eslint-disable-next-line new-cap
const client = new ws(`http://localhost:${port}/ws-server?YEP`);
let receivedError = false;
client.onerror = (e) => {
receivedError = e.error;
};
client.onclose = (e) => {
expect(e.code).toEqual(4000);
};
setTimeout(() => {
expect(receivedConnection).toBeTruthy();
expect(receivedError).toBeFalsy();
done();
}, 3000);
});
it('should ignore other paths', (done) => {
let receivedConnection = false;
socketServer.onConnection((connection) => {
receivedConnection = true;
connection.close(4000);
});
// eslint-disable-next-line new-cap
const client = new ws(`http://localhost:${port}/ws-server-NOT`, {
handshakeTimeout: 1000,
});
let receivedError = false;
client.onerror = (e) => {
receivedError = e.error;
};
client.onclose = (e) => {
expect(e.code).not.toEqual(4000);
};
setTimeout(() => {
expect(receivedConnection).toBeFalsy();
expect(receivedError).toBeTruthy();
done();
}, 3000);
});
});
afterAll((done) => {
listeningApp.close(done);
});
});