@serenity-js/rest
Version:
Serenity/JS Screenplay Pattern library for interacting with REST and other HTTP-based services, supporting comprehensive API testing and blended testing scenarios
66 lines • 2.4 kB
JavaScript
import * as process from 'node:process';
import { ensure, isDefined } from 'tiny-types';
import { createUrl } from './createUrl.js';
import { EnvironmentVariables } from './EnvironmentVariables.js';
import { ProxyAgent } from './ProxyAgent.js';
import { ProxyBypass } from './ProxyBypass.js';
/**
* @param options
*/
export function axiosProxyOverridesFor(options) {
const agent = new ProxyAgent({
httpAgent: options.httpAgent,
httpsAgent: options.httpsAgent,
// if there's a specific proxy override configured, use it
// if not - detect proxy automatically based on the env variables
getProxyForUrl: options.proxy
? createGetProxyForUrlFromConfig(options.proxy)
: createGetProxyForUrlFromEnvironmentVariables(new EnvironmentVariables(process.env)),
});
return {
proxy: false,
httpAgent: agent,
httpsAgent: agent,
};
}
export function createGetProxyForUrlFromConfig(proxyOptions) {
const proxyBypass = ProxyBypass.create(proxyOptions?.bypass);
return createGetProxyForUrl(proxyBypass, (url) => {
return createUrl({
username: proxyOptions.auth?.username,
password: proxyOptions.auth?.password,
protocol: proxyOptions.protocol,
hostname: ensure('proxy.host', proxyOptions?.host, isDefined()),
port: proxyOptions.port ? Number(proxyOptions.port) : undefined,
}).toString();
});
}
export function createGetProxyForUrlFromEnvironmentVariables(env) {
const proxyBypass = ProxyBypass.create(env.findFirst('npm_config_no_proxy', 'no_proxy'));
return createGetProxyForUrl(proxyBypass, (url) => {
const protocolName = url.protocol.replace(/:$/, '');
const proxyUrl = env.findFirst(`npm_config_${protocolName}_proxy`, `${protocolName}_proxy`, 'npm_config_proxy', 'all_proxy');
return proxyUrl || undefined;
});
}
function createGetProxyForUrl(bypass, getProxy) {
return function getProxyForUrl(urlValue) {
if (!isValidUrl(urlValue)) {
return undefined;
}
const url = new URL(urlValue);
return bypass.matches(url)
? undefined
: getProxy(url);
};
}
function isValidUrl(url) {
try {
new URL(url);
return true;
}
catch {
return false;
}
}
//# sourceMappingURL=proxy.js.map