2023-02-04 19:57:18 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
const { indentString } = require('../../v1/src/utils');
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
const enabled = (items = []) => items.filter((item) => item.enabled);
|
|
|
|
const disabled = (items = []) => items.filter((item) => !item.enabled);
|
2023-02-04 19:57:18 +01:00
|
|
|
|
|
|
|
// remove the last line if two new lines are found
|
|
|
|
const stripLastLine = (text) => {
|
2023-09-21 21:12:48 +02:00
|
|
|
if (!text || !text.length) return text;
|
2023-02-04 19:57:18 +01:00
|
|
|
|
|
|
|
return text.replace(/(\r?\n)$/, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
const jsonToBru = (json) => {
|
2023-09-21 21:12:48 +02:00
|
|
|
const { meta, http, query, headers, body, script, tests, vars, assertions, docs } = json;
|
2023-02-04 19:57:18 +01:00
|
|
|
|
|
|
|
let bru = '';
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (meta) {
|
2023-02-04 19:57:18 +01:00
|
|
|
bru += 'meta {\n';
|
|
|
|
for (const key in meta) {
|
|
|
|
bru += ` ${key}: ${meta[key]}\n`;
|
|
|
|
}
|
|
|
|
bru += '}\n\n';
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (http && http.method) {
|
2023-02-04 19:57:18 +01:00
|
|
|
bru += `${http.method} {
|
|
|
|
url: ${http.url}`;
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (http.body && http.body.length) {
|
2023-02-04 19:57:18 +01:00
|
|
|
bru += `
|
2023-02-06 22:20:15 +01:00
|
|
|
body: ${http.body}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bru += `
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (query && query.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += 'query {';
|
2023-09-21 21:12:48 +02:00
|
|
|
if (enabled(query).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
enabled(query)
|
|
|
|
.map((item) => `${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (disabled(query).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
disabled(query)
|
|
|
|
.map((item) => `~${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (headers && headers.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += 'headers {';
|
2023-09-21 21:12:48 +02:00
|
|
|
if (enabled(headers).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
enabled(headers)
|
|
|
|
.map((item) => `${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (disabled(headers).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
disabled(headers)
|
|
|
|
.map((item) => `~${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.json && body.json.length) {
|
|
|
|
bru += `body:json {
|
2023-02-04 19:57:18 +01:00
|
|
|
${indentString(body.json)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.text && body.text.length) {
|
|
|
|
bru += `body:text {
|
2023-02-04 19:57:18 +01:00
|
|
|
${indentString(body.text)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.xml && body.xml.length) {
|
|
|
|
bru += `body:xml {
|
2023-02-04 19:57:18 +01:00
|
|
|
${indentString(body.xml)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.formUrlEncoded && body.formUrlEncoded.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `body:form-urlencoded {`;
|
2023-09-21 21:12:48 +02:00
|
|
|
if (enabled(body.formUrlEncoded).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
enabled(body.formUrlEncoded)
|
|
|
|
.map((item) => `${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (disabled(body.formUrlEncoded).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
disabled(body.formUrlEncoded)
|
|
|
|
.map((item) => `~${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.multipartForm && body.multipartForm.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `body:multipart-form {`;
|
2023-09-21 21:12:48 +02:00
|
|
|
if (enabled(body.multipartForm).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
enabled(body.multipartForm)
|
|
|
|
.map((item) => `${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (disabled(body.multipartForm).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
disabled(body.multipartForm)
|
|
|
|
.map((item) => `~${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.graphql && body.graphql.query) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `body:graphql {\n`;
|
|
|
|
bru += `${indentString(body.graphql.query)}`;
|
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (body && body.graphql && body.graphql.variables) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `body:graphql:vars {\n`;
|
2023-09-21 21:12:48 +02:00
|
|
|
bru += `${indentString(body.graphql.variables)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 16:48:36 +01:00
|
|
|
let reqvars = _.get(vars, 'req');
|
|
|
|
let resvars = _.get(vars, 'res');
|
2023-09-21 21:12:48 +02:00
|
|
|
if (reqvars && reqvars.length) {
|
2023-02-06 16:48:36 +01:00
|
|
|
const varsEnabled = _.filter(reqvars, (v) => v.enabled && !v.local);
|
|
|
|
const varsDisabled = _.filter(reqvars, (v) => !v.enabled && !v.local);
|
|
|
|
const varsLocalEnabled = _.filter(reqvars, (v) => v.enabled && v.local);
|
|
|
|
const varsLocalDisabled = _.filter(reqvars, (v) => !v.enabled && v.local);
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-07 00:43:14 +01:00
|
|
|
bru += `vars:pre-request {`;
|
2023-02-06 16:48:36 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsEnabled.length) {
|
|
|
|
bru += `\n${indentString(varsEnabled.map((item) => `${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-06 16:48:36 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsLocalEnabled.length) {
|
|
|
|
bru += `\n${indentString(varsLocalEnabled.map((item) => `@${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-06 16:48:36 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsDisabled.length) {
|
|
|
|
bru += `\n${indentString(varsDisabled.map((item) => `~${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-06 16:48:36 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsLocalDisabled.length) {
|
|
|
|
bru += `\n${indentString(varsLocalDisabled.map((item) => `~@${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-06 16:48:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bru += '\n}\n\n';
|
|
|
|
}
|
2023-09-21 21:12:48 +02:00
|
|
|
if (resvars && resvars.length) {
|
2023-02-06 16:48:36 +01:00
|
|
|
const varsEnabled = _.filter(resvars, (v) => v.enabled && !v.local);
|
|
|
|
const varsDisabled = _.filter(resvars, (v) => !v.enabled && !v.local);
|
|
|
|
const varsLocalEnabled = _.filter(resvars, (v) => v.enabled && v.local);
|
|
|
|
const varsLocalDisabled = _.filter(resvars, (v) => !v.enabled && v.local);
|
|
|
|
|
2023-02-07 00:43:14 +01:00
|
|
|
bru += `vars:post-response {`;
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsEnabled.length) {
|
|
|
|
bru += `\n${indentString(varsEnabled.map((item) => `${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsLocalEnabled.length) {
|
|
|
|
bru += `\n${indentString(varsLocalEnabled.map((item) => `@${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsDisabled.length) {
|
|
|
|
bru += `\n${indentString(varsDisabled.map((item) => `~${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (varsLocalDisabled.length) {
|
|
|
|
bru += `\n${indentString(varsLocalDisabled.map((item) => `~@${item.name}: ${item.value}`).join('\n'))}`;
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
2023-02-05 18:43:18 +01:00
|
|
|
|
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (assertions && assertions.length) {
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += `assert {`;
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (enabled(assertions).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
enabled(assertions)
|
|
|
|
.map((item) => `${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (disabled(assertions).length) {
|
|
|
|
bru += `\n${indentString(
|
|
|
|
disabled(assertions)
|
|
|
|
.map((item) => `~${item.name}: ${item.value}`)
|
|
|
|
.join('\n')
|
|
|
|
)}`;
|
2023-02-05 18:43:18 +01:00
|
|
|
}
|
2023-02-04 19:57:18 +01:00
|
|
|
|
2023-02-05 18:43:18 +01:00
|
|
|
bru += '\n}\n\n';
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (script && script.req && script.req.length) {
|
|
|
|
bru += `script:pre-request {
|
2023-02-06 16:48:36 +01:00
|
|
|
${indentString(script.req)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (script && script.res && script.res.length) {
|
|
|
|
bru += `script:post-response {
|
2023-02-06 16:48:36 +01:00
|
|
|
${indentString(script.res)}
|
2023-02-04 19:57:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (tests && tests.length) {
|
|
|
|
bru += `tests {
|
2023-02-04 19:57:18 +01:00
|
|
|
${indentString(tests)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
if (docs && docs.length) {
|
|
|
|
bru += `docs {
|
2023-02-04 19:57:18 +01:00
|
|
|
${indentString(docs)}
|
|
|
|
}
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stripLastLine(bru);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = jsonToBru;
|