mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-24 13:15:40 +00:00
fix: inherit vars and headers from the collection (#5876)
* fix: inherit vars and headers from the collection
This commit is contained in:
committed by
GitHub
parent
c997b91698
commit
77681ca51e
@@ -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: {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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());
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
3
tests/websockets/fixtures/collection/collection.bru
Normal file
3
tests/websockets/fixtures/collection/collection.bru
Normal file
@@ -0,0 +1,3 @@
|
||||
vars:pre-request {
|
||||
variable: Variable Value
|
||||
}
|
||||
@@ -11,6 +11,7 @@ ws {
|
||||
|
||||
headers {
|
||||
Authorization: Dummy
|
||||
X-BRUNO-COLLECTION-VAR: {{variable}}
|
||||
}
|
||||
|
||||
body:ws {
|
||||
|
||||
@@ -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\"/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user