feat: bru lang - support disabled headers parsing

This commit is contained in:
Anoop M D 2023-02-03 23:27:06 +05:30
parent 118ceacf46
commit 7a8e5198ff
3 changed files with 54 additions and 11 deletions

View File

@ -2,15 +2,17 @@ const ohm = require("ohm-js");
const _ = require('lodash');
const grammar = ohm.grammar(`Bru {
BruFile = (script | test | headers)*
BruFile = (script | test | headersdisabled | headers)*
nl = "\\r"? "\\n"
st = " " | "\\t"
tagend = nl "}"
validkey = ~(st | ":") any
validvalue = ~nl any
headers = "headers" st* "{" pairlist? tagend
headers = "headers" pairblock
headersdisabled = "headers:disabled" pairblock
pairblock = st* "{" pairlist? tagend
pairlist = nl* pair (~tagend nl pair)* (~tagend space)*
pair = st* key st* ":" st* value? st*
key = ~tagend validkey*
@ -38,6 +40,12 @@ const mapPairListToKeyValPairs = (pairList = [], enabled = true) => {
});
};
const concatArrays = (objValue, srcValue) => {
if (_.isArray(objValue) && _.isArray(srcValue)) {
return objValue.concat(srcValue);
}
};
const sem = grammar.createSemantics().addAttribute('ast', {
BruFile(tags) {
if(!tags || !tags.ast || !tags.ast.length) {
@ -45,14 +53,22 @@ const sem = grammar.createSemantics().addAttribute('ast', {
}
return _.reduce(tags.ast, (result, item) => {
return _.assign(result, item);
return _.mergeWith(result, item, concatArrays);
}, {});
},
headers(_1, _2, _3, pairlist, _4) {
headers(_1, pairblock) {
return {
headers: mapPairListToKeyValPairs(pairlist.ast)
headers: mapPairListToKeyValPairs(pairblock.ast)
};
},
headersdisabled(_1, pairblock) {
return {
headers: mapPairListToKeyValPairs(pairblock.ast, false)
};
},
pairblock(_1, _2, pairlist, _3) {
return pairlist.ast;
},
pairlist(_1, pair, _2, rest, _3) {
return [pair.ast, ...rest.ast];
},

View File

@ -95,6 +95,23 @@ headers {
expect(output).toEqual(expected);
});
it("should parse disabled headers", () => {
const input = `
headers:disabled {
content-type: application/json
}`;
const output = bruToJsonV2(input);
const expected = {
"headers": [{
"name": "content-type",
"value": "application/json",
"enabled": false
}]
};
expect(output).toEqual(expected);
});
it("should throw error on invalid header", () => {
const input = `
headers {

View File

@ -4,8 +4,12 @@ describe("parser", () => {
it("should parse the bru file", () => {
const input = `
headers {
hello: world
foo: bar
content-type: application/json
Authorization: Bearer 123
}
headers:disabled {
transaction-id: {{transactionId}}
}
script {
@ -19,18 +23,24 @@ script {
const expected = {
"headers": [
{
"name": "hello",
"value": "world",
"name": "content-type",
"value": "application/json",
"enabled": true
},
{
"name": "foo",
"value": "bar",
"name": "Authorization",
"value": "Bearer 123",
"enabled": true
},
{
"name": "transaction-id",
"value": "{{transactionId}}",
"enabled": false
}
],
"script": " function onResponse(request, response) {\n expect(response.status).to.equal(200);\n }"
}
expect(output).toEqual(expected);
});
});