-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathws-test.mjs
More file actions
118 lines (114 loc) · 3.2 KB
/
Copy pathws-test.mjs
File metadata and controls
118 lines (114 loc) · 3.2 KB
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
'use strict';
import { assert } from 'chai';
import axios from 'axios';
import { pack, unpack } from 'msgpackr';
import { EventSource } from 'eventsource';
import { WebSocket } from 'ws';
import { setupTestApp } from './setupTestApp.mjs';
describe('test WebSockets connections and messaging', () => {
let ws1, ws2;
before(async function () {
this.timeout(5000);
await setupTestApp();
ws1 = new WebSocket('ws://localhost:9926/Echo');
await new Promise((resolve, reject) => {
ws1.on('open', resolve);
ws1.on('error', reject);
});
});
after(function () {
ws1.close();
if (ws2) ws2.close();
});
it('ping echo server', async function () {
ws1.send(
JSON.stringify({
action: 'ping',
})
);
let message = await new Promise((resolve) => {
ws1.once('message', (msg) => resolve(JSON.parse(msg)));
});
assert.equal(message.action, 'ping');
ws1.send(
JSON.stringify({
action: 'another ping',
})
);
message = await new Promise((resolve) => {
ws1.once('message', (msg) => resolve(JSON.parse(msg)));
});
assert.equal(message.action, 'another ping');
});
it('ping echo server with content type', async function () {
let ws = new WebSocket('ws://localhost:9926/Echo.msgpack');
await new Promise((resolve, reject) => {
ws.on('open', resolve);
ws.on('error', reject);
});
let encoded = pack({
action: 'ping',
});
ws.send(encoded);
let message = await new Promise((resolve) => {
ws.once('message', (msg) => resolve(unpack(msg)));
});
ws.close();
assert.equal(message.action, 'ping');
});
it('ping echo EventSource', async function () {
let event_source = new EventSource('http://localhost:9926/Echo');
let greetings_message;
let message = await new Promise((resolve) => {
event_source.addEventListener('open', () => {
console.log('open');
});
event_source.addEventListener('message', (event) => {
greetings_message = event.data;
});
event_source.addEventListener('another-message', (event) => {
resolve(event);
});
});
event_source.close();
assert.equal(greetings_message, 'greetings');
assert.equal(message.data, 'hello again');
});
it('default subscribe on WS', async function () {
this.timeout(5000);
ws2 = new WebSocket('ws://localhost:9926/SimpleRecord/5');
await new Promise((resolve, reject) => {
ws2.on('open', resolve);
ws2.on('error', reject);
});
let messages = [];
await new Promise(async (resolve, reject) => {
ws2.on('message', (message) => {
messages.push(JSON.parse(message));
if (messages.length === 2) {
resolve();
}
});
try {
let response = await axios.put('http://localhost:9926/SimpleRecord/5', {
id: '5',
name: 'new name',
});
assert.equal(response.status, 204);
response = await axios.patch('http://localhost:9926/SimpleRecord/5', {
id: '5',
newProperty: 'test',
});
assert.equal(response.status, 204);
} catch (error) {
console.error(error);
reject(error);
}
});
assert.equal(messages[0].value.name, 'new name');
assert.equal(messages[0].type, 'put');
assert.equal(messages[1].value.name, 'new name');
assert.equal(messages[1].value.newProperty, 'test');
assert.equal(messages[1].type, 'put');
});
});