From cb92e46f8d091cc4b7d5d10ab30249057b9bca49 Mon Sep 17 00:00:00 2001 From: poojabela Date: Wed, 30 Apr 2025 15:14:36 +0530 Subject: [PATCH 1/4] feat: add `req.getName` api --- .../bruno-converters/src/postman/postman-translations.js | 1 + .../postman-translations/postman-request.spec.js | 2 ++ .../bruno-electron/src/ipc/network/prepare-request.js | 1 + packages/bruno-js/src/bruno-request.js | 6 +++++- .../bruno-js/src/sandbox/quickjs/shims/bruno-request.js | 9 +++++++++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/bruno-converters/src/postman/postman-translations.js b/packages/bruno-converters/src/postman/postman-translations.js index b741cd3b2..00b3b945e 100644 --- a/packages/bruno-converters/src/postman/postman-translations.js +++ b/packages/bruno-converters/src/postman/postman-translations.js @@ -26,6 +26,7 @@ const replacements = { 'pm\\.request\\.method': 'req.getMethod()', 'pm\\.request\\.headers': 'req.getHeaders()', 'pm\\.request\\.body': 'req.getBody()', + 'pm\\.info\\.requestName': 'req.getName()', // deprecated translations 'postman\\.setEnvironmentVariable\\(': 'bru.setEnvVar(', 'postman\\.getEnvironmentVariable\\(': 'bru.getEnvVar(', diff --git a/packages/bruno-converters/tests/postman/postman-to-bruno/postman-translations/postman-request.spec.js b/packages/bruno-converters/tests/postman/postman-to-bruno/postman-translations/postman-request.spec.js index 3ee071640..93eed719a 100644 --- a/packages/bruno-converters/tests/postman/postman-to-bruno/postman-translations/postman-request.spec.js +++ b/packages/bruno-converters/tests/postman/postman-to-bruno/postman-translations/postman-request.spec.js @@ -7,6 +7,7 @@ describe('postmanTranslations - request commands', () => { const requestMethod = pm.request.method; const requestHeaders = pm.request.headers; const requestBody = pm.request.body; + const requestName = pm.info.requestName; pm.test('Request method is POST', function() { pm.expect(pm.request.method).to.equal('POST'); @@ -17,6 +18,7 @@ describe('postmanTranslations - request commands', () => { const requestMethod = req.getMethod(); const requestHeaders = req.getHeaders(); const requestBody = req.getBody(); + const requestName = req.getName(); test('Request method is POST', function() { expect(req.getMethod()).to.equal('POST'); diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index 749e32a6d..9efa56e45 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -301,6 +301,7 @@ const prepareRequest = async (item, collection = {}, abortController) => { method: request.method, url, headers, + name: item.name, pathParams: request?.params?.filter((param) => param.type === 'path'), responseType: 'arraybuffer' }; diff --git a/packages/bruno-js/src/bruno-request.js b/packages/bruno-js/src/bruno-request.js index 32e40c19e..3ee2127ca 100644 --- a/packages/bruno-js/src/bruno-request.js +++ b/packages/bruno-js/src/bruno-request.js @@ -17,7 +17,7 @@ class BrunoRequest { this.method = req.method; this.headers = req.headers; this.timeout = req.timeout; - + this.name = req.name; /** * We automatically parse the JSON body if the content type is JSON * This is to make it easier for the user to access the body directly @@ -177,6 +177,10 @@ class BrunoRequest { getExecutionMode() { return this.req.__bruno__executionMode; } + + getName() { + return this.req.name; + } } module.exports = BrunoRequest; diff --git a/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js b/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js index e3f364fe7..4a51ae580 100644 --- a/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js +++ b/packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js @@ -8,18 +8,21 @@ const addBrunoRequestShimToContext = (vm, req) => { const headers = marshallToVm(req.getHeaders(), vm); const body = marshallToVm(req.getBody(), vm); const timeout = marshallToVm(req.getTimeout(), vm); + const name = marshallToVm(req.getName(), vm); vm.setProp(reqObject, 'url', url); vm.setProp(reqObject, 'method', method); vm.setProp(reqObject, 'headers', headers); vm.setProp(reqObject, 'body', body); vm.setProp(reqObject, 'timeout', timeout); + vm.setProp(reqObject, 'name', name); url.dispose(); method.dispose(); headers.dispose(); body.dispose(); timeout.dispose(); + name.dispose(); let getUrl = vm.newFunction('getUrl', function () { return marshallToVm(req.getUrl(), vm); @@ -45,6 +48,12 @@ const addBrunoRequestShimToContext = (vm, req) => { vm.setProp(reqObject, 'getAuthMode', getAuthMode); getAuthMode.dispose(); + let getName = vm.newFunction('getName', function () { + return marshallToVm(req.getName(), vm); + }); + vm.setProp(reqObject, 'getName', getName); + getName.dispose(); + let setMethod = vm.newFunction('setMethod', function (method) { req.setMethod(vm.dump(method)); }); From 261a36c4350441542f11d97c69453ae3b762258a Mon Sep 17 00:00:00 2001 From: poojabela Date: Wed, 30 Apr 2025 15:29:10 +0530 Subject: [PATCH 2/4] add: getName in hint --- packages/bruno-app/src/components/CodeEditor/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index f574cf82f..330662256 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -58,6 +58,7 @@ if (!SERVER_RENDERED) { 'req.getTimeout()', 'req.setTimeout(timeout)', 'req.getExecutionMode()', + 'req.getName()', 'bru', 'bru.cwd()', 'bru.getEnvName()', From ba9362ccb2c9a0d54d0ef31f0595d4bfc44eef3e Mon Sep 17 00:00:00 2001 From: poojabela Date: Wed, 30 Apr 2025 15:36:44 +0530 Subject: [PATCH 3/4] add: getName in collection --- .../collection/scripting/api/req/getName.bru | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/bruno-tests/collection/scripting/api/req/getName.bru diff --git a/packages/bruno-tests/collection/scripting/api/req/getName.bru b/packages/bruno-tests/collection/scripting/api/req/getName.bru new file mode 100644 index 000000000..95e0369f5 --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/req/getName.bru @@ -0,0 +1,17 @@ +meta { + name: getName + type: http + seq: 11 +} + +get { + url: {{host}}/ping + body: none + auth: inherit +} + +tests { + test("Check if request name is getName", function () { + expect(req.getName()).to.eql("getName"); + }); +} From e0fb379511cdff1c13dd846ac58f759db592023c Mon Sep 17 00:00:00 2001 From: poojabela Date: Wed, 30 Apr 2025 17:25:42 +0530 Subject: [PATCH 4/4] add: `bru.collectionName` api --- .../src/components/CodeEditor/index.js | 1 + .../bruno-cli/src/runner/prepare-request.js | 1 + .../bruno-cli/src/runner/run-single-request.js | 10 +++++++--- .../bruno-electron/src/ipc/network/index.js | 16 ++++++++++++---- packages/bruno-js/src/bru.js | 7 ++++++- packages/bruno-js/src/runtime/script-runtime.js | 10 ++++++---- packages/bruno-js/src/runtime/test-runtime.js | 5 +++-- .../bruno-js/src/sandbox/quickjs/shims/bru.js | 6 ++++++ .../scripting/api/bru/getCollectionName.bru | 17 +++++++++++++++++ 9 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 packages/bruno-tests/collection/scripting/api/bru/getCollectionName.bru diff --git a/packages/bruno-app/src/components/CodeEditor/index.js b/packages/bruno-app/src/components/CodeEditor/index.js index 330662256..f8c13462e 100644 --- a/packages/bruno-app/src/components/CodeEditor/index.js +++ b/packages/bruno-app/src/components/CodeEditor/index.js @@ -81,6 +81,7 @@ if (!SERVER_RENDERED) { 'bru.getAssertionResults()', 'bru.getTestResults()', 'bru.sleep(ms)', + 'bru.getCollectionName()', 'bru.getGlobalEnvVar(key)', 'bru.setGlobalEnvVar(key, value)', 'bru.runner', diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index 7e7f5d3ac..1725b53b7 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -31,6 +31,7 @@ const prepareRequest = (item = {}, collection = {}) => { method: request.method, url: request.url, headers: headers, + name: item.name, pathParams: request?.params?.filter((param) => param.type === 'path'), responseType: 'arraybuffer' }; diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 774587614..a8e5aee01 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -60,6 +60,7 @@ const runSingleRequest = async function ( // run pre request script const requestScriptFile = get(request, 'script.req'); + const collectionName = collection?.brunoConfig?.name if (requestScriptFile?.length) { const scriptRuntime = new ScriptRuntime({ runtime: scriptingConfig?.runtime }); const result = await scriptRuntime.runRequestScript( @@ -71,7 +72,8 @@ const runSingleRequest = async function ( onConsoleLog, processEnvVars, scriptingConfig, - runSingleRequestByPathname + runSingleRequestByPathname, + collectionName ); if (result?.nextRequestName !== undefined) { nextRequestName = result.nextRequestName; @@ -425,7 +427,8 @@ const runSingleRequest = async function ( null, processEnvVars, scriptingConfig, - runSingleRequestByPathname + runSingleRequestByPathname, + collectionName ); if (result?.nextRequestName !== undefined) { nextRequestName = result.nextRequestName; @@ -475,7 +478,8 @@ const runSingleRequest = async function ( null, processEnvVars, scriptingConfig, - runSingleRequestByPathname + runSingleRequestByPathname, + collectionName ); testResults = get(result, 'results', []); diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 8fddd2d98..ab8d146e0 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -341,6 +341,7 @@ const registerNetworkIpc = (mainWindow) => { ) => { // run pre-request script let scriptResult; + const collectionName = collection?.name const requestScript = get(request, 'script.req'); if (requestScript?.length) { const scriptRuntime = new ScriptRuntime({ runtime: scriptingConfig?.runtime }); @@ -353,7 +354,8 @@ const registerNetworkIpc = (mainWindow) => { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ); mainWindow.webContents.send('main:script-environment-update', { @@ -447,6 +449,7 @@ const registerNetworkIpc = (mainWindow) => { // run post-response script const responseScript = get(request, 'script.res'); let scriptResult; + const collectionName = collection?.name if (responseScript?.length) { const scriptRuntime = new ScriptRuntime({ runtime: scriptingConfig?.runtime }); scriptResult = await scriptRuntime.runResponseScript( @@ -459,7 +462,8 @@ const registerNetworkIpc = (mainWindow) => { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ); mainWindow.webContents.send('main:script-environment-update', { @@ -706,6 +710,7 @@ const registerNetworkIpc = (mainWindow) => { } const testFile = get(request, 'tests'); + const collectionName = collection?.name if (typeof testFile === 'string') { const testRuntime = new TestRuntime({ runtime: scriptingConfig?.runtime }); const testResults = await testRuntime.runTests( @@ -718,7 +723,8 @@ const registerNetworkIpc = (mainWindow) => { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ); !runInBackground && mainWindow.webContents.send('main:run-request-event', { @@ -1171,6 +1177,7 @@ const registerNetworkIpc = (mainWindow) => { } const testFile = get(request, 'tests'); + const collectionName = collection?.name if (typeof testFile === 'string') { const testRuntime = new TestRuntime({ runtime: scriptingConfig?.runtime }); const testResults = await testRuntime.runTests( @@ -1183,7 +1190,8 @@ const registerNetworkIpc = (mainWindow) => { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ); if (testResults?.nextRequestName !== undefined) { diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index 76360a97c..77255b3a1 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -4,7 +4,7 @@ const { interpolate } = require('@usebruno/common'); const variableNameRegex = /^[\w-.]*$/; class Bru { - constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables) { + constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName) { this.envVariables = envVariables || {}; this.runtimeVariables = runtimeVariables || {}; this.processEnvVars = cloneDeep(processEnvVars || {}); @@ -14,6 +14,7 @@ class Bru { this.globalEnvironmentVariables = globalEnvironmentVariables || {}; this.oauth2CredentialVariables = oauth2CredentialVariables || {}; this.collectionPath = collectionPath; + this.collectionName = collectionName; this.runner = { skipRequest: () => { this.skipRequest = true; @@ -159,6 +160,10 @@ class Bru { sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } + + getCollectionName() { + return this.collectionName; + } } module.exports = Bru; diff --git a/packages/bruno-js/src/runtime/script-runtime.js b/packages/bruno-js/src/runtime/script-runtime.js index 2a8d02a87..165dc2f34 100644 --- a/packages/bruno-js/src/runtime/script-runtime.js +++ b/packages/bruno-js/src/runtime/script-runtime.js @@ -48,14 +48,15 @@ class ScriptRuntime { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ) { const globalEnvironmentVariables = request?.globalEnvironmentVariables || {}; const oauth2CredentialVariables = request?.oauth2CredentialVariables || {}; const collectionVariables = request?.collectionVariables || {}; const folderVariables = request?.folderVariables || {}; const requestVariables = request?.requestVariables || {}; - const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables); + const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName); const req = new BrunoRequest(request); const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false); const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []); @@ -181,14 +182,15 @@ class ScriptRuntime { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ) { const globalEnvironmentVariables = request?.globalEnvironmentVariables || {}; const oauth2CredentialVariables = request?.oauth2CredentialVariables || {}; const collectionVariables = request?.collectionVariables || {}; const folderVariables = request?.folderVariables || {}; const requestVariables = request?.requestVariables || {}; - const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables); + const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName); const req = new BrunoRequest(request); const res = new BrunoResponse(response); const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false); diff --git a/packages/bruno-js/src/runtime/test-runtime.js b/packages/bruno-js/src/runtime/test-runtime.js index e2d1f4865..101dd4333 100644 --- a/packages/bruno-js/src/runtime/test-runtime.js +++ b/packages/bruno-js/src/runtime/test-runtime.js @@ -68,14 +68,15 @@ class TestRuntime { onConsoleLog, processEnvVars, scriptingConfig, - runRequestByItemPathname + runRequestByItemPathname, + collectionName ) { const globalEnvironmentVariables = request?.globalEnvironmentVariables || {}; const collectionVariables = request?.collectionVariables || {}; const folderVariables = request?.folderVariables || {}; const requestVariables = request?.requestVariables || {}; const assertionResults = request?.assertionResults || []; - const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables); + const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, {}, collectionName); const req = new BrunoRequest(request); const res = new BrunoResponse(response); const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false); diff --git a/packages/bruno-js/src/sandbox/quickjs/shims/bru.js b/packages/bruno-js/src/sandbox/quickjs/shims/bru.js index b5b807ec2..b8ffa76ab 100644 --- a/packages/bruno-js/src/sandbox/quickjs/shims/bru.js +++ b/packages/bruno-js/src/sandbox/quickjs/shims/bru.js @@ -17,6 +17,12 @@ const addBruShimToContext = (vm, bru) => { vm.setProp(bruObject, 'getEnvName', getEnvName); getEnvName.dispose(); + let getCollectionName = vm.newFunction('getCollectionName', function () { + return marshallToVm(bru.getCollectionName(), vm); + }); + vm.setProp(bruObject, 'getCollectionName', getCollectionName); + getCollectionName.dispose(); + let getProcessEnv = vm.newFunction('getProcessEnv', function (key) { return marshallToVm(bru.getProcessEnv(vm.dump(key)), vm); }); diff --git a/packages/bruno-tests/collection/scripting/api/bru/getCollectionName.bru b/packages/bruno-tests/collection/scripting/api/bru/getCollectionName.bru new file mode 100644 index 000000000..3302d38fd --- /dev/null +++ b/packages/bruno-tests/collection/scripting/api/bru/getCollectionName.bru @@ -0,0 +1,17 @@ +meta { + name: getCollectionName + type: http + seq: 13 +} + +get { + url: {{host}}/ping + body: none + auth: inherit +} + +tests { + test("Check if collection name is bruno-testbench", function () { + expect(bru.getCollectionName()).to.eql("bruno-testbench"); + }); +}