diff --git a/packages/bruno-electron/package.json b/packages/bruno-electron/package.json index 542f663f9..f1a992e47 100644 --- a/packages/bruno-electron/package.json +++ b/packages/bruno-electron/package.json @@ -18,6 +18,9 @@ "pack": "electron-builder --dir", "test": "jest" }, + "jest": { + "modulePaths": ["node_modules"] + }, "dependencies": { "@aws-sdk/credential-providers": "3.525.0", "@usebruno/common": "0.1.0", diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 49144d479..c384ba71e 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -408,10 +408,13 @@ const registerNetworkIpc = (mainWindow) => { } // run post-response script + const responseScript = compact(scriptingConfig.flow === 'natural' ? [ + get(collectionRoot, 'request.script.res'), get(request, 'script.res') + ] : [ + get(request, 'script.res'), get(collectionRoot, 'request.script.res') + ]).join(os.EOL); + let scriptResult; - const responseScript = compact([get(request, 'script.res'), get(collectionRoot, 'request.script.res')]).join( - os.EOL - ); if (responseScript?.length) { const scriptRuntime = new ScriptRuntime(); scriptResult = await scriptRuntime.runResponseScript( @@ -592,10 +595,13 @@ const registerNetworkIpc = (mainWindow) => { } // run tests - const testFile = compact([ - item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'), - get(collectionRoot, 'request.tests') + const testScript = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); + const testFile = compact(scriptingConfig.flow === 'natural' ? [ + get(collectionRoot, 'request.tests'), testScript, + ] : [ + testScript, get(collectionRoot, 'request.tests') ]).join(os.EOL); + if (typeof testFile === 'string') { const testRuntime = new TestRuntime(); const testResults = await testRuntime.runTests( @@ -1029,10 +1035,13 @@ const registerNetworkIpc = (mainWindow) => { } // run tests - const testFile = compact([ - item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'), - get(collectionRoot, 'request.tests') + const testScript = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests'); + const testFile = compact(scriptingConfig.flow === 'natural' ? [ + get(collectionRoot, 'request.tests'), testScript + ] : [ + testScript, get(collectionRoot, 'request.tests') ]).join(os.EOL); + if (typeof testFile === 'string') { const testRuntime = new TestRuntime(); const testResults = await testRuntime.runTests( diff --git a/packages/bruno-electron/src/ipc/network/oauth2-helper.js b/packages/bruno-electron/src/ipc/network/oauth2-helper.js index 33b845e59..216c3be97 100644 --- a/packages/bruno-electron/src/ipc/network/oauth2-helper.js +++ b/packages/bruno-electron/src/ipc/network/oauth2-helper.js @@ -51,26 +51,26 @@ const getOAuth2AuthorizationCode = (request, codeChallenge, collectionUid) => { const { oauth2 } = request; const { callbackUrl, clientId, authorizationUrl, scope, state, pkce } = oauth2; - let oauth2QueryParams = - (authorizationUrl.indexOf('?') > -1 ? '&' : '?') + `client_id=${clientId}&response_type=code`; + const authorizationUrlWithQueryParams = new URL(authorizationUrl); + authorizationUrlWithQueryParams.searchParams.append('response_type', 'code'); + authorizationUrlWithQueryParams.searchParams.append('client_id', clientId); if (callbackUrl) { - oauth2QueryParams += `&redirect_uri=${callbackUrl}`; + authorizationUrlWithQueryParams.searchParams.append('redirect_uri', callbackUrl); } if (scope) { - oauth2QueryParams += `&scope=${scope}`; + authorizationUrlWithQueryParams.searchParams.append('scope', scope); } if (pkce) { - oauth2QueryParams += `&code_challenge=${codeChallenge}&code_challenge_method=S256`; + authorizationUrlWithQueryParams.searchParams.append('code_challenge', codeChallenge); + authorizationUrlWithQueryParams.searchParams.append('code_challenge_method', 'S256'); } if (state) { - oauth2QueryParams += `&state=${state}`; + authorizationUrlWithQueryParams.searchParams.append('state', state); } - - const authorizationUrlWithQueryParams = authorizationUrl + oauth2QueryParams; try { const oauth2Store = new Oauth2Store(); const { authorizationCode } = await authorizeUserInWindow({ - authorizeUrl: authorizationUrlWithQueryParams, + authorizeUrl: authorizationUrlWithQueryParams.toString(), callbackUrl, session: oauth2Store.getSessionIdOfCollection(collectionUid) }); diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index e2e4c3233..19d40a3e8 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -119,7 +119,7 @@ const mergeFolderLevelVars = (request, requestTreePath) => { })); }; -const mergeFolderLevelScripts = (request, requestTreePath) => { +const mergeFolderLevelScripts = (request, requestTreePath, scriptFlow) => { let folderCombinedPreReqScript = []; let folderCombinedPostResScript = []; let folderCombinedTests = []; @@ -147,11 +147,19 @@ const mergeFolderLevelScripts = (request, requestTreePath) => { } if (folderCombinedPostResScript.length) { - request.script.res = compact([request?.script?.res || '', ...folderCombinedPostResScript.reverse()]).join(os.EOL); + if (scriptFlow === 'natural') { + request.script.res = compact([...folderCombinedPostResScript, request?.script?.res || '']).join(os.EOL); + } else { + request.script.res = compact([request?.script?.res || '', ...folderCombinedPostResScript.reverse()]).join(os.EOL); + } } if (folderCombinedTests.length) { - request.tests = compact([request?.tests || '', ...folderCombinedTests.reverse()]).join(os.EOL); + if (scriptFlow === 'natural') { + request.tests = compact([...folderCombinedTests, request?.tests || '']).join(os.EOL); + } else { + request.tests = compact([request?.tests || '', ...folderCombinedTests.reverse()]).join(os.EOL); + } } }; @@ -301,10 +309,12 @@ const prepareRequest = (item, collection) => { } }); + // scriptFlow is either "sandwich" or "natural" + const scriptFlow = collection.brunoConfig?.scripts?.flow ?? 'sandwich'; const requestTreePath = getTreePathFromCollectionToItem(collection, item); if (requestTreePath && requestTreePath.length > 0) { mergeFolderLevelHeaders(request, requestTreePath); - mergeFolderLevelScripts(request, requestTreePath); + mergeFolderLevelScripts(request, requestTreePath, scriptFlow); mergeFolderLevelVars(request, requestTreePath); } diff --git a/packages/bruno-electron/tests/network/index.spec.js b/packages/bruno-electron/tests/network/index.spec.js index 7c45c2538..02a9b9083 100644 --- a/packages/bruno-electron/tests/network/index.spec.js +++ b/packages/bruno-electron/tests/network/index.spec.js @@ -1,25 +1,15 @@ -// damn jest throws an error when no tests are found in a file -// --passWithNoTests doesn't work +const { configureRequest } = require('../../src/ipc/network/index'); -describe('dummy test', () => { - it('should pass', () => { - expect(true).toBe(true); +describe('index: configureRequest', () => { + it("Should add 'http://' to the URL if no protocol is specified", async () => { + const request = { method: 'GET', url: 'test-domain', body: {} }; + await configureRequest(null, request, null, null, null, null); + expect(request.url).toEqual('http://test-domain'); + }); + + it("Should NOT add 'http://' to the URL if a protocol is specified", async () => { + const request = { method: 'GET', url: 'ftp://test-domain', body: {} }; + await configureRequest(null, request, null, null, null, null); + expect(request.url).toEqual('ftp://test-domain'); }); }); - -// todo: fix this failing test -// const { configureRequest } = require('../../src/ipc/network/index'); - -// describe('index: configureRequest', () => { -// it("Should add 'http://' to the URL if no protocol is specified", async () => { -// const request = { method: 'GET', url: 'test-domain', body: {} }; -// await configureRequest(null, request, null, null, null, null); -// expect(request.url).toEqual('http://test-domain'); -// }); - -// it("Should NOT add 'http://' to the URL if a protocol is specified", async () => { -// const request = { method: 'GET', url: 'ftp://test-domain', body: {} }; -// await configureRequest(null, request, null, null, null, null); -// expect(request.url).toEqual('ftp://test-domain'); -// }); -// });