From 4eed999db1b89bec56e6449761456b258a53708c Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 15 Jan 2023 22:42:56 +0530 Subject: [PATCH] feat: bruno lang now supports parsing for url encoded params --- packages/bruno-lang/src/body-tag.js | 51 ++++++++++++++++++- packages/bruno-lang/src/headers-tag.js | 1 - packages/bruno-lang/src/index.js | 4 +- .../bruno-lang/tests/fixtures/request.bru | 4 ++ packages/bruno-lang/tests/parser.spec.js | 14 ++++- 5 files changed, 70 insertions(+), 4 deletions(-) diff --git a/packages/bruno-lang/src/body-tag.js b/packages/bruno-lang/src/body-tag.js index a20f6fe2..1ad390b5 100644 --- a/packages/bruno-lang/src/body-tag.js +++ b/packages/bruno-lang/src/body-tag.js @@ -2,6 +2,14 @@ const { between, regex, everyCharUntil, + digit, + whitespace, + optionalWhitespace, + endOfInput, + choice, + many, + sepBy, + sequenceOf } = require("arcsecond"); const { safeParseJson } = require('./utils'); @@ -18,6 +26,9 @@ const bodyTextBegin = regex(/^body\s*\(\s*type\s*=\s*text\s*\)\s*\r?\n/); // body(type=xml) const bodyXmlBegin = regex(/^body\s*\(\s*type\s*=\s*xml\s*\)\s*\r?\n/); +// body(type=form-url-encoded) +const bodyFormUrlEncoded = regex(/^body\s*\(\s*type\s*=\s*form-url-encoded\s*\)\s*\r?\n/); + const bodyEnd = regex(/^[\r?\n]+\/body\s*[\r?\n]*/); const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyJson) => { @@ -73,9 +84,47 @@ const bodyXmlTag = between(bodyXmlBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map(( } }); +// generic key value parser +const newline = regex(/^\r?\n/); +const newLineOrEndOfInput = choice([newline, endOfInput]); +const word = regex(/^[^\s\t\n]+/g); + +const line = sequenceOf([ + optionalWhitespace, + digit, + whitespace, + word, + whitespace, + word, + newLineOrEndOfInput +]).map(([_, enabled, __, key, ___, value]) => { + return { + "enabled": enabled, + "key": key, + "value": value + }; +}); + +const lines = many(line); +const keyvalLines = sepBy(newline)(lines); + +// this regex allows the body end tag to start without a newline +// currently the line parser consumes the last newline +// todo: fix this +const bodyEndRelaxed = regex(/^[\r?\n]*\/body\s*[\r?\n]*/); + +const bodyFormUrlEncodedTag = between(bodyFormUrlEncoded)(bodyEndRelaxed)(keyvalLines).map(([result]) => { + return { + body: { + formUrlEncoded: result + } + } +}); + module.exports = { bodyJsonTag, bodyGraphqlTag, bodyTextTag, - bodyXmlTag + bodyXmlTag, + bodyFormUrlEncodedTag }; diff --git a/packages/bruno-lang/src/headers-tag.js b/packages/bruno-lang/src/headers-tag.js index e371f152..0dd7a617 100644 --- a/packages/bruno-lang/src/headers-tag.js +++ b/packages/bruno-lang/src/headers-tag.js @@ -15,7 +15,6 @@ const { const newline = regex(/^\r?\n/); const newLineOrEndOfInput = choice([newline, endOfInput]); - const begin = regex(/^headers\s*\r?\n/); const end = regex(/^[\r?\n]*\/headers\s*[\r?\n]*/); const word = regex(/^[^\s\t\n]+/g); diff --git a/packages/bruno-lang/src/index.js b/packages/bruno-lang/src/index.js index b1a7eb39..7568fd7d 100644 --- a/packages/bruno-lang/src/index.js +++ b/packages/bruno-lang/src/index.js @@ -12,7 +12,8 @@ const { bodyJsonTag, bodyGraphqlTag, bodyTextTag, - bodyXmlTag + bodyXmlTag, + bodyFormUrlEncodedTag } = require('./body-tag'); const bruToJson = (fileContents) => { @@ -24,6 +25,7 @@ const bruToJson = (fileContents) => { bodyGraphqlTag, bodyTextTag, bodyXmlTag, + bodyFormUrlEncodedTag, anyChar ])); diff --git a/packages/bruno-lang/tests/fixtures/request.bru b/packages/bruno-lang/tests/fixtures/request.bru index f7294a21..510c6a59 100644 --- a/packages/bruno-lang/tests/fixtures/request.bru +++ b/packages/bruno-lang/tests/fixtures/request.bru @@ -40,3 +40,7 @@ body(type=xml) back to the ice age /body +body(type=form-url-encoded) + 1 username john + 0 password {{password}} +/body diff --git a/packages/bruno-lang/tests/parser.spec.js b/packages/bruno-lang/tests/parser.spec.js index 54d61884..343afb71 100644 --- a/packages/bruno-lang/tests/parser.spec.js +++ b/packages/bruno-lang/tests/parser.spec.js @@ -56,7 +56,19 @@ describe('bruToJson', () => { "query": " {\n launchesPast {\n launch_success\n }\n }" }, "text": " Hello, there. You must be from the past", - "xml": " back to the ice age" + "xml": " back to the ice age", + "formUrlEncoded": [ + { + "enabled": "1", + "key": "username", + "value": "john" + }, + { + "enabled": "0", + "key": "password", + "value": "{{password}}" + } + ] } }); });