1
0
mirror of https://github.com/usebruno/bruno.git synced 2025-01-05 13:39:13 +01:00

Merge pull request 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
packages
bruno-app/src/providers/ReduxStore/slices/collections
bruno-cli/src
bruno-electron/src/ipc/network
bruno-js/src

View File

@ -1322,29 +1322,29 @@ export const collectionsSlice = createSlice({
}
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.requestSent = action.payload.requestSent;
}
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.responseReceived = action.payload.responseReceived;
}
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;
}
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;
}
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.responseReceived = action.payload.responseReceived;
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 result = await runSingleRequest(
bruFilepath,
@ -369,6 +372,28 @@ const handler = async function (argv) {
);
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);

View File

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

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

View File

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

View File

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