Skip to content

Commit

Permalink
ensure variables set in scripts/tests during bru.runRequest reflect…
Browse files Browse the repository at this point in the history
… in original request scripts/tests
  • Loading branch information
lohxt1 committed Jan 21, 2025
1 parent dab4bb6 commit 9b49d6e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 23 deletions.
50 changes: 27 additions & 23 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ const configureRequest = async (
});
}


let axiosInstance = makeAxiosInstance();

if (request.ntlmConfig) {
Expand Down Expand Up @@ -403,7 +402,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -437,6 +436,8 @@ const registerNetworkIpc = (mainWindow) => {
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: scriptResult.globalEnvironmentVariables
});

collection.globalEnvironmentVariables = scriptResult.globalEnvironmentVariables;
}

// interpolate variables inside request
Expand Down Expand Up @@ -470,7 +471,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -507,6 +508,8 @@ const registerNetworkIpc = (mainWindow) => {
if (result?.error) {
mainWindow.webContents.send('main:display-error', result.error);
}

collection.globalEnvironmentVariables = result.globalEnvironmentVariables;
}

// run post-response script
Expand Down Expand Up @@ -537,11 +540,13 @@ const registerNetworkIpc = (mainWindow) => {
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: scriptResult.globalEnvironmentVariables
});

collection.globalEnvironmentVariables = scriptResult.globalEnvironmentVariables;
}
return scriptResult;
};

const runRequest = async ({ item, collection, environment, runtimeVariables, runInBackground = false }) => {
const runRequest = async ({ item, collection, envVars, processEnvVars, runtimeVariables, runInBackground = false }) => {
const collectionUid = collection.uid;
const collectionPath = collection.pathname;
const cancelTokenUid = uuid();
Expand All @@ -553,9 +558,9 @@ const registerNetworkIpc = (mainWindow) => {
if (itemPathname && !itemPathname?.endsWith('.bru')) {
itemPathname = `${itemPathname}.bru`;
}
const _item = findItemInCollectionByPathname(collection, itemPathname);
const _item = cloneDeep(findItemInCollectionByPathname(collection, itemPathname));
if(_item) {
const res = await runRequest({ item: _item, collection, environment, runtimeVariables, runInBackground: true });
const res = await runRequest({ item: _item, collection, envVars, processEnvVars, runtimeVariables, runInBackground: true });
resolve(res);
}
reject(`bru.runRequest: invalid request path - ${itemPathname}`);
Expand All @@ -570,11 +575,8 @@ const registerNetworkIpc = (mainWindow) => {
cancelTokenUid
});

const collectionRoot = get(collection, 'root', {});
const request = prepareRequest(item, collection);
request.__bruno__executionMode = 'standalone';
const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid);
const brunoConfig = getBrunoConfig(collectionUid);
const scriptingConfig = get(brunoConfig, 'scripts', {});
scriptingConfig.runtime = getJsSandboxRuntime(collection);
Expand All @@ -589,7 +591,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -674,7 +676,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -758,7 +760,10 @@ const registerNetworkIpc = (mainWindow) => {

// handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collection, environment, runtimeVariables) => {
return await runRequest({ item, collection, environment, runtimeVariables });
const collectionUid = collection.uid;
const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid);
return await runRequest({ item, collection, envVars, processEnvVars, runtimeVariables, runInBackground: false });
});

ipcMain.handle('send-collection-oauth2-request', async (event, collection, environment, runtimeVariables) => {
Expand All @@ -782,7 +787,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -818,7 +823,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -888,7 +893,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand All @@ -912,7 +917,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -949,7 +954,8 @@ const registerNetworkIpc = (mainWindow) => {
const brunoConfig = getBrunoConfig(collectionUid);
const scriptingConfig = get(brunoConfig, 'scripts', {});
scriptingConfig.runtime = getJsSandboxRuntime(collection);
const collectionRoot = get(collection, 'root', {});
const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid);
let stopRunnerExecution = false;

const abortController = new AbortController();
Expand All @@ -961,9 +967,9 @@ const registerNetworkIpc = (mainWindow) => {
if (itemPathname && !itemPathname?.endsWith('.bru')) {
itemPathname = `${itemPathname}.bru`;
}
const _item = findItemInCollectionByPathname(collection, itemPathname);
const _item = cloneDeep(findItemInCollectionByPathname(collection, itemPathname));
if(_item) {
const res = await runRequest({ item: _item, collection, environment, runtimeVariables, runInBackground: true });
const res = await runRequest({ item: _item, collection, envVars, processEnvVars, runtimeVariables, runInBackground: true });
resolve(res);
}
reject(`bru.runRequest: invalid request path - ${itemPathname}`);
Expand All @@ -983,7 +989,6 @@ const registerNetworkIpc = (mainWindow) => {
});

try {
const envVars = getEnvVars(environment);
let folderRequests = [];

if (recursive) {
Expand Down Expand Up @@ -1035,15 +1040,14 @@ const registerNetworkIpc = (mainWindow) => {
request.__bruno__executionMode = 'runner';

const requestUid = uuid();
const processEnvVars = getProcessEnvVars(collectionUid);

try {
const preRequestScriptResult = await runPreRequest(
request,
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down Expand Up @@ -1181,7 +1185,7 @@ const registerNetworkIpc = (mainWindow) => {
requestUid,
envVars,
collectionPath,
collectionRoot,
collection,
collectionUid,
runtimeVariables,
processEnvVars,
Expand Down
57 changes: 57 additions & 0 deletions packages/bruno-tests/collection/scripting/api/bru/runRequest-1.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
meta {
name: runRequest-1
type: http
seq: 10
}

post {
url: {{echo-host}}
body: text
auth: none
}

body:text {
bruno
}

script:pre-request {
// reset values
bru.setVar('run-request-runtime-var', null);
bru.setEnvVar('run-request-env-var', null);
bru.setGlobalEnvVar('run-request-global-env-var', null);

// the above vars will be set in the below request
const resp = await bru.runRequest('scripting/api/bru/runRequest-2');

bru.setVar('run-request-resp', {
data: resp?.data,
statusText: resp?.statusText,
status: resp?.status
});
}

tests {
test("should get runtime var set in runRequest-2", function() {
const val = bru.getVar("run-request-runtime-var");
expect(val).to.equal("run-request-runtime-var-value");
});

test("should get env var set in runRequest-2", function() {
const val = bru.getEnvVar("run-request-env-var");
expect(val).to.equal("run-request-env-var-value");
});

test("should get global env var set in runRequest-2", function() {
const val = bru.getGlobalEnvVar("run-request-global-env-var");
expect(val).to.equal("run-request-global-env-var-value");
});

test("should get response of runRequest-2", function() {
const val = bru.getVar('run-request-resp');
expect(JSON.stringify(val)).to.equal(JSON.stringify({
"data": "bruno",
"statusText": "OK",
"status": 200
}));
});
}
21 changes: 21 additions & 0 deletions packages/bruno-tests/collection/scripting/api/bru/runRequest-2.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
meta {
name: runRequest-2
type: http
seq: 11
}

post {
url: {{echo-host}}
body: text
auth: none
}

body:text {
bruno
}

script:pre-request {
bru.setVar('run-request-runtime-var', 'run-request-runtime-var-value');
bru.setEnvVar('run-request-env-var', 'run-request-env-var-value');
bru.setGlobalEnvVar('run-request-global-env-var', 'run-request-global-env-var-value');
}

0 comments on commit 9b49d6e

Please sign in to comment.