aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/mock-servers/mock-proxy.ts
blob: 5365f87d12b3d9b3cd8916b48d4ca62ab9bd0bf6 (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
import { createServer, Server } from 'http'
import * as proxy from 'proxy'
import { randomInt } from '@shared/core-utils'

class MockProxy {
  private server: Server

  initialize () {
    return new Promise<number>(res => {
      const port = 42501 + randomInt(1, 100)

      this.server = proxy(createServer())
      this.server.listen(port, () => res(port))
    })
  }

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

// ---------------------------------------------------------------------------

export {
  MockProxy
}