diff --git a/packages/bruno-cli/src/runner/prepare-request.js b/packages/bruno-cli/src/runner/prepare-request.js index bc2b22886..3465f4e47 100644 --- a/packages/bruno-cli/src/runner/prepare-request.js +++ b/packages/bruno-cli/src/runner/prepare-request.js @@ -35,7 +35,7 @@ const prepareRequest = (request, collectionRoot) => { }; const collectionAuth = get(collectionRoot, 'request.auth'); - if (collectionAuth && request.auth.mode === 'inherit') { + if (collectionAuth && request.auth?.mode === 'inherit') { if (collectionAuth.mode === 'basic') { axiosRequest.auth = { username: get(collectionAuth, 'basic.username'), @@ -46,9 +46,22 @@ const prepareRequest = (request, collectionRoot) => { if (collectionAuth.mode === 'bearer') { axiosRequest.headers['Authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`; } + + if (collectionAuth.mode === 'apikey') { + if (collectionAuth.apikey?.placement === 'header') { + axiosRequest.headers[collectionAuth.apikey?.key] = collectionAuth.apikey?.value; + } + + if (collectionAuth.apikey?.placement === 'queryparams') { + if (axiosRequest.url) { + const prefix = (!axiosRequest.url.includes('?')) ? '?' : '&'; + axiosRequest.url += `${prefix}${collectionAuth.apikey.key}=${collectionAuth.apikey.value}`; + } + } + } } - if (request.auth) { + if (request.auth && request.auth.mode !== 'inherit') { if (request.auth.mode === 'basic') { axiosRequest.auth = { username: get(request, 'auth.basic.username'), diff --git a/packages/bruno-cli/tests/runner/prepare-request.spec.js b/packages/bruno-cli/tests/runner/prepare-request.spec.js index 6e2219af8..28e6c13f1 100644 --- a/packages/bruno-cli/tests/runner/prepare-request.spec.js +++ b/packages/bruno-cli/tests/runner/prepare-request.spec.js @@ -18,4 +18,126 @@ describe('prepare-request: prepareRequest', () => { expect(result.data).toEqual(expected); }); }); + + describe('Properly maps inherited auth from collectionRoot', () => { + // Initialize Test Fixtures + let collectionRoot, request; + + beforeEach(() => { + // Reset test fixtures + collectionRoot = { + request: { + auth: {} + } + }; + + request = { + url: 'https://www.usebruno.com', + auth: { + mode: "inherit" + }, + body: { + mode: 'json', + json: '{\n"test": {{someVar}} // comment\n}' + } + }; + }); + + it('If collection auth is apikey in header', () => { + collectionRoot.request.auth = { + mode: "apikey", + apikey: { + key: "x-api-key", + value: "{{apiKey}}", + placement: "header" + } + }; + + const result = prepareRequest(request, collectionRoot); + expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}'); + }); + + + it('If collection auth is apikey in header and request has existing headers', () => { + collectionRoot.request.auth = { + mode: "apikey", + apikey: { + key: "x-api-key", + value: "{{apiKey}}", + placement: "header" + } + }; + + request['headers'] = [{ name: 'Content-Type', value: 'application/json', enabled: true }]; + + const result = prepareRequest(request, collectionRoot); + expect(result.headers).toHaveProperty('Content-Type', 'application/json'); + expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}'); + }); + + it('If collection auth is apikey in query parameters', () => { + collectionRoot.request.auth = { + mode: "apikey", + apikey: { + key: "apiKey", + value: "{{apiKey}}", + placement: "queryparams" + } + }; + + const expected = `${request.url}?${collectionRoot.request.auth.apikey.key}=${collectionRoot.request.auth.apikey.value}`; + const result = prepareRequest(request, collectionRoot); + expect(result.url).toEqual(expected); + }); + + it('If request does not have auth configured', () => { + delete request.auth; + let result; + expect(() => { + result = prepareRequest(request, collectionRoot); + }).not.toThrow(); + expect(result).toBeDefined(); + }); + + it('If collection auth is basic auth', () => { + collectionRoot.request.auth = { + mode: 'basic', + basic: { + username: 'testUser', + password: 'testPass123' + } + }; + + const result = prepareRequest(request, collectionRoot); + const expected = { username: 'testUser', password: 'testPass123' }; + expect(result.auth).toEqual(expected); + }); + + it('If collection auth is bearer token', () => { + collectionRoot.request.auth = { + mode: 'bearer', + bearer: { + token: 'token' + } + }; + + const result = prepareRequest(request, collectionRoot); + expect(result.headers).toHaveProperty('Authorization', 'Bearer token'); + }); + + it('If collection auth is bearer token and request has existing headers', () => { + collectionRoot.request.auth = { + mode: 'bearer', + bearer: { + token: 'token' + } + }; + + request['headers'] = [{ name: 'Content-Type', value: 'application/json', enabled: true }]; + + const result = prepareRequest(request, collectionRoot); + expect(result.headers).toHaveProperty('Authorization', 'Bearer token'); + expect(result.headers).toHaveProperty('Content-Type', 'application/json'); + }); + }); });