fix: inherit vars and headers from the collection (#5876)

* fix: inherit vars and headers from the collection
This commit is contained in:
Siddharth Gelera (reaper)
2025-10-24 15:08:10 +05:30
committed by GitHub
parent c997b91698
commit 77681ca51e
6 changed files with 35 additions and 13 deletions

View File

@@ -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: {

View File

@@ -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;

View File

@@ -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());
});
});

View File

@@ -0,0 +1,3 @@
vars:pre-request {
variable: Variable Value
}

View File

@@ -11,6 +11,7 @@ ws {
headers {
Authorization: Dummy
X-BRUNO-COLLECTION-VAR: {{variable}}
}
body:ws {

View File

@@ -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\"/);
});
});