From 7a8e5198ffdca500c0b406106c29533996974de0 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Fri, 3 Feb 2023 23:27:06 +0530 Subject: [PATCH] feat: bru lang - support disabled headers parsing --- packages/bruno-lang/v2/src/index.js | 26 ++++++++++++++++---- packages/bruno-lang/v2/tests/headers.spec.js | 17 +++++++++++++ packages/bruno-lang/v2/tests/index.spec.js | 22 ++++++++++++----- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/packages/bruno-lang/v2/src/index.js b/packages/bruno-lang/v2/src/index.js index 577f46e92..3863060d0 100644 --- a/packages/bruno-lang/v2/src/index.js +++ b/packages/bruno-lang/v2/src/index.js @@ -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]; }, diff --git a/packages/bruno-lang/v2/tests/headers.spec.js b/packages/bruno-lang/v2/tests/headers.spec.js index 64c729841..56b0de46a 100644 --- a/packages/bruno-lang/v2/tests/headers.spec.js +++ b/packages/bruno-lang/v2/tests/headers.spec.js @@ -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 { diff --git a/packages/bruno-lang/v2/tests/index.spec.js b/packages/bruno-lang/v2/tests/index.spec.js index 2ad905ed6..d260d8b57 100644 --- a/packages/bruno-lang/v2/tests/index.spec.js +++ b/packages/bruno-lang/v2/tests/index.spec.js @@ -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); }); });