1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import WebSocket from 'ws'
4 import { cleanupTests, createSingleServer, PeerTubeServer, PluginsCommand, setAccessTokensToServers } from '@shared/server-commands'
6 function buildWebSocket (server: PeerTubeServer, path: string) {
7 return new WebSocket('ws://' + server.host + path)
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())
15 const timeout = setTimeout(() => res(), expectedTimeout)
20 return rej(new Error('Connect did not timeout'))
25 describe('Test plugin websocket', function () {
26 let server: PeerTubeServer
28 '/plugins/test-websocket/ws/',
29 '/plugins/test-websocket/0.0.1/ws/'
32 before(async function () {
35 server = await createSingleServer(1)
36 await setAccessTokensToServers([ server ])
38 await server.plugins.install({ path: PluginsCommand.getPluginTestPath('-websocket') })
41 it('Should not connect to the websocket without the appropriate path', async function () {
43 '/plugins/unknown/ws/',
44 '/plugins/unknown/0.0.1/ws/'
47 for (const path of paths) {
48 await expectErrorOrTimeout(server, path, 1000)
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)
58 it('Should connect to the websocket and receive pong', function (done) {
59 const ws = buildWebSocket(server, basePaths[0])
61 ws.on('open', () => ws.send('ping'))
62 ws.on('message', data => {
63 if (data.toString() === 'pong') return done()
67 after(async function () {
68 await cleanupTests([ server ])