]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/mock-servers/mock-plugin-blocklist.ts
Wait mock server termination
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / mock-servers / mock-plugin-blocklist.ts
1 import express, { Request, Response } from 'express'
2 import { Server } from 'http'
3 import { randomInt } from '@shared/core-utils'
4 import { terminateServer } from './utils'
5
6 type BlocklistResponse = {
7 data: {
8 value: string
9 action?: 'add' | 'remove'
10 updatedAt?: string
11 }[]
12 }
13
14 export class MockBlocklist {
15 private body: BlocklistResponse
16 private server: Server
17
18 initialize () {
19 return new Promise<number>(res => {
20 const app = express()
21
22 app.get('/blocklist', (req: Request, res: Response) => {
23 return res.json(this.body)
24 })
25
26 const port = 42201 + randomInt(1, 100)
27 this.server = app.listen(port, () => res(port))
28 })
29 }
30
31 replace (body: BlocklistResponse) {
32 this.body = body
33 }
34
35 terminate () {
36 return terminateServer(this.server)
37 }
38 }