-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ParseWebSocketServer.spec.js
81 lines (73 loc) · 2.24 KB
/
ParseWebSocketServer.spec.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
const { ParseWebSocketServer } = require('../lib/LiveQuery/ParseWebSocketServer');
const EventEmitter = require('events');
describe('ParseWebSocketServer', function () {
beforeEach(function (done) {
// Mock ws server
const mockServer = function () {
return new EventEmitter();
};
jasmine.mockLibrary('ws', 'Server', mockServer);
done();
});
it('can handle connect event when ws is open', function (done) {
const onConnectCallback = jasmine.createSpy('onConnectCallback');
const http = require('http');
const server = http.createServer();
const parseWebSocketServer = new ParseWebSocketServer(server, onConnectCallback, {
websocketTimeout: 5,
}).server;
const ws = new EventEmitter();
ws.readyState = 0;
ws.OPEN = 0;
ws.ping = jasmine.createSpy('ping');
parseWebSocketServer.onConnection(ws);
// Make sure callback is called
expect(onConnectCallback).toHaveBeenCalled();
// Make sure we ping to the client
setTimeout(function () {
expect(ws.ping).toHaveBeenCalled();
server.close();
done();
}, 10);
});
it('can handle error event', async () => {
jasmine.restoreLibrary('ws', 'Server');
const WebSocketServer = require('ws').Server;
let wssError;
class WSSAdapter {
constructor(options) {
this.options = options;
}
onListen() {}
onConnection() {}
onError() {}
start() {
const wss = new WebSocketServer({ server: this.options.server });
wss.on('listening', this.onListen);
wss.on('connection', this.onConnection);
wss.on('error', error => {
wssError = error;
this.onError(error);
});
this.wss = wss;
}
}
const server = await reconfigureServer({
liveQuery: {
classNames: ['TestObject'],
},
liveQueryServerOptions: {
wssAdapter: WSSAdapter,
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});
const wssAdapter = server.liveQueryServer.parseWebSocketServer.server;
wssAdapter.wss.emit('error', 'Invalid Packet');
expect(wssError).toBe('Invalid Packet');
});
afterEach(function () {
jasmine.restoreLibrary('ws', 'Server');
});
});