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

38 lines
769 B
TypeScript
Raw Normal View History

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