Merge pull request #619 from mj-h/feature/add-bru-setNextRequest

feat: bru.setNextRequest()
This commit is contained in:
Anoop M D 2023-12-06 01:37:44 +05:30 committed by GitHub
commit 3061507284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 96 additions and 21 deletions

View File

@ -1322,29 +1322,29 @@ export const collectionsSlice = createSlice({
} }
if (type === 'request-sent') { if (type === 'request-sent') {
const item = collection.runnerResult.items.find((i) => i.uid === request.uid); const item = collection.runnerResult.items.find((i) => i.uid === request.uid && i.status === 'queued');
item.status = 'running'; item.status = 'running';
item.requestSent = action.payload.requestSent; item.requestSent = action.payload.requestSent;
} }
if (type === 'response-received') { if (type === 'response-received') {
const item = collection.runnerResult.items.find((i) => i.uid === request.uid); const item = collection.runnerResult.items.find((i) => i.uid === request.uid && i.status === 'running');
item.status = 'completed'; item.status = 'completed';
item.responseReceived = action.payload.responseReceived; item.responseReceived = action.payload.responseReceived;
} }
if (type === 'test-results') { if (type === 'test-results') {
const item = collection.runnerResult.items.find((i) => i.uid === request.uid); const item = collection.runnerResult.items.find((i) => i.uid === request.uid && i.status === 'running');
item.testResults = action.payload.testResults; item.testResults = action.payload.testResults;
} }
if (type === 'assertion-results') { if (type === 'assertion-results') {
const item = collection.runnerResult.items.find((i) => i.uid === request.uid); const item = collection.runnerResult.items.find((i) => i.uid === request.uid && i.status === 'running');
item.assertionResults = action.payload.assertionResults; item.assertionResults = action.payload.assertionResults;
} }
if (type === 'error') { if (type === 'error') {
const item = collection.runnerResult.items.find((i) => i.uid === request.uid); const item = collection.runnerResult.items.find((i) => i.uid === request.uid && i.status === 'running');
item.error = action.payload.error; item.error = action.payload.error;
item.responseReceived = action.payload.responseReceived; item.responseReceived = action.payload.responseReceived;
item.status = 'error'; item.status = 'error';

View File

@ -355,7 +355,10 @@ const handler = async function (argv) {
} }
} }
for (const iter of bruJsons) { let currentRequestIndex = 0;
let nJumps = 0; // count the number of jumps to avoid infinite loops
while (currentRequestIndex < bruJsons.length) {
const iter = bruJsons[currentRequestIndex];
const { bruFilepath, bruJson } = iter; const { bruFilepath, bruJson } = iter;
const result = await runSingleRequest( const result = await runSingleRequest(
bruFilepath, bruFilepath,
@ -369,6 +372,28 @@ const handler = async function (argv) {
); );
results.push(result); results.push(result);
// determine next request
const nextRequestName = result?.nextRequestName;
if (nextRequestName !== undefined) {
nJumps++;
if (nJumps > 10000) {
console.error(chalk.red(`Too many jumps, possible infinite loop`));
process.exit(1);
}
if (nextRequestName === null) {
break;
}
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++;
}
} }
const summary = printRunSummary(results); const summary = printRunSummary(results);

View File

@ -31,6 +31,7 @@ const runSingleRequest = async function (
) { ) {
try { try {
let request; let request;
let nextRequestName;
request = prepareRequest(bruJson.request, collectionRoot); request = prepareRequest(bruJson.request, collectionRoot);
@ -68,7 +69,7 @@ const runSingleRequest = async function (
]).join(os.EOL); ]).join(os.EOL);
if (requestScriptFile?.length) { if (requestScriptFile?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runRequestScript( const result = await scriptRuntime.runRequestScript(
decomment(requestScriptFile), decomment(requestScriptFile),
request, request,
envVariables, envVariables,
@ -78,6 +79,9 @@ const runSingleRequest = async function (
processEnvVars, processEnvVars,
scriptingConfig scriptingConfig
); );
if (result?.nextRequestName !== undefined) {
nextRequestName = result.nextRequestName;
}
} }
// interpolate variables inside request // interpolate variables inside request
@ -211,7 +215,8 @@ const runSingleRequest = async function (
}, },
error: err.message, error: err.message,
assertionResults: [], assertionResults: [],
testResults: [] testResults: [],
nextRequestName: nextRequestName
}; };
} }
} }
@ -245,7 +250,7 @@ const runSingleRequest = async function (
]).join(os.EOL); ]).join(os.EOL);
if (responseScriptFile?.length) { if (responseScriptFile?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
await scriptRuntime.runResponseScript( const result = await scriptRuntime.runResponseScript(
decomment(responseScriptFile), decomment(responseScriptFile),
request, request,
response, response,
@ -256,6 +261,9 @@ const runSingleRequest = async function (
processEnvVars, processEnvVars,
scriptingConfig scriptingConfig
); );
if (result?.nextRequestName !== undefined) {
nextRequestName = result.nextRequestName;
}
} }
// run assertions // run assertions
@ -327,7 +335,8 @@ const runSingleRequest = async function (
}, },
error: null, error: null,
assertionResults, assertionResults,
testResults testResults,
nextRequestName: nextRequestName
}; };
} catch (err) { } catch (err) {
console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`)); console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`));

View File

@ -258,10 +258,11 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run pre-request script // run pre-request script
let scriptResult;
const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(os.EOL); const requestScript = compact([get(collectionRoot, 'request.script.req'), get(request, 'script.req')]).join(os.EOL);
if (requestScript?.length) { if (requestScript?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runRequestScript( scriptResult = await scriptRuntime.runRequestScript(
decomment(requestScript), decomment(requestScript),
request, request,
envVars, envVars,
@ -273,8 +274,8 @@ const registerNetworkIpc = (mainWindow) => {
); );
mainWindow.webContents.send('main:script-environment-update', { mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables, envVariables: scriptResult.envVariables,
collectionVariables: result.collectionVariables, collectionVariables: scriptResult.collectionVariables,
requestUid, requestUid,
collectionUid collectionUid
}); });
@ -293,6 +294,8 @@ const registerNetworkIpc = (mainWindow) => {
if (request.headers['content-type'] === 'application/x-www-form-urlencoded') { if (request.headers['content-type'] === 'application/x-www-form-urlencoded') {
request.data = qs.stringify(request.data); request.data = qs.stringify(request.data);
} }
return scriptResult;
}; };
const runPostResponse = async ( const runPostResponse = async (
@ -332,12 +335,13 @@ const registerNetworkIpc = (mainWindow) => {
} }
// run post-response script // run post-response script
let scriptResult;
const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join( const responseScript = compact([get(collectionRoot, 'request.script.res'), get(request, 'script.res')]).join(
os.EOL os.EOL
); );
if (responseScript?.length) { if (responseScript?.length) {
const scriptRuntime = new ScriptRuntime(); const scriptRuntime = new ScriptRuntime();
const result = await scriptRuntime.runResponseScript( scriptResult = await scriptRuntime.runResponseScript(
decomment(responseScript), decomment(responseScript),
request, request,
response, response,
@ -350,12 +354,13 @@ const registerNetworkIpc = (mainWindow) => {
); );
mainWindow.webContents.send('main:script-environment-update', { mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables, envVariables: scriptResult.envVariables,
collectionVariables: result.collectionVariables, collectionVariables: scriptResult.collectionVariables,
requestUid, requestUid,
collectionUid collectionUid
}); });
} }
return scriptResult;
}; };
// handler for sending http request // handler for sending http request
@ -696,7 +701,11 @@ const registerNetworkIpc = (mainWindow) => {
}); });
} }
for (let item of folderRequests) { let currentRequestIndex = 0;
let nJumps = 0; // count the number of jumps to avoid infinite loops
while (currentRequestIndex < folderRequests.length) {
const item = folderRequests[currentRequestIndex];
let nextRequestName;
const itemUid = item.uid; const itemUid = item.uid;
const eventData = { const eventData = {
collectionUid, collectionUid,
@ -718,7 +727,7 @@ const registerNetworkIpc = (mainWindow) => {
const processEnvVars = getProcessEnvVars(collectionUid); const processEnvVars = getProcessEnvVars(collectionUid);
try { try {
await runPreRequest( const preRequestScriptResult = await runPreRequest(
request, request,
requestUid, requestUid,
envVars, envVars,
@ -730,6 +739,10 @@ const registerNetworkIpc = (mainWindow) => {
scriptingConfig scriptingConfig
); );
if (preRequestScriptResult?.nextRequestName !== undefined) {
nextRequestName = preRequestScriptResult.nextRequestName;
}
// todo: // todo:
// i have no clue why electron can't send the request object // i have no clue why electron can't send the request object
// without safeParseJSON(safeStringifyJSON(request.data)) // without safeParseJSON(safeStringifyJSON(request.data))
@ -805,7 +818,7 @@ const registerNetworkIpc = (mainWindow) => {
} }
} }
await runPostResponse( const postRequestScriptResult = await runPostResponse(
request, request,
response, response,
requestUid, requestUid,
@ -818,6 +831,10 @@ const registerNetworkIpc = (mainWindow) => {
scriptingConfig scriptingConfig
); );
if (postRequestScriptResult?.nextRequestName !== undefined) {
nextRequestName = postRequestScriptResult.nextRequestName;
}
// run assertions // run assertions
const assertions = get(item, 'request.assertions'); const assertions = get(item, 'request.assertions');
if (assertions) { if (assertions) {
@ -878,6 +895,24 @@ const registerNetworkIpc = (mainWindow) => {
...eventData ...eventData
}); });
} }
if (nextRequestName !== undefined) {
nJumps++;
if (nJumps > 10000) {
throw new Error('Too many jumps, possible infinite loop');
}
if (nextRequestName === null) {
break;
}
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', { mainWindow.webContents.send('main:run-folder-event', {

View File

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

View File

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