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