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