aboutsummaryrefslogblamecommitdiffhomepage
path: root/shared/extra-utils/plugins/mock-blocklist.ts
blob: 50e2289f1eb9090b69f93e7d5a114ef097e7d322 (plain) (tree)
1
2
3
4
5
6
7
8
                                  
                             




                             
                      




                                 
                        

                 
                                     





                                                                              
                                                  





                                     



                                        
 
import * as express from 'express'
import { Server } from 'http'

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

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

  initialize () {
    return new Promise<void>(res => {
      const app = express()

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

      this.server = app.listen(42100, () => res())
    })
  }

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

  terminate () {
    if (this.server) this.server.close()
  }
}