feat: bru.setNextRequest()

This commit is contained in:
Martin Hoecker 2023-10-16 13:28:58 +02:00
parent a9459bc236
commit d4c0207545
No known key found for this signature in database
GPG Key ID: DD1434854AB52DD0
5 changed files with 60 additions and 8 deletions

View File

@ -357,7 +357,9 @@ const handler = async function (argv) {
}
}
for (const iter of bruJsons) {
let currentRequestIndex = 0;
while (currentRequestIndex < bruJsons.length) {
const iter = bruJsons[currentRequestIndex];
const { bruFilepath, bruJson } = iter;
const result = await runSingleRequest(
bruFilepath,
@ -370,6 +372,19 @@ const handler = async function (argv) {
collectionRoot
);
const nextRequestName = result?.nextRequestName;
if (nextRequestName) {
const nextRequestIdx = bruJsons.findIndex((iter) => iter.bruJson.name === nextRequestName);
if (nextRequestIdx > 0) {
currentRequestIndex = nextRequestIdx;
} else {
console.error("Could not find request with name '" + nextRequestName + "'");
currentRequestIndex++;
}
} else {
currentRequestIndex++;
}
results.push(result);
}

View File

@ -29,6 +29,7 @@ const runSingleRequest = async function (
) {
try {
let request;
let nextRequestName;
request = prepareRequest(bruJson.request, collectionRoot);
@ -66,7 +67,7 @@ const runSingleRequest = async function (
]).join(os.EOL);
if (requestScriptFile && requestScriptFile.length) {
const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runRequestScript(
const result = await scriptRuntime.runRequestScript(
decomment(requestScriptFile),
request,
envVariables,
@ -76,6 +77,9 @@ const runSingleRequest = async function (
processEnvVars,
scriptingConfig
);
if (result?.nextRequestName) {
nextRequestName = result.nextRequestName;
}
}
// interpolate variables inside request
@ -187,7 +191,8 @@ const runSingleRequest = async function (
},
error: err.message,
assertionResults: [],
testResults: []
testResults: [],
nextRequestName: nextRequestName
};
}
}
@ -219,7 +224,7 @@ const runSingleRequest = async function (
]).join(os.EOL);
if (responseScriptFile && responseScriptFile.length) {
const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runResponseScript(
const result = await scriptRuntime.runResponseScript(
decomment(responseScriptFile),
request,
response,
@ -230,6 +235,9 @@ const runSingleRequest = async function (
processEnvVars,
scriptingConfig
);
if (result?.nextRequestName) {
nextRequestName = result.nextRequestName;
}
}
// run assertions
@ -301,7 +309,8 @@ const runSingleRequest = async function (
},
error: null,
assertionResults,
testResults
testResults,
nextRequestName: nextRequestName
};
} catch (err) {
console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`));

View File

@ -605,7 +605,10 @@ const registerNetworkIpc = (mainWindow) => {
});
}
for (let item of folderRequests) {
let currentRequestIndex = 0;
while (currentRequestIndex < folderRequests.length) {
item = folderRequests[currentRequestIndex];
let nextRequestName = undefined;
const itemUid = item.uid;
const eventData = {
collectionUid,
@ -685,6 +688,10 @@ const registerNetworkIpc = (mainWindow) => {
collectionVariables: result.collectionVariables,
collectionUid
});
if (result?.nextRequestName) {
nextRequestName = result.nextRequestName;
}
}
// interpolate variables inside request
@ -807,6 +814,10 @@ const registerNetworkIpc = (mainWindow) => {
collectionVariables: result.collectionVariables,
collectionUid
});
if (result?.nextRequestName) {
nextRequestName = result.nextRequestName;
}
}
// run assertions
@ -963,6 +974,17 @@ const registerNetworkIpc = (mainWindow) => {
...eventData
});
}
if (nextRequestName) {
const nextRequestIdx = folderRequests.findIndex((request) => request.name === nextRequestName);
if (nextRequestIdx > 0) {
currentRequestIndex = nextRequestIdx;
} else {
console.error("Could not find request with name '" + nextRequestName + "'");
currentRequestIndex++;
}
} else {
currentRequestIndex++;
}
}
mainWindow.webContents.send('main:run-folder-event', {

View File

@ -65,6 +65,10 @@ class Bru {
getVar(key) {
return this.collectionVariables[key];
}
setNextRequest(nextRequest) {
this.nextRequest = nextRequest;
}
}
module.exports = Bru;

View File

@ -116,7 +116,8 @@ class ScriptRuntime {
return {
request,
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables)
collectionVariables: cleanJson(collectionVariables),
nextRequestName: bru.nextRequest
};
}
@ -207,7 +208,8 @@ class ScriptRuntime {
return {
response,
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables)
collectionVariables: cleanJson(collectionVariables),
nextRequestName: bru.nextRequest
};
}
}