mirror of
https://github.com/usebruno/bruno.git
synced 2025-02-18 10:41:09 +01:00
Adds variable interpolate to assert runtime
This commit is contained in:
parent
b28f7625e4
commit
544edfa7d7
55
packages/bruno-js/src/interpolate-string.js
Normal file
55
packages/bruno-js/src/interpolate-string.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
const Handlebars = require('handlebars');
|
||||||
|
const { forOwn, cloneDeep } = require('lodash');
|
||||||
|
|
||||||
|
const interpolateEnvVars = (str, processEnvVars) => {
|
||||||
|
if (!str || !str.length || typeof str !== 'string') {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
const template = Handlebars.compile(str, { noEscape: true });
|
||||||
|
|
||||||
|
return template({
|
||||||
|
process: {
|
||||||
|
env: {
|
||||||
|
...processEnvVars
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const interpolateString = (str, { envVars, collectionVariables, processEnvVars }) => {
|
||||||
|
if (!str || !str.length || typeof str !== 'string') {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
processEnvVars = processEnvVars || {};
|
||||||
|
collectionVariables = collectionVariables || {};
|
||||||
|
|
||||||
|
// we clone envVars because we don't want to modify the original object
|
||||||
|
envVars = envVars ? cloneDeep(envVars) : {};
|
||||||
|
|
||||||
|
// envVars can inturn have values as {{process.env.VAR_NAME}}
|
||||||
|
// so we need to interpolate envVars first with processEnvVars
|
||||||
|
forOwn(envVars, (value, key) => {
|
||||||
|
envVars[key] = interpolateEnvVars(value, processEnvVars);
|
||||||
|
});
|
||||||
|
|
||||||
|
const template = Handlebars.compile(str, { noEscape: true });
|
||||||
|
|
||||||
|
// collectionVariables take precedence over envVars
|
||||||
|
const combinedVars = {
|
||||||
|
...envVars,
|
||||||
|
...collectionVariables,
|
||||||
|
process: {
|
||||||
|
env: {
|
||||||
|
...processEnvVars
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return template(combinedVars);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
interpolateString
|
||||||
|
};
|
@ -4,6 +4,7 @@ const { nanoid } = require('nanoid');
|
|||||||
const Bru = require('../bru');
|
const Bru = require('../bru');
|
||||||
const BrunoRequest = require('../bruno-request');
|
const BrunoRequest = require('../bruno-request');
|
||||||
const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
|
const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
|
||||||
|
const { interpolateString } = require('../interpolate-string');
|
||||||
|
|
||||||
const { expect } = chai;
|
const { expect } = chai;
|
||||||
chai.use(require('chai-string'));
|
chai.use(require('chai-string'));
|
||||||
@ -167,11 +168,15 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
|
|||||||
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
|
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rhsOperand.split(',').map((v) => evaluateJsTemplateLiteral(v.trim(), context));
|
return rhsOperand
|
||||||
|
.split(',')
|
||||||
|
.map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), context.bru), context));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operator === 'between') {
|
if (operator === 'between') {
|
||||||
const [lhs, rhs] = rhsOperand.split(',').map((v) => evaluateJsTemplateLiteral(v.trim(), context));
|
const [lhs, rhs] = rhsOperand
|
||||||
|
.split(',')
|
||||||
|
.map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), context.bru), context));
|
||||||
return [lhs, rhs];
|
return [lhs, rhs];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,10 +186,10 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
|
|||||||
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
|
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return rhsOperand;
|
return interpolateString(rhsOperand, context.bru);
|
||||||
}
|
}
|
||||||
|
|
||||||
return evaluateJsTemplateLiteral(rhsOperand, context);
|
return evaluateJsTemplateLiteral(interpolateString(rhsOperand, context.bru), context);
|
||||||
};
|
};
|
||||||
|
|
||||||
class AssertRuntime {
|
class AssertRuntime {
|
||||||
|
Loading…
Reference in New Issue
Block a user