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