mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-15 20:01:28 +00:00
Merge pull request #4590 from poojabela/feat/add-getName-api-for-script
feat: add `req.getName` & `bru.getCollectionName` api
This commit is contained in:
@@ -58,6 +58,7 @@ if (!SERVER_RENDERED) {
|
||||
'req.getTimeout()',
|
||||
'req.setTimeout(timeout)',
|
||||
'req.getExecutionMode()',
|
||||
'req.getName()',
|
||||
'bru',
|
||||
'bru.cwd()',
|
||||
'bru.getEnvName()',
|
||||
@@ -80,6 +81,7 @@ if (!SERVER_RENDERED) {
|
||||
'bru.getAssertionResults()',
|
||||
'bru.getTestResults()',
|
||||
'bru.sleep(ms)',
|
||||
'bru.getCollectionName()',
|
||||
'bru.getGlobalEnvVar(key)',
|
||||
'bru.setGlobalEnvVar(key, value)',
|
||||
'bru.runner',
|
||||
|
||||
@@ -32,6 +32,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'
|
||||
};
|
||||
|
||||
@@ -59,6 +59,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(
|
||||
@@ -70,7 +71,8 @@ const runSingleRequest = async function (
|
||||
onConsoleLog,
|
||||
processEnvVars,
|
||||
scriptingConfig,
|
||||
runSingleRequestByPathname
|
||||
runSingleRequestByPathname,
|
||||
collectionName
|
||||
);
|
||||
if (result?.nextRequestName !== undefined) {
|
||||
nextRequestName = result.nextRequestName;
|
||||
@@ -460,7 +462,8 @@ const runSingleRequest = async function (
|
||||
null,
|
||||
processEnvVars,
|
||||
scriptingConfig,
|
||||
runSingleRequestByPathname
|
||||
runSingleRequestByPathname,
|
||||
collectionName
|
||||
);
|
||||
if (result?.nextRequestName !== undefined) {
|
||||
nextRequestName = result.nextRequestName;
|
||||
@@ -510,7 +513,8 @@ const runSingleRequest = async function (
|
||||
null,
|
||||
processEnvVars,
|
||||
scriptingConfig,
|
||||
runSingleRequestByPathname
|
||||
runSingleRequestByPathname,
|
||||
collectionName
|
||||
);
|
||||
testResults = get(result, 'results', []);
|
||||
|
||||
|
||||
@@ -33,6 +33,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(',
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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'
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -49,14 +49,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', []);
|
||||
@@ -183,14 +184,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);
|
||||
|
||||
@@ -69,14 +69,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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
}
|
||||
@@ -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");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user