Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bru.getCollectionName() and req.getName() functions #3815

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/bruno-app/src/components/CodeEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ if (!SERVER_RENDERED) {
'req.getTimeout()',
'req.setTimeout(timeout)',
'req.getExecutionMode()',
'req.getName()',
'bru',
'bru.cwd()',
'bru.getEnvName()',
Expand All @@ -80,7 +81,8 @@ if (!SERVER_RENDERED) {
'bru.runner',
'bru.runner.setNextRequest(requestName)',
'bru.runner.skipRequest()',
'bru.runner.stopExecution()'
'bru.runner.stopExecution()',
'bru.getCollectionName()'
];
CodeMirror.registerHelper('hint', 'brunoJS', (editor, options) => {
const cursor = editor.getCursor();
Expand Down
2 changes: 2 additions & 0 deletions packages/bruno-cli/src/runner/run-single-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const runSingleRequest = async function (
request = prepareRequest(item, collection);

request.__bruno__executionMode = 'cli';
request.__bruno__collectionName = brunoConfig?.name;
request.__bruno__requestName = item?.name;

const scriptingConfig = get(brunoConfig, 'scripts', {});
scriptingConfig.runtime = runtime;
Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ const registerNetworkIpc = (mainWindow) => {
const collectionRoot = get(collection, 'root', {});
const request = prepareRequest(item, collection);
request.__bruno__executionMode = 'standalone';
request.__bruno__collectionName = collection?.name;
request.__bruno__requestName = item?.name;
const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid);
const brunoConfig = getBrunoConfig(collectionUid);
Expand Down Expand Up @@ -771,6 +773,8 @@ const registerNetworkIpc = (mainWindow) => {
const _request = collectionRoot?.request;
const request = prepareCollectionRequest(_request, collection, collectionPath);
request.__bruno__executionMode = 'standalone';
request.__bruno__collectionName = collection?.name;
request.__bruno__requestName = item?.name;
const envVars = getEnvVars(environment);
const processEnvVars = getProcessEnvVars(collectionUid);
const brunoConfig = getBrunoConfig(collectionUid);
Expand Down Expand Up @@ -1033,6 +1037,8 @@ const registerNetworkIpc = (mainWindow) => {

const request = prepareRequest(item, collection);
request.__bruno__executionMode = 'runner';
request.__bruno__collectionName = collection?.name;
request.__bruno__requestName = item?.name;

const requestUid = uuid();
const processEnvVars = getProcessEnvVars(collectionUid);
Expand Down
7 changes: 6 additions & 1 deletion packages/bruno-js/src/bru.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { interpolate } = require('@usebruno/common');
const variableNameRegex = /^[\w-.]*$/;

class Bru {
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables) {
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, collectionName) {
this.envVariables = envVariables || {};
this.runtimeVariables = runtimeVariables || {};
this.processEnvVars = cloneDeep(processEnvVars || {});
Expand All @@ -13,6 +13,7 @@ class Bru {
this.requestVariables = requestVariables || {};
this.globalEnvironmentVariables = globalEnvironmentVariables || {};
this.collectionPath = collectionPath;
this.collectionName = collectionName;
this.runner = {
skipRequest: () => {
this.skipRequest = true;
Expand Down Expand Up @@ -153,6 +154,10 @@ class Bru {
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

getCollectionName() {
return this.collectionName;
}
}

module.exports = Bru;
4 changes: 4 additions & 0 deletions packages/bruno-js/src/bruno-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class BrunoRequest {
getExecutionMode() {
return this.req.__bruno__executionMode;
}

getName() {
return this.req.__bruno__requestName;
}
}

module.exports = BrunoRequest;
6 changes: 4 additions & 2 deletions packages/bruno-js/src/runtime/script-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class ScriptRuntime {
const collectionVariables = request?.collectionVariables || {};
const folderVariables = request?.folderVariables || {};
const requestVariables = request?.requestVariables || {};
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables);
const collectionName = request?.__bruno__collectionName;
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, collectionName);
const req = new BrunoRequest(request);
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
Expand Down Expand Up @@ -181,7 +182,8 @@ class ScriptRuntime {
const collectionVariables = request?.collectionVariables || {};
const folderVariables = request?.folderVariables || {};
const requestVariables = request?.requestVariables || {};
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables);
const collectionName = request?.__bruno__collectionName;
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);
Expand Down
3 changes: 2 additions & 1 deletion packages/bruno-js/src/runtime/test-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class TestRuntime {
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 collectionName = request?.__bruno__collectionName;
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);
Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-js/src/sandbox/quickjs/shims/bru.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ const addBruShimToContext = (vm, bru) => {
});
sleep.consume((handle) => vm.setProp(bruObject, 'sleep', handle));

let getCollectionName = vm.newFunction('getCollectionName', function () {
return marshallToVm(bru.getCollectionName(), vm);
});
vm.setProp(bruObject, 'getCollectionName', getCollectionName);
getCollectionName.dispose();

vm.setProp(bruObject, 'runner', bruRunnerObject);
vm.setProp(vm.global, 'bru', bruObject);
bruObject.dispose();
Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ const addBrunoRequestShimToContext = (vm, req) => {
vm.setProp(reqObject, 'getExecutionMode', getExecutionMode);
getExecutionMode.dispose();

let getName = vm.newFunction('getName', function () {
return marshallToVm(req.getName(), vm);
});
vm.setProp(reqObject, 'getName', getName);
getName.dispose();

vm.setProp(vm.global, 'req', reqObject);
reqObject.dispose();
};
Expand Down
6 changes: 6 additions & 0 deletions packages/bruno-tests/collection/collection.bru
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ script:pre-request {
if(shouldTestCollectionScripts) {
bru.setVar('collection-var-set-by-collection-script', 'collection-var-value-set-by-collection-script');
}
{
const collectionName = bru.getCollectionName();
bru.setVar("collectionName-collection-level-scripts", collectionName);
const requestName = req.getName();
bru.setVar("requestName-collection-level-scripts", requestName);
}
}

tests {
Expand Down
7 changes: 7 additions & 0 deletions packages/bruno-tests/collection/scripting/api/bru/folder.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ meta {
vars:pre-request {
folder-var: folder-var-value
}

script:pre-request {
{
const collectionName = bru.getCollectionName();
bru.setVar("collectionName-folder-level-scripts", collectionName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
meta {
name: getCollectionName
type: http
seq: 10
}

get {
url: {{host}}/ping
body: none
auth: none
}

script:pre-request {
{
const collectionName = bru.getCollectionName();
bru.setVar("collectionName-request-level-scripts", collectionName);
}

}

tests {
test("should get collection name in request-level scripts", function() {
const collectionName = bru.getVar("collectionName-request-level-scripts");
expect(collectionName).to.equal("bruno-testbench");
});

test("should get collection name in folder-level scripts", function() {
const collectionName = bru.getVar("collectionName-folder-level-scripts");
expect(collectionName).to.equal("bruno-testbench");
});

test("should get collection name in collection-level scripts", function() {
const collectionName = bru.getVar("collectionName-collection-level-scripts");
expect(collectionName).to.equal("bruno-testbench");
});
}
10 changes: 10 additions & 0 deletions packages/bruno-tests/collection/scripting/api/req/folder.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
meta {
name: req
}

script:pre-request {
{
const requestName = req.getName();
bru.setVar("requestName-folder-level-scripts", requestName);
}
}
41 changes: 41 additions & 0 deletions packages/bruno-tests/collection/scripting/api/req/getName.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
meta {
name: getName
type: http
seq: 11
}

get {
url: {{host}}/ping
body: none
auth: none
}

assert {
res.status: eq 200
res.body: eq pong
}

script:pre-request {
{
const requestName = req.getName();
bru.setVar("requestName-request-level-scripts", requestName);
}

}

tests {
test("should get request name in request-level scripts", function() {
const requestName = bru.getVar("requestName-request-level-scripts");
expect(requestName).to.equal("getName");
});

test("should get request name in folder-level scripts", function() {
const requestName = bru.getVar("requestName-folder-level-scripts");
expect(requestName).to.equal("getName");
});

test("should get request name in collection-level scripts", function() {
const requestName = bru.getVar("requestName-collection-level-scripts");
expect(requestName).to.equal("getName");
});
}
Loading