mirror of
https://github.com/usebruno/bruno.git
synced 2024-12-23 15:19:01 +01:00
fix(#1143): Fix PR #971 - Use literal segments only for env/collection variables + Add to CLI (#1154)
* fix(#1143): Fix PR #971 - Add literal-segment notation in string only to variables that are not process env vars * fix(#1143): Fix PR #971 - Add to CLI as well * fix(#1143): Fix PR #971 - Use improved Regex after CR + add test case for escaped vars
This commit is contained in:
parent
dbb5e912eb
commit
7de5bbbdf6
@ -28,6 +28,8 @@ const interpolateEnvVars = (str, processEnvVars) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const varsRegex = /(?<!\\)\{\{(?!process\.env\.\w+)(.*\..*)\}\}/g;
|
||||||
|
|
||||||
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
||||||
// we clone envVars because we don't want to modify the original object
|
// we clone envVars because we don't want to modify the original object
|
||||||
envVars = cloneDeep(envVars);
|
envVars = cloneDeep(envVars);
|
||||||
@ -43,6 +45,10 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (varsRegex.test(str)) {
|
||||||
|
// Handlebars doesn't allow dots as identifiers, so we need to use literal segments
|
||||||
|
str = str.replaceAll(varsRegex, '{{[$1]}}');
|
||||||
|
}
|
||||||
const template = Handlebars.compile(str, { noEscape: true });
|
const template = Handlebars.compile(str, { noEscape: true });
|
||||||
|
|
||||||
// collectionVariables take precedence over envVars
|
// collectionVariables take precedence over envVars
|
||||||
|
@ -28,6 +28,8 @@ const interpolateEnvVars = (str, processEnvVars) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const varsRegex = /(?<!\\)\{\{(?!process\.env\.\w+)(.*\..*)\}\}/g;
|
||||||
|
|
||||||
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
const interpolateVars = (request, envVars = {}, collectionVariables = {}, processEnvVars = {}) => {
|
||||||
// we clone envVars because we don't want to modify the original object
|
// we clone envVars because we don't want to modify the original object
|
||||||
envVars = cloneDeep(envVars);
|
envVars = cloneDeep(envVars);
|
||||||
@ -43,9 +45,11 @@ const interpolateVars = (request, envVars = {}, collectionVariables = {}, proces
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handlebars doesn't allow dots as identifiers, so we need to use literal segments
|
if (varsRegex.test(str)) {
|
||||||
const strLiteralSegment = str.replace('{{', '{{[').replace('}}', ']}}');
|
// Handlebars doesn't allow dots as identifiers, so we need to use literal segments
|
||||||
const template = Handlebars.compile(strLiteralSegment, { noEscape: true });
|
str = str.replaceAll(varsRegex, '{{[$1]}}');
|
||||||
|
}
|
||||||
|
const template = Handlebars.compile(str, { noEscape: true });
|
||||||
|
|
||||||
// collectionVariables take precedence over envVars
|
// collectionVariables take precedence over envVars
|
||||||
const combinedVars = {
|
const combinedVars = {
|
||||||
|
130
packages/bruno-electron/tests/network/interpolate-vars.spec.js
Normal file
130
packages/bruno-electron/tests/network/interpolate-vars.spec.js
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
const interpolateVars = require('../../src/ipc/network/interpolate-vars');
|
||||||
|
|
||||||
|
describe('interpolate-vars: interpolateVars', () => {
|
||||||
|
describe('Interpolates string', () => {
|
||||||
|
describe('With environment variables', () => {
|
||||||
|
it("If there's a var with only alphanumeric characters in its name", async () => {
|
||||||
|
const request = { method: 'GET', url: '{{testUrl1}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { testUrl1: 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("If there's a var with a '.' in its name", async () => {
|
||||||
|
const request = { method: 'GET', url: '{{test.url}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { 'test.url': 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("If there's a var with a '-' in its name", async () => {
|
||||||
|
const request = { method: 'GET', url: '{{test-url}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { 'test-url': 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("If there's a var with a '_' in its name", async () => {
|
||||||
|
const request = { method: 'GET', url: '{{test_url}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { test_url: 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('test.com');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('If there are multiple variables', async () => {
|
||||||
|
const body =
|
||||||
|
'{\n "firstElem": {{body-var-1}},\n "secondElem": [{{body.var.2}}],\n "thirdElem": {\n "fourthElem": {{body_var_3}},\n "{{varAsKey}}": {{valueForKey}} }}';
|
||||||
|
const expectedBody =
|
||||||
|
'{\n "firstElem": Test1,\n "secondElem": [Test2],\n "thirdElem": {\n "fourthElem": Test3,\n "TestKey": TestValueForKey }}';
|
||||||
|
|
||||||
|
const request = { method: 'POST', url: 'test', data: body, headers: { 'content-type': 'json' } };
|
||||||
|
const result = interpolateVars(
|
||||||
|
request,
|
||||||
|
{
|
||||||
|
'body-var-1': 'Test1',
|
||||||
|
'body.var.2': 'Test2',
|
||||||
|
body_var_3: 'Test3',
|
||||||
|
varAsKey: 'TestKey',
|
||||||
|
valueForKey: 'TestValueForKey'
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
expect(result.data).toEqual(expectedBody);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('With process environment variables', () => {
|
||||||
|
/*
|
||||||
|
* It should NOT turn process env vars into literal segments.
|
||||||
|
* Otherwise, Handlebars will try to access the var literally
|
||||||
|
*/
|
||||||
|
it("If there's a var that starts with 'process.env.'", async () => {
|
||||||
|
const request = { method: 'GET', url: '{{process.env.TEST_VAR}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, null, null, { TEST_VAR: 'test.com' });
|
||||||
|
expect(result.url).toEqual('test.com');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Does NOT interpolate string', () => {
|
||||||
|
describe('With environment variables', () => {
|
||||||
|
it('If the var is escaped', async () => {
|
||||||
|
const request = { method: 'GET', url: `\\{{test.url}}` };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { 'test.url': 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('{{test.url}}');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("If it's not a var (no braces)", async () => {
|
||||||
|
const request = { method: 'GET', url: 'test' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { 'test.url': 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('test');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("If it's not a var (only 1 set of braces)", async () => {
|
||||||
|
const request = { method: 'GET', url: '{test.url}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { 'test.url': 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('{test.url}');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("If it's not a var (1 opening & 2 closing braces)", async () => {
|
||||||
|
const request = { method: 'GET', url: '{test.url}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, { 'test.url': 'test.com' }, null, null);
|
||||||
|
expect(result.url).toEqual('{test.url}}');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('If there are no variables (multiple)', async () => {
|
||||||
|
let gqlBody = `{"query":"mutation {\\n test(input: { native: { firstElem: \\"{should-not-get-interpolated}\\", secondElem: \\"{should-not-get-interpolated}}"}}) {\\n __typename\\n ... on TestType {\\n id\\n identifier\\n }\\n }\\n}","variables":"{}"}`;
|
||||||
|
|
||||||
|
const request = { method: 'POST', url: 'test', data: gqlBody };
|
||||||
|
const result = interpolateVars(request, { 'should-not-get-interpolated': 'ERROR' }, null, null);
|
||||||
|
expect(result.data).toEqual(gqlBody);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('With process environment variables', () => {
|
||||||
|
it("If there's a var that doesn't start with 'process.env.'", async () => {
|
||||||
|
const request = { method: 'GET', url: '{{process-env-TEST_VAR}}' };
|
||||||
|
|
||||||
|
const result = interpolateVars(request, null, null, { TEST_VAR: 'test.com' });
|
||||||
|
expect(result.url).toEqual('');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Throws an error', () => {
|
||||||
|
it("If there's a var with an invalid character in its name", async () => {
|
||||||
|
'!@#%^&*()[{]}=+,<>;\\|'.split('').forEach((character) => {
|
||||||
|
const request = { method: 'GET', url: `{{test${character}Url}}` };
|
||||||
|
expect(() => interpolateVars(request, { [`test${character}Url`]: 'test.com' }, null, null)).toThrow(
|
||||||
|
/Parse error.*/
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user