PeerTube/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts

39 lines
812 B
TypeScript
Raw Normal View History

2021-08-27 12:32:44 +00:00
import express, { Request, Response } from 'express'
import { Server } from 'http'
import { randomInt } from '@shared/core-utils'
2021-09-06 06:13:11 +00:00
import { terminateServer } from './utils'
2020-05-07 14:32:54 +00:00
type BlocklistResponse = {
data: {
value: string
action?: 'add' | 'remove'
2020-05-07 15:08:16 +00:00
updatedAt?: string
2020-05-07 14:32:54 +00:00
}[]
}
export class MockBlocklist {
private body: BlocklistResponse
private server: Server
2020-05-07 14:32:54 +00:00
initialize () {
return new Promise<number>(res => {
2020-05-07 14:32:54 +00:00
const app = express()
2021-08-27 12:32:44 +00:00
app.get('/blocklist', (req: Request, res: Response) => {
2020-05-07 14:32:54 +00:00
return res.json(this.body)
})
const port = 42201 + randomInt(1, 100)
this.server = app.listen(port, () => res(port))
2020-05-07 14:32:54 +00:00
})
}
replace (body: BlocklistResponse) {
this.body = body
}
terminate () {
2021-09-06 06:13:11 +00:00
return terminateServer(this.server)
}
2020-05-07 14:32:54 +00:00
}