feat: script and vars are segmented at req and res levels separately

This commit is contained in:
Anoop M D 2023-02-06 21:18:36 +05:30
parent c8de57aa51
commit 60c96f7d27
4 changed files with 136 additions and 46 deletions

View File

@ -62,9 +62,9 @@ const grammar = ohm.grammar(`Bru {
query = "query" dictionary
varsandassert = vars | varslocal | assert
vars = "vars" dictionary
varslocal = "vars:local" dictionary
varsandassert = varsreq | varsres | assert
varsreq = "vars:req" dictionary
varsres = "vars:res" dictionary
assert = "assert" dictionary
body = "body" st* "{" nl* textblock tagend
@ -77,7 +77,9 @@ const grammar = ohm.grammar(`Bru {
bodyformurlencoded = "body:form-urlencoded" dictionary
bodymultipart = "body:multipart-form" dictionary
script = "script" st* "{" nl* textblock tagend
script = scriptreq | scriptres
scriptreq = "script:req" st* "{" nl* textblock tagend
scriptres = "script:res" st* "{" nl* textblock tagend
tests = "tests" st* "{" nl* textblock tagend
docs = "docs" st* "{" nl* textblock tagend
}`);
@ -309,7 +311,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
}
};
},
vars(_1, dictionary) {
varsreq(_1, dictionary) {
const vars = mapPairListToKeyValPairs(dictionary.ast);
_.each(vars, (v) => {
let name = v.name;
@ -322,7 +324,27 @@ const sem = grammar.createSemantics().addAttribute('ast', {
});
return {
vars
vars: {
req: vars
}
};
},
varsres(_1, dictionary) {
const vars = mapPairListToKeyValPairs(dictionary.ast);
_.each(vars, (v) => {
let name = v.name;
if (name && name.length && name.charAt(0) === "@") {
v.name = name.slice(1);
v.local = true;
} else {
v.local = false;
}
});
return {
vars: {
res: vars
}
};
},
assert(_1, dictionary) {
@ -330,9 +352,18 @@ const sem = grammar.createSemantics().addAttribute('ast', {
assert: mapPairListToKeyValPairs(dictionary.ast)
};
},
script(_1, _2, _3, _4, textblock, _5) {
scriptreq(_1, _2, _3, _4, textblock, _5) {
return {
script: outdentString(textblock.sourceString)
script: {
req: outdentString(textblock.sourceString)
}
};
},
scriptres(_1, _2, _3, _4, textblock, _5) {
return {
script: {
res: outdentString(textblock.sourceString)
}
};
},
tests(_1, _2, _3, _4, textblock, _5) {

View File

@ -141,13 +141,41 @@ ${indentString(body.xml)}
bru += '\n}\n\n';
}
if(vars && vars.length) {
const varsEnabled = _.filter(vars, (v) => v.enabled && !v.local);
const varsDisabled = _.filter(vars, (v) => !v.enabled && !v.local);
const varsLocalEnabled = _.filter(vars, (v) => v.enabled && v.local);
const varsLocalDisabled = _.filter(vars, (v) => !v.enabled && v.local);
let reqvars = _.get(vars, 'req');
let resvars = _.get(vars, 'res');
if(reqvars && reqvars.length) {
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);
bru += `vars {`;
bru += `vars:req {`;
if(varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalEnabled.length) {
bru += `\n${indentString(varsLocalEnabled.map(item => `@${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsDisabled.length) {
bru += `\n${indentString(varsDisabled.map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalDisabled.length) {
bru += `\n${indentString(varsLocalDisabled.map(item => `~@${item.name}: ${item.value}`).join('\n'))}`;
}
bru += '\n}\n\n';
}
if(resvars && resvars.length) {
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);
bru += `vars:res {`;
if(varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
@ -182,9 +210,17 @@ ${indentString(body.xml)}
bru += '\n}\n\n';
}
if(script && script.length) {
bru += `script {
${indentString(script)}
if(script && script.req && script.req.length) {
bru += `script:req {
${indentString(script.req)}
}
`;
}
if(script && script.res && script.res.length) {
bru += `script:res {
${indentString(script.res)}
}
`;

View File

@ -67,7 +67,12 @@ body:graphql:vars {
}
}
vars {
vars:req {
departingDate: 2020-01-01
~returningDate: 2020-01-02
}
vars:res {
token: $res.body.token
@orderNumber: $res.body.orderNumber
~petId: $res.body.id
@ -79,7 +84,7 @@ assert {
~$res.body.message: success
}
script {
script:req {
const foo = 'bar';
}

View File

@ -82,32 +82,48 @@
}
]
},
"vars": [
{
"name": "token",
"value": "$res.body.token",
"local": false,
"enabled": true
},
{
"name": "orderNumber",
"value": "$res.body.orderNumber",
"local": true,
"enabled": true
},
{
"name": "petId",
"value": "$res.body.id",
"local": false,
"enabled": false
},
{
"name": "transactionId",
"value": "$res.body.transactionId",
"local": true,
"enabled": false
}
],
"vars": {
"req": [
{
"name": "departingDate",
"value": "2020-01-01",
"local": false,
"enabled": true
},
{
"name": "returningDate",
"value": "2020-01-02",
"local": false,
"enabled": false
}
],
"res": [
{
"name": "token",
"value": "$res.body.token",
"local": false,
"enabled": true
},
{
"name": "orderNumber",
"value": "$res.body.orderNumber",
"local": true,
"enabled": true
},
{
"name": "petId",
"value": "$res.body.id",
"local": false,
"enabled": false
},
{
"name": "transactionId",
"value": "$res.body.transactionId",
"local": true,
"enabled": false
}
]
},
"assert": [
{
"name": "$res.status",
@ -120,7 +136,9 @@
"enabled": false
}
],
"script": "const foo = 'bar';",
"script": {
"req": "const foo = 'bar';"
},
"tests": "function onResponse(request, response) {\n expect(response.status).to.equal(200);\n}",
"docs": "This request needs auth token to be set in the headers."
}