aboutsummaryrefslogblamecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts
blob: f8a271cba1a0ed7d6211e21e3dc41b772fd5585a (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                    
                             
                                                                




                             
                      




                                 
                        
 

                         
 

                                                            
      



                                         




                                     

                
                                       
   
 
import express, { Request, Response } from 'express'
import { Server } from 'http'
import { getPort, randomListen, terminateServer } from './utils'

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)
  }
}