merge main into proxy branch

This commit is contained in:
Mirko Golze 2023-10-16 12:08:10 +02:00
parent 3fa87fe99a
commit 19463cd0cf
2 changed files with 44 additions and 25 deletions

View File

@ -48,7 +48,7 @@ const runSingleRequest = async function (
// run pre-request vars // run pre-request vars
const preRequestVars = get(bruJson, 'request.vars.req'); const preRequestVars = get(bruJson, 'request.vars.req');
if (preRequestVars && preRequestVars.length) { if (preRequestVars?.length) {
const varsRuntime = new VarsRuntime(); const varsRuntime = new VarsRuntime();
varsRuntime.runPreRequestVars( varsRuntime.runPreRequestVars(
preRequestVars, preRequestVars,
@ -65,7 +65,7 @@ const runSingleRequest = async function (
get(collectionRoot, 'request.script.req'), get(collectionRoot, 'request.script.req'),
get(bruJson, 'request.script.req') get(bruJson, 'request.script.req')
]).join(os.EOL); ]).join(os.EOL);
if (requestScriptFile && requestScriptFile.length) { if (requestScriptFile?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runRequestScript( await scriptRuntime.runRequestScript(
decomment(requestScriptFile), decomment(requestScriptFile),
@ -99,23 +99,45 @@ const runSingleRequest = async function (
} }
} }
// set proxy if enabled
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
const shouldProxy = shouldUseProxy(request.url, get(brunoConfig, 'proxy.noProxy', ''));
if (proxyEnabled && shouldProxy) {
let proxyUri;
const interpolationOptions = { const interpolationOptions = {
envVars: envVariables, envVars: envVariables,
collectionVariables, collectionVariables,
processEnvVars processEnvVars
}; };
// client certificate config
const clientCertConfig = get(brunoConfig, 'clientCertificates.certs', []);
for (let clientCert of clientCertConfig) {
const domain = interpolateString(clientCert.domain, interpolationOptions);
const certFilePath = interpolateString(clientCert.certFilePath, interpolationOptions);
const keyFilePath = interpolateString(clientCert.keyFilePath, interpolationOptions);
if (domain && certFilePath && keyFilePath) {
const hostRegex = '^https:\\/\\/' + domain.replaceAll('.', '\\.').replaceAll('*', '.*');
if (request.url.match(hostRegex)) {
try {
httpsAgentRequestFields['cert'] = fs.readFileSync(certFilePath);
httpsAgentRequestFields['key'] = fs.readFileSync(keyFilePath);
} catch (err) {
console.log('Error reading cert/key file', err);
}
httpsAgentRequestFields['passphrase'] = interpolateString(clientCert.passphrase, interpolationOptions);
break;
}
}
}
// set proxy if enabled
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
const shouldProxy = shouldUseProxy(request.url, get(brunoConfig, 'proxy.noProxy', ''));
if (proxyEnabled && shouldProxy) {
const proxyProtocol = interpolateString(get(brunoConfig, 'proxy.protocol'), interpolationOptions); const proxyProtocol = interpolateString(get(brunoConfig, 'proxy.protocol'), interpolationOptions);
const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions); const proxyHostname = interpolateString(get(brunoConfig, 'proxy.hostname'), interpolationOptions);
const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions); const proxyPort = interpolateString(get(brunoConfig, 'proxy.port'), interpolationOptions);
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false); const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const socksEnabled = proxyProtocol.includes('socks'); const socksEnabled = proxyProtocol.includes('socks');
let proxyUri;
if (proxyAuthEnabled) { if (proxyAuthEnabled) {
const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions); const proxyAuthUsername = interpolateString(get(brunoConfig, 'proxy.auth.username'), interpolationOptions);
const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions); const proxyAuthPassword = interpolateString(get(brunoConfig, 'proxy.auth.password'), interpolationOptions);
@ -159,7 +181,7 @@ const runSingleRequest = async function (
responseTime = response.headers.get('request-duration'); responseTime = response.headers.get('request-duration');
response.headers.delete('request-duration'); response.headers.delete('request-duration');
} catch (err) { } catch (err) {
if (err && err.response) { if (err?.response) {
response = err.response; response = err.response;
// Prevents the duration on leaking to the actual result // Prevents the duration on leaking to the actual result
@ -195,7 +217,7 @@ const runSingleRequest = async function (
// run post-response vars // run post-response vars
const postResponseVars = get(bruJson, 'request.vars.res'); const postResponseVars = get(bruJson, 'request.vars.res');
if (postResponseVars && postResponseVars.length) { if (postResponseVars?.length) {
const varsRuntime = new VarsRuntime(); const varsRuntime = new VarsRuntime();
varsRuntime.runPostResponseVars( varsRuntime.runPostResponseVars(
postResponseVars, postResponseVars,
@ -213,7 +235,7 @@ const runSingleRequest = async function (
get(collectionRoot, 'request.script.res'), get(collectionRoot, 'request.script.res'),
get(bruJson, 'request.script.res') get(bruJson, 'request.script.res')
]).join(os.EOL); ]).join(os.EOL);
if (responseScriptFile && responseScriptFile.length) { if (responseScriptFile?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runResponseScript( await scriptRuntime.runResponseScript(
decomment(responseScriptFile), decomment(responseScriptFile),
@ -271,7 +293,7 @@ const runSingleRequest = async function (
testResults = get(result, 'results', []); testResults = get(result, 'results', []);
} }
if (testResults && testResults.length) { if (testResults?.length) {
each(testResults, (testResult) => { each(testResults, (testResult) => {
if (testResult.status === 'pass') { if (testResult.status === 'pass') {
console.log(chalk.green(``) + chalk.dim(testResult.description)); console.log(chalk.green(``) + chalk.dim(testResult.description));

View File

@ -225,7 +225,7 @@ const registerNetworkIpc = (mainWindow) => {
// run pre-request vars // run pre-request vars
const preRequestVars = get(request, 'vars.req', []); const preRequestVars = get(request, 'vars.req', []);
if (preRequestVars && preRequestVars.length) { if (preRequestVars?.length) {
const varsRuntime = new VarsRuntime(); const varsRuntime = new VarsRuntime();
const result = varsRuntime.runPreRequestVars( const result = varsRuntime.runPreRequestVars(
preRequestVars, preRequestVars,
@ -250,7 +250,7 @@ const registerNetworkIpc = (mainWindow) => {
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join( const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(
os.EOL os.EOL
); );
if (requestScript && requestScript.length) { if (requestScript?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runRequestScript( const result = await scriptRuntime.runRequestScript(
decomment(requestScript), decomment(requestScript),
@ -309,7 +309,7 @@ const registerNetworkIpc = (mainWindow) => {
// run post-response vars // run post-response vars
const postResponseVars = get(request, 'vars.res', []); const postResponseVars = get(request, 'vars.res', []);
if (postResponseVars && postResponseVars.length) { if (postResponseVars?.length) {
const varsRuntime = new VarsRuntime(); const varsRuntime = new VarsRuntime();
const result = varsRuntime.runPostResponseVars( const result = varsRuntime.runPostResponseVars(
postResponseVars, postResponseVars,
@ -335,7 +335,7 @@ const registerNetworkIpc = (mainWindow) => {
const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join( const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join(
os.EOL os.EOL
); );
if (responseScript && responseScript.length) { if (responseScript?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runResponseScript( const result = await scriptRuntime.runResponseScript(
decomment(responseScript), decomment(responseScript),
@ -438,7 +438,7 @@ const registerNetworkIpc = (mainWindow) => {
return Promise.reject(error); return Promise.reject(error);
} }
if (error && error.response) { if (error?.response) {
// run assertions // run assertions
const assertions = get(request, 'assertions'); const assertions = get(request, 'assertions');
if (assertions) { if (assertions) {
@ -530,10 +530,7 @@ const registerNetworkIpc = (mainWindow) => {
const collectionRoot = get(collection, 'root', {}); const collectionRoot = get(collection, 'root', {});
const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request, collectionRoot); const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request, collectionRoot);
const preferences = getPreferences(); request.timeout = preferences.getTimeout();
const timeout = get(preferences, 'request.timeout', 0);
request.timeout = timeout;
const sslVerification = get(preferences, 'request.sslVerification', true);
if (!preferences.isTlsVerification()) { if (!preferences.isTlsVerification()) {
request.httpsAgent = new https.Agent({ request.httpsAgent = new https.Agent({
@ -673,7 +670,7 @@ const registerNetworkIpc = (mainWindow) => {
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join( const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(
os.EOL os.EOL
); );
if (requestScript && requestScript.length) { if (requestScript?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runRequestScript( const result = await scriptRuntime.runRequestScript(
decomment(requestScript), decomment(requestScript),
@ -751,7 +748,7 @@ const registerNetworkIpc = (mainWindow) => {
get(collectionRoot, 'request.script.res'), get(collectionRoot, 'request.script.res'),
get(request, 'script.res') get(request, 'script.res')
]).join(os.EOL); ]).join(os.EOL);
if (responseScript && responseScript.length) { if (responseScript?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runResponseScript( const result = await scriptRuntime.runResponseScript(
decomment(responseScript), decomment(responseScript),
@ -845,7 +842,7 @@ const registerNetworkIpc = (mainWindow) => {
duration = timeEnd - timeStart; duration = timeEnd - timeStart;
} }
if (error && error.response) { if (error?.response) {
responseReceived = { responseReceived = {
status: error.response.status, status: error.response.status,
statusText: error.response.statusText, statusText: error.response.statusText,