feat: bruno lang now supports parsing for url encoded params

This commit is contained in:
Anoop M D 2023-01-15 22:42:56 +05:30
parent c29ab50a3d
commit 4eed999db1
5 changed files with 70 additions and 4 deletions

View File

@ -2,6 +2,14 @@ const {
between, between,
regex, regex,
everyCharUntil, everyCharUntil,
digit,
whitespace,
optionalWhitespace,
endOfInput,
choice,
many,
sepBy,
sequenceOf
} = require("arcsecond"); } = require("arcsecond");
const { safeParseJson } = require('./utils'); 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) // body(type=xml)
const bodyXmlBegin = regex(/^body\s*\(\s*type\s*=\s*xml\s*\)\s*\r?\n/); 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 bodyEnd = regex(/^[\r?\n]+\/body\s*[\r?\n]*/);
const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyJson) => { 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 = { module.exports = {
bodyJsonTag, bodyJsonTag,
bodyGraphqlTag, bodyGraphqlTag,
bodyTextTag, bodyTextTag,
bodyXmlTag bodyXmlTag,
bodyFormUrlEncodedTag
}; };

View File

@ -15,7 +15,6 @@ const {
const newline = regex(/^\r?\n/); const newline = regex(/^\r?\n/);
const newLineOrEndOfInput = choice([newline, endOfInput]); const newLineOrEndOfInput = choice([newline, endOfInput]);
const begin = regex(/^headers\s*\r?\n/); const begin = regex(/^headers\s*\r?\n/);
const end = regex(/^[\r?\n]*\/headers\s*[\r?\n]*/); const end = regex(/^[\r?\n]*\/headers\s*[\r?\n]*/);
const word = regex(/^[^\s\t\n]+/g); const word = regex(/^[^\s\t\n]+/g);

View File

@ -12,7 +12,8 @@ const {
bodyJsonTag, bodyJsonTag,
bodyGraphqlTag, bodyGraphqlTag,
bodyTextTag, bodyTextTag,
bodyXmlTag bodyXmlTag,
bodyFormUrlEncodedTag
} = require('./body-tag'); } = require('./body-tag');
const bruToJson = (fileContents) => { const bruToJson = (fileContents) => {
@ -24,6 +25,7 @@ const bruToJson = (fileContents) => {
bodyGraphqlTag, bodyGraphqlTag,
bodyTextTag, bodyTextTag,
bodyXmlTag, bodyXmlTag,
bodyFormUrlEncodedTag,
anyChar anyChar
])); ]));

View File

@ -40,3 +40,7 @@ body(type=xml)
<body>back to the ice age</body> <body>back to the ice age</body>
/body /body
body(type=form-url-encoded)
1 username john
0 password {{password}}
/body

View File

@ -56,7 +56,19 @@ describe('bruToJson', () => {
"query": " {\n launchesPast {\n launch_success\n }\n }" "query": " {\n launchesPast {\n launch_success\n }\n }"
}, },
"text": " Hello, there. You must be from the past", "text": " Hello, there. You must be from the past",
"xml": " <body>back to the ice age</body>" "xml": " <body>back to the ice age</body>",
"formUrlEncoded": [
{
"enabled": "1",
"key": "username",
"value": "john"
},
{
"enabled": "0",
"key": "password",
"value": "{{password}}"
}
]
} }
}); });
}); });