forked from extern/bruno
feat: bru lang - support disabled headers parsing
This commit is contained in:
parent
118ceacf46
commit
7a8e5198ff
@ -2,15 +2,17 @@ const ohm = require("ohm-js");
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
|
||||||
const grammar = ohm.grammar(`Bru {
|
const grammar = ohm.grammar(`Bru {
|
||||||
BruFile = (script | test | headers)*
|
BruFile = (script | test | headersdisabled | headers)*
|
||||||
nl = "\\r"? "\\n"
|
nl = "\\r"? "\\n"
|
||||||
st = " " | "\\t"
|
st = " " | "\\t"
|
||||||
tagend = nl "}"
|
tagend = nl "}"
|
||||||
validkey = ~(st | ":") any
|
validkey = ~(st | ":") any
|
||||||
validvalue = ~nl 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)*
|
pairlist = nl* pair (~tagend nl pair)* (~tagend space)*
|
||||||
pair = st* key st* ":" st* value? st*
|
pair = st* key st* ":" st* value? st*
|
||||||
key = ~tagend validkey*
|
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', {
|
const sem = grammar.createSemantics().addAttribute('ast', {
|
||||||
BruFile(tags) {
|
BruFile(tags) {
|
||||||
if(!tags || !tags.ast || !tags.ast.length) {
|
if(!tags || !tags.ast || !tags.ast.length) {
|
||||||
@ -45,14 +53,22 @@ const sem = grammar.createSemantics().addAttribute('ast', {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return _.reduce(tags.ast, (result, item) => {
|
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 {
|
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) {
|
pairlist(_1, pair, _2, rest, _3) {
|
||||||
return [pair.ast, ...rest.ast];
|
return [pair.ast, ...rest.ast];
|
||||||
},
|
},
|
||||||
|
@ -95,6 +95,23 @@ headers {
|
|||||||
expect(output).toEqual(expected);
|
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", () => {
|
it("should throw error on invalid header", () => {
|
||||||
const input = `
|
const input = `
|
||||||
headers {
|
headers {
|
||||||
|
@ -4,8 +4,12 @@ describe("parser", () => {
|
|||||||
it("should parse the bru file", () => {
|
it("should parse the bru file", () => {
|
||||||
const input = `
|
const input = `
|
||||||
headers {
|
headers {
|
||||||
hello: world
|
content-type: application/json
|
||||||
foo: bar
|
Authorization: Bearer 123
|
||||||
|
}
|
||||||
|
|
||||||
|
headers:disabled {
|
||||||
|
transaction-id: {{transactionId}}
|
||||||
}
|
}
|
||||||
|
|
||||||
script {
|
script {
|
||||||
@ -19,18 +23,24 @@ script {
|
|||||||
const expected = {
|
const expected = {
|
||||||
"headers": [
|
"headers": [
|
||||||
{
|
{
|
||||||
"name": "hello",
|
"name": "content-type",
|
||||||
"value": "world",
|
"value": "application/json",
|
||||||
"enabled": true
|
"enabled": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "foo",
|
"name": "Authorization",
|
||||||
"value": "bar",
|
"value": "Bearer 123",
|
||||||
"enabled": true
|
"enabled": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "transaction-id",
|
||||||
|
"value": "{{transactionId}}",
|
||||||
|
"enabled": false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"script": " function onResponse(request, response) {\n expect(response.status).to.equal(200);\n }"
|
"script": " function onResponse(request, response) {\n expect(response.status).to.equal(200);\n }"
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(output).toEqual(expected);
|
expect(output).toEqual(expected);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user