(plan = 1, opts = {})
| 209 | } |
| 210 | |
| 211 | const createEnvHttpProxyAgentWithMocks = (plan = 1, opts = {}) => { |
| 212 | const factory = (origin) => { |
| 213 | const mockAgent = new MockAgent() |
| 214 | const mockPool = mockAgent.get(origin) |
| 215 | let i = 0 |
| 216 | while (i < plan) { |
| 217 | mockPool.intercept({ path: /.*/ }).reply(200, 'OK') |
| 218 | i++ |
| 219 | } |
| 220 | return mockPool |
| 221 | } |
| 222 | process.env.http_proxy = 'http://localhost:8080' |
| 223 | process.env.https_proxy = 'http://localhost:8443' |
| 224 | const dispatcher = new EnvHttpProxyAgent({ proxyTunnel: true, ...opts, factory }) |
| 225 | const agentSymbols = [kNoProxyAgent, kHttpProxyAgent, kHttpsProxyAgent] |
| 226 | agentSymbols.forEach((agentSymbol) => { |
| 227 | const originalDispatch = dispatcher[agentSymbol].dispatch |
| 228 | dispatcher[agentSymbol].dispatch = function () { |
| 229 | dispatcher[agentSymbol].dispatch.called = true |
| 230 | return originalDispatch.apply(this, arguments) |
| 231 | } |
| 232 | dispatcher[agentSymbol].dispatch.called = false |
| 233 | }) |
| 234 | const usesProxyAgent = async (agent, url) => { |
| 235 | await fetch(url, { dispatcher }) |
| 236 | const result = agentSymbols.every((agentSymbol) => agent === agentSymbol |
| 237 | ? dispatcher[agentSymbol].dispatch.called === true |
| 238 | : dispatcher[agentSymbol].dispatch.called === false) |
| 239 | |
| 240 | agentSymbols.forEach((agentSymbol) => { |
| 241 | dispatcher[agentSymbol].dispatch.called = false |
| 242 | }) |
| 243 | return result |
| 244 | } |
| 245 | const doesNotProxy = usesProxyAgent.bind(this, kNoProxyAgent) |
| 246 | return { |
| 247 | dispatcher, |
| 248 | doesNotProxy, |
| 249 | usesProxyAgent |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | test('uses the appropriate proxy for the protocol', async (t) => { |
| 254 | t = tspl(t, { plan: 2 }) |
no test coverage detected