-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
SockJSServer.test.js
103 lines (83 loc) · 2.54 KB
/
SockJSServer.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
'use strict';
const http = require('http');
const express = require('express');
const SockJS = require('sockjs-client/dist/sockjs');
const SockJSServer = require('../../../lib/servers/SockJSServer');
const port = require('../../ports-map').SockJSServer;
describe('SockJSServer', () => {
let socketServer;
let listeningApp;
beforeEach((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: '/sockjs-node',
listeningApp,
};
socketServer = new SockJSServer(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);
});
const client = new SockJS(`http://localhost:${port}/sockjs-node`);
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 SockJS(`http://localhost:${port}/sockjs-node`);
setTimeout(() => {
// the client closes itself, the server does not close it
client.close();
}, 1000);
setTimeout(() => {
expect(receivedClientClose).toBeTruthy();
done();
}, 3000);
});
it('should not throw an exception when connection is null', () => {
const cb = jest.fn();
socketServer.onConnection(cb);
expect(() => {
socketServer.socket.emit('connection', null);
}).not.toThrow();
expect(cb.mock.calls[0]).toEqual([null, null]);
});
});
afterEach((done) => {
listeningApp.close(done);
});
});