forked from webpack/webpack-dev-server
/
WebsocketServer.test.js
117 lines (98 loc) · 2.97 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
'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 server;
let socketServer;
let listeningApp;
beforeEach((done) => {
// eslint-disable-next-line new-cap
const app = new express();
listeningApp = http.createServer(app);
listeningApp.listen(port, 'localhost', () => {
server = {
log: {
error: () => {},
debug: () => {},
},
sockPath: '/ws-server',
listeningApp,
heartbeatInterval: 800,
};
socketServer = new WebsocketServer(server);
done();
});
});
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');
};
// the heartbeat interval was shortened greatly above
// so that the client is quickly pinged
client.on('ping', () => {
data.push('ping');
});
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 terminate a client that is not alive', (done) => {
let receivedClientClose = false;
socketServer.onConnection((connection) => {
// this makes the server think the client did not respond
// to a ping in time, so the server will terminate it
connection.isAlive = false;
socketServer.onConnectionClose(connection, () => {
receivedClientClose = true;
});
});
// eslint-disable-next-line new-cap, no-unused-vars
const client = new ws(`http://localhost:${port}/ws-server`);
setTimeout(() => {
expect(receivedClientClose).toBeTruthy();
done();
}, 3000);
});
afterEach((done) => {
listeningApp.close(done);
});
});