|
3 | 3 | */ |
4 | 4 | import os from 'os' |
5 | 5 |
|
| 6 | +import followRedirects from 'follow-redirects' |
6 | 7 | import axios, { AxiosInstance } from 'axios' |
7 | | - |
8 | 8 | import pkg from '@packages/root' |
9 | | -import { httpAgent, httpsAgent } from '@packages/network/lib/agent' |
| 9 | +import agent from '@packages/network/lib/agent' |
10 | 10 |
|
11 | 11 | import app_config from '../../../config/app.json' |
12 | 12 | import { installErrorTransform } from './axios_middleware/transform_error' |
13 | 13 | import { installLogging } from './axios_middleware/logging' |
14 | 14 |
|
15 | 15 | // initialized with an export for testing purposes |
16 | | -export const _create = (): AxiosInstance => { |
| 16 | +export const _create = (options: { baseURL?: string } = {}): AxiosInstance => { |
17 | 17 | const cfgKey = process.env.CYPRESS_CONFIG_ENV || process.env.CYPRESS_INTERNAL_ENV || 'development' |
18 | 18 |
|
19 | 19 | const instance = axios.create({ |
20 | | - baseURL: app_config[cfgKey].api_url, |
21 | | - httpAgent, |
22 | | - httpsAgent, |
| 20 | + baseURL: options.baseURL ?? app_config[cfgKey].api_url, |
| 21 | + httpAgent: agent, |
| 22 | + httpsAgent: agent, |
23 | 23 | headers: { |
24 | 24 | 'x-os-name': os.platform(), |
25 | 25 | 'x-cypress-version': pkg.version, |
26 | 26 | 'User-Agent': `cypress/${pkg.version}`, |
27 | 27 | }, |
| 28 | + transport: { |
| 29 | + // https://github.com/axios/axios/issues/6313#issue-2198831362 |
| 30 | + // Tapping into the transport seems the only way to handle this at the moment: |
| 31 | + // https://github.com/axios/axios/blob/a406a93e2d99c3317596f02f3537f5457a2a80fd/lib/adapters/http.js#L438-L450 |
| 32 | + request (options, cb) { |
| 33 | + if ((process.env.HTTP_PROXY || process.env.HTTPS_PROXY) && options.headers['Proxy-Authorization']) { |
| 34 | + delete options.headers['Proxy-Authorization'] |
| 35 | + } |
| 36 | + |
| 37 | + if (/https:?/.test(options.protocol)) { |
| 38 | + return followRedirects.https.request(options, cb) |
| 39 | + } |
| 40 | + |
| 41 | + return followRedirects.http.request(options, cb) |
| 42 | + }, |
| 43 | + }, |
28 | 44 | }) |
29 | 45 |
|
30 | 46 | installLogging(instance) |
|
0 commit comments