feat(#224): Bru CLI support for proxying requests

This commit is contained in:
Anoop M D 2023-09-28 05:26:09 +05:30
parent 3661fa7df3
commit f695036721
5 changed files with 75 additions and 11 deletions

View File

@ -1,5 +1,9 @@
# Changelog
## 0.10.0
- Support for proxying requests through a proxy server
## 0.9.0
- `--output` flag to collect the results of your API tests

View File

@ -1,6 +1,6 @@
{
"name": "@usebruno/cli",
"version": "0.9.0",
"version": "0.10.0",
"license": "MIT",
"main": "src/index.js",
"bin": {

View File

@ -165,6 +165,9 @@ const handler = async function (argv) {
return;
}
const brunoConfigFile = fs.readFileSync(brunoJsonPath, 'utf8');
const brunoConfig = JSON.parse(brunoConfigFile);
if (filename && filename.length) {
const pathExists = await exists(filename);
if (!pathExists) {
@ -304,7 +307,8 @@ const handler = async function (argv) {
collectionPath,
collectionVariables,
envVars,
processEnvVars
processEnvVars,
brunoConfig
);
if (result) {

View File

@ -85,6 +85,17 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
param.value = interpolate(param.value);
});
if (request.proxy) {
request.proxy.protocol = interpolate(request.proxy.protocol);
request.proxy.hostname = interpolate(request.proxy.hostname);
request.proxy.port = interpolate(request.proxy.port);
if (request.proxy.auth) {
request.proxy.auth.username = interpolate(request.proxy.auth.username);
request.proxy.auth.password = interpolate(request.proxy.auth.password);
}
}
return request;
};

View File

@ -17,7 +17,8 @@ const runSingleRequest = async function (
collectionPath,
collectionVariables,
envVariables,
processEnvVars
processEnvVars,
brunoConfig
) {
let request;
@ -39,7 +40,14 @@ const runSingleRequest = async function (
const preRequestVars = get(bruJson, 'request.vars.req');
if (preRequestVars && preRequestVars.length) {
const varsRuntime = new VarsRuntime();
varsRuntime.runPreRequestVars(preRequestVars, request, envVariables, collectionVariables, collectionPath);
varsRuntime.runPreRequestVars(
preRequestVars,
request,
envVariables,
collectionVariables,
collectionPath,
processEnvVars
);
}
// run pre request script
@ -51,10 +59,37 @@ const runSingleRequest = async function (
request,
envVariables,
collectionVariables,
collectionPath
collectionPath,
null,
processEnvVars
);
}
// set proxy if enabled
const proxyEnabled = get(brunoConfig, 'proxy.enabled', false);
if (proxyEnabled) {
const proxyProtocol = get(brunoConfig, 'proxy.protocol');
const proxyHostname = get(brunoConfig, 'proxy.hostname');
const proxyPort = get(brunoConfig, 'proxy.port');
const proxyAuthEnabled = get(brunoConfig, 'proxy.auth.enabled', false);
const proxyConfig = {
protocol: proxyProtocol,
hostname: proxyHostname,
port: proxyPort
};
if (proxyAuthEnabled) {
const proxyAuthUsername = get(brunoConfig, 'proxy.auth.username');
const proxyAuthPassword = get(brunoConfig, 'proxy.auth.password');
proxyConfig.auth = {
username: proxyAuthUsername,
password: proxyAuthPassword
};
}
request.proxy = proxyConfig;
}
// interpolate variables inside request
interpolateVars(request, envVariables, collectionVariables, processEnvVars);
@ -102,7 +137,8 @@ const runSingleRequest = async function (
response,
envVariables,
collectionVariables,
collectionPath
collectionPath,
processEnvVars
);
}
@ -116,7 +152,9 @@ const runSingleRequest = async function (
response,
envVariables,
collectionVariables,
collectionPath
collectionPath,
null,
processEnvVars
);
}
@ -155,7 +193,9 @@ const runSingleRequest = async function (
response,
envVariables,
collectionVariables,
collectionPath
collectionPath,
null,
processEnvVars
);
testResults = get(result, 'results', []);
}
@ -202,7 +242,8 @@ const runSingleRequest = async function (
err.response,
envVariables,
collectionVariables,
collectionPath
collectionPath,
processEnvVars
);
}
@ -216,7 +257,9 @@ const runSingleRequest = async function (
err.response,
envVariables,
collectionVariables,
collectionPath
collectionPath,
null,
processEnvVars
);
}
@ -255,7 +298,9 @@ const runSingleRequest = async function (
err.response,
envVariables,
collectionVariables,
collectionPath
collectionPath,
null,
processEnvVars
);
testResults = get(result, 'results', []);
}