aboutsummaryrefslogblamecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-429.ts
blob: 9e0d1281aa4e0689dd4898065c5311397a8ae7c4 (plain) (tree)
































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

export class Mock429 {
  private server: Server
  private responseSent = false

  async initialize () {
    const app = express()

    app.get('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {

      if (!this.responseSent) {
        this.responseSent = true

        // Retry after 5 seconds
        res.header('retry-after', '2')
        return res.sendStatus(429)
      }

      return res.sendStatus(200)
    })

    this.server = await randomListen(app)

    return getPort(this.server)
  }

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