From 9b49d6e9a04ba32d48ecd2d75dea2390ddea675d Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Tue, 21 Jan 2025 23:15:07 +0530 Subject: [PATCH] ensure variables set in scripts/tests during `bru.runRequest` reflect in original request scripts/tests --- .../bruno-electron/src/ipc/network/index.js | 50 ++++++++-------- .../scripting/api/bru/runRequest-1.bru | 57 +++++++++++++++++++ .../scripting/api/bru/runRequest-2.bru | 21 +++++++ 3 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 packages/bruno-tests/collection/scripting/api/bru/runRequest-1.bru create mode 100644 packages/bruno-tests/collection/scripting/api/bru/runRequest-2.bru diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 1865426e0e..f541a2864c 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -274,7 +274,6 @@ const configureRequest = async ( }); } - let axiosInstance = makeAxiosInstance(); if (request.ntlmConfig) { @@ -403,7 +402,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -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 @@ -470,7 +471,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -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 @@ -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(); @@ -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}`); @@ -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); @@ -589,7 +591,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -674,7 +676,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -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) => { @@ -782,7 +787,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -818,7 +823,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -888,7 +893,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -912,7 +917,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -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(); @@ -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}`); @@ -983,7 +989,6 @@ const registerNetworkIpc = (mainWindow) => { }); try { - const envVars = getEnvVars(environment); let folderRequests = []; if (recursive) { @@ -1035,7 +1040,6 @@ const registerNetworkIpc = (mainWindow) => { request.__bruno__executionMode = 'runner'; const requestUid = uuid(); - const processEnvVars = getProcessEnvVars(collectionUid); try { const preRequestScriptResult = await runPreRequest( @@ -1043,7 +1047,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, @@ -1181,7 +1185,7 @@ const registerNetworkIpc = (mainWindow) => { requestUid, envVars, collectionPath, - collectionRoot, + collection, collectionUid, runtimeVariables, processEnvVars, diff --git a/packages/bruno-tests/collection/scripting/api/bru/runRequest-1.bru b/packages/bruno-tests/collection/scripting/api/bru/runRequest-1.bru new file mode 100644 index 0000000000..599f80c4cb --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/runRequest-1.bru @@ -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 + })); + }); +} diff --git a/packages/bruno-tests/collection/scripting/api/bru/runRequest-2.bru b/packages/bruno-tests/collection/scripting/api/bru/runRequest-2.bru new file mode 100644 index 0000000000..7a5f4d08d0 --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/runRequest-2.bru @@ -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'); +}