From 77681ca51edc77fa62e82d26deb78006e011f7a8 Mon Sep 17 00:00:00 2001 From: "Siddharth Gelera (reaper)" Date: Fri, 24 Oct 2025 15:08:10 +0530 Subject: [PATCH] fix: inherit vars and headers from the collection (#5876) * fix: inherit vars and headers from the collection --- eslint.config.js | 1 + .../src/ipc/network/ws-event-handlers.js | 10 ++++++ packages/bruno-tests/src/ws/index.js | 32 +++++++++++-------- .../fixtures/collection/collection.bru | 3 ++ .../ws-test-request-with-headers.bru | 1 + tests/websockets/headers.spec.ts | 1 + 6 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 tests/websockets/fixtures/collection/collection.bru diff --git a/eslint.config.js b/eslint.config.js index cdeb10f29..fd712f667 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -37,6 +37,7 @@ module.exports = runESMImports().then(() => defineConfig([ 'packages/bruno-lang/**/*.js', 'packages/bruno-requests/**/*.ts', 'packages/bruno-requests/**/*.js', + 'packages/bruno-tests/**/*.{js,ts}' ], processor: 'diff/diff', rules: { diff --git a/packages/bruno-electron/src/ipc/network/ws-event-handlers.js b/packages/bruno-electron/src/ipc/network/ws-event-handlers.js index c811d99e2..03c8abdcd 100644 --- a/packages/bruno-electron/src/ipc/network/ws-event-handlers.js +++ b/packages/bruno-electron/src/ipc/network/ws-event-handlers.js @@ -27,8 +27,18 @@ const { setAuthHeaders } = require('./prepare-request'); const prepareWsRequest = async (item, collection, environment, runtimeVariables, certsAndProxyConfig = {}) => { const request = item.draft ? item.draft.request : item.request; const collectionRoot = collection?.draft ? get(collection, 'draft', {}) : get(collection, 'root', {}); + const brunoConfig = get(collection, 'brunoConfig', {}); const headers = {}; + const scriptFlow = brunoConfig?.scripts?.flow ?? 'sandwich'; + const requestTreePath = getTreePathFromCollectionToItem(collection, item); + if (requestTreePath && requestTreePath.length > 0) { + mergeHeaders(collection, request, requestTreePath); + mergeScripts(collection, request, requestTreePath, scriptFlow); + mergeVars(collection, request, requestTreePath); + mergeAuth(collection, request, requestTreePath); + } + each(get(collectionRoot, 'request.headers', []), (h) => { if (h.enabled && h.name?.toLowerCase() === 'content-type') { return false; diff --git a/packages/bruno-tests/src/ws/index.js b/packages/bruno-tests/src/ws/index.js index c86f70354..918042b00 100644 --- a/packages/bruno-tests/src/ws/index.js +++ b/packages/bruno-tests/src/ws/index.js @@ -20,20 +20,26 @@ const wss = new ws.Server({ wss.on('connection', function connection(ws, request) { ws.on('message', function message(data) { const msg = Buffer.from(data).toString().trim(); - const obj = JSON.parse(msg); - if ('func' in obj && obj.func === 'headers') { - ws.send( - JSON.stringify({ - headers: request.headers - }) - ); - } else { - ws.send( - JSON.stringify({ - data: JSON.parse(Buffer.from(data).toString()) - }) - ); + let isJSON = false; + let obj = {}; + try { + obj = JSON.parse(msg); + isJSON = true; + } catch (err) { + // Not a json value, don't do any modification } + if (isJSON) { + if ('func' in obj && obj.func === 'headers') { + return ws.send(JSON.stringify({ + headers: request.headers + })); + } else { + return ws.send(JSON.stringify({ + data: JSON.parse(Buffer.from(data).toString()) + })); + } + } + return ws.send(Buffer.from(data).toString()); }); }); diff --git a/tests/websockets/fixtures/collection/collection.bru b/tests/websockets/fixtures/collection/collection.bru new file mode 100644 index 000000000..68dacf870 --- /dev/null +++ b/tests/websockets/fixtures/collection/collection.bru @@ -0,0 +1,3 @@ +vars:pre-request { + variable: Variable Value +} \ No newline at end of file diff --git a/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru b/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru index f181d6479..4c483af76 100644 --- a/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru +++ b/tests/websockets/fixtures/collection/ws-test-request-with-headers.bru @@ -11,6 +11,7 @@ ws { headers { Authorization: Dummy + X-BRUNO-COLLECTION-VAR: {{variable}} } body:ws { diff --git a/tests/websockets/headers.spec.ts b/tests/websockets/headers.spec.ts index 81f89dcd0..457fb1013 100644 --- a/tests/websockets/headers.spec.ts +++ b/tests/websockets/headers.spec.ts @@ -16,5 +16,6 @@ test.describe.serial('headers', () => { // Check if the message has the authorisation header await expect(locators.messages().nth(2).locator('.text-ellipsis')).toHaveText(/\"(authorization)\"\:\s+\"Dummy\"/); + await expect(locators.messages().nth(2).locator('.text-ellipsis')).toHaveText(/\"(x-bruno-collection-var)\"\:\s+\"Variable Value\"/); }); });