* MockPool provides an API that extends the Pool to influence the mockDispatches.
| 21 | * MockPool provides an API that extends the Pool to influence the mockDispatches. |
| 22 | */ |
| 23 | class MockPool extends Pool { |
| 24 | constructor (origin, opts) { |
| 25 | if (!opts || !opts.agent || typeof opts.agent.dispatch !== 'function') { |
| 26 | throw new InvalidArgumentError('Argument opts.agent must implement Agent') |
| 27 | } |
| 28 | |
| 29 | super(origin, opts) |
| 30 | |
| 31 | this[kMockAgent] = opts.agent |
| 32 | this[kOrigin] = origin |
| 33 | this[kIgnoreTrailingSlash] = opts.ignoreTrailingSlash ?? false |
| 34 | this[kDispatches] = [] |
| 35 | this[kConnected] = 1 |
| 36 | this[kOriginalDispatch] = this.dispatch |
| 37 | this[kOriginalClose] = this.close.bind(this) |
| 38 | |
| 39 | this.dispatch = buildMockDispatch.call(this) |
| 40 | this.close = this[kClose] |
| 41 | } |
| 42 | |
| 43 | get [Symbols.kConnected] () { |
| 44 | return this[kConnected] |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Sets up the base interceptor for mocking replies from undici. |
| 49 | */ |
| 50 | intercept (opts) { |
| 51 | return new MockInterceptor( |
| 52 | opts && { ignoreTrailingSlash: this[kIgnoreTrailingSlash], ...opts }, |
| 53 | this[kDispatches] |
| 54 | ) |
| 55 | } |
| 56 | |
| 57 | cleanMocks () { |
| 58 | this[kDispatches] = [] |
| 59 | } |
| 60 | |
| 61 | async [kClose] () { |
| 62 | await promisify(this[kOriginalClose])() |
| 63 | this[kConnected] = 0 |
| 64 | this[kMockAgent][Symbols.kClients].delete(this[kOrigin]) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | module.exports = MockPool |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…