diff options
Diffstat (limited to 'packages/tests/src/plugins/plugin-websocket.ts')
-rw-r--r-- | packages/tests/src/plugins/plugin-websocket.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/packages/tests/src/plugins/plugin-websocket.ts b/packages/tests/src/plugins/plugin-websocket.ts new file mode 100644 index 000000000..832dcebd0 --- /dev/null +++ b/packages/tests/src/plugins/plugin-websocket.ts | |||
@@ -0,0 +1,76 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import WebSocket from 'ws' | ||
4 | import { | ||
5 | cleanupTests, | ||
6 | createSingleServer, | ||
7 | PeerTubeServer, | ||
8 | PluginsCommand, | ||
9 | setAccessTokensToServers | ||
10 | } from '@peertube/peertube-server-commands' | ||
11 | |||
12 | function buildWebSocket (server: PeerTubeServer, path: string) { | ||
13 | return new WebSocket('ws://' + server.host + path) | ||
14 | } | ||
15 | |||
16 | function expectErrorOrTimeout (server: PeerTubeServer, path: string, expectedTimeout: number) { | ||
17 | return new Promise<void>((res, rej) => { | ||
18 | const ws = buildWebSocket(server, path) | ||
19 | ws.on('error', () => res()) | ||
20 | |||
21 | const timeout = setTimeout(() => res(), expectedTimeout) | ||
22 | |||
23 | ws.on('open', () => { | ||
24 | clearTimeout(timeout) | ||
25 | |||
26 | return rej(new Error('Connect did not timeout')) | ||
27 | }) | ||
28 | }) | ||
29 | } | ||
30 | |||
31 | describe('Test plugin websocket', function () { | ||
32 | let server: PeerTubeServer | ||
33 | const basePaths = [ | ||
34 | '/plugins/test-websocket/ws/', | ||
35 | '/plugins/test-websocket/0.0.1/ws/' | ||
36 | ] | ||
37 | |||
38 | before(async function () { | ||
39 | this.timeout(30000) | ||
40 | |||
41 | server = await createSingleServer(1) | ||
42 | await setAccessTokensToServers([ server ]) | ||
43 | |||
44 | await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-websocket') }) | ||
45 | }) | ||
46 | |||
47 | it('Should not connect to the websocket without the appropriate path', async function () { | ||
48 | const paths = [ | ||
49 | '/plugins/unknown/ws/', | ||
50 | '/plugins/unknown/0.0.1/ws/' | ||
51 | ] | ||
52 | |||
53 | for (const path of paths) { | ||
54 | await expectErrorOrTimeout(server, path, 1000) | ||
55 | } | ||
56 | }) | ||
57 | |||
58 | it('Should not connect to the websocket without the appropriate sub path', async function () { | ||
59 | for (const path of basePaths) { | ||
60 | await expectErrorOrTimeout(server, path + '/unknown', 1000) | ||
61 | } | ||
62 | }) | ||
63 | |||
64 | it('Should connect to the websocket and receive pong', function (done) { | ||
65 | const ws = buildWebSocket(server, basePaths[0]) | ||
66 | |||
67 | ws.on('open', () => ws.send('ping')) | ||
68 | ws.on('message', data => { | ||
69 | if (data.toString() === 'pong') return done() | ||
70 | }) | ||
71 | }) | ||
72 | |||
73 | after(async function () { | ||
74 | await cleanupTests([ server ]) | ||
75 | }) | ||
76 | }) | ||