aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/shared/mock-servers/mock-plugin-blocklist.ts
blob: 5d6e018164eef8fc8803bba9f1d649b644a5104c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import express, { Request, Response } from 'express'
import { Server } from 'http'
import { getPort, randomListen, terminateServer } from './shared'

type BlocklistResponse = {
  data: {
    value: string
    action?: 'add' | 'remove'
    updatedAt?: string
  }[]
}

export class MockBlocklist {
  private body: BlocklistResponse
  private server: Server

  async initialize () {
    const app = express()

    app.get('/blocklist', (req: Request, res: Response) => {
      return res.json(this.body)
    })

    this.server = await randomListen(app)

    return getPort(this.server)
  }

  replace (body: BlocklistResponse) {
    this.body = body
  }

  terminate () {
    return terminateServer(this.server)
  }
}