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

feat: bru.setNextRequest() #619

Merged
merged 7 commits into from
Dec 5, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1303,29 +1303,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';
Expand Down
27 changes: 26 additions & 1 deletion packages/bruno-cli/src/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Copy link

@fpassaniti fpassaniti Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be a parameter in bruno.json (the max number of jumps allowed) ? just in case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it -- but there are already a ton of config options... Do you have a use-case that needs it? If so, we can add it. If not, I would say YAGNI.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

YAGNI it is then ;)

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);
Expand Down
17 changes: 13 additions & 4 deletions packages/bruno-cli/src/runner/run-single-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const runSingleRequest = async function (
) {
try {
let request;
let nextRequestName;

request = prepareRequest(bruJson.request, collectionRoot);

Expand Down Expand Up @@ -66,7 +67,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,
Expand All @@ -76,6 +77,9 @@ const runSingleRequest = async function (
processEnvVars,
scriptingConfig
);
if (result?.nextRequestName !== undefined) {
nextRequestName = result.nextRequestName;
}
}

// interpolate variables inside request
Expand Down Expand Up @@ -204,7 +208,8 @@ const runSingleRequest = async function (
},
error: err.message,
assertionResults: [],
testResults: []
testResults: [],
nextRequestName: nextRequestName
};
}
}
Expand Down Expand Up @@ -236,7 +241,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,
Expand All @@ -247,6 +252,9 @@ const runSingleRequest = async function (
processEnvVars,
scriptingConfig
);
if (result?.nextRequestName !== undefined) {
nextRequestName = result.nextRequestName;
}
}

// run assertions
Expand Down Expand Up @@ -318,7 +326,8 @@ const runSingleRequest = async function (
},
error: null,
assertionResults,
testResults
testResults,
nextRequestName: nextRequestName
};
} catch (err) {
console.log(chalk.red(stripExtension(filename)) + chalk.dim(` (${err.message})`));
Expand Down
53 changes: 44 additions & 9 deletions packages/bruno-electron/src/ipc/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,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,
Expand All @@ -269,8 +270,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
});
Expand All @@ -283,6 +284,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 (
Expand Down Expand Up @@ -322,12 +325,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,
Expand All @@ -340,12 +344,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
Expand Down Expand Up @@ -667,7 +672,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,
Expand All @@ -689,7 +698,7 @@ const registerNetworkIpc = (mainWindow) => {
const processEnvVars = getProcessEnvVars(collectionUid);

try {
await runPreRequest(
const preRequestScriptResult = await runPreRequest(
request,
requestUid,
envVars,
Expand All @@ -701,6 +710,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))
Expand Down Expand Up @@ -776,7 +789,7 @@ const registerNetworkIpc = (mainWindow) => {
}
}

await runPostResponse(
const postRequestScriptResult = await runPostResponse(
request,
response,
requestUid,
Expand All @@ -789,6 +802,10 @@ const registerNetworkIpc = (mainWindow) => {
scriptingConfig
);

if (postRequestScriptResult?.nextRequestName !== undefined) {
nextRequestName = postRequestScriptResult.nextRequestName;
}

// run assertions
const assertions = get(item, 'request.assertions');
if (assertions) {
Expand Down Expand Up @@ -849,6 +866,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', {
Expand Down
4 changes: 4 additions & 0 deletions packages/bruno-js/src/bru.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ class Bru {

return this.collectionVariables[key];
}

setNextRequest(nextRequest) {
this.nextRequest = nextRequest;
}
}

module.exports = Bru;
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 @@ -121,7 +121,8 @@ class ScriptRuntime {
return {
request,
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables)
collectionVariables: cleanJson(collectionVariables),
nextRequestName: bru.nextRequest
};
}

Expand Down Expand Up @@ -212,7 +213,8 @@ class ScriptRuntime {
return {
response,
envVariables: cleanJson(envVariables),
collectionVariables: cleanJson(collectionVariables)
collectionVariables: cleanJson(collectionVariables),
nextRequestName: bru.nextRequest
};
}
}
Expand Down