feat: bruno lang now supports parsing text only multipart form data

This commit is contained in:
Anoop M D 2023-01-15 23:02:59 +05:30
parent 4eed999db1
commit 8dab9268f2
4 changed files with 36 additions and 5 deletions

View File

@ -26,9 +26,6 @@ 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) => {
@ -108,6 +105,12 @@ const line = sequenceOf([
const lines = many(line); const lines = many(line);
const keyvalLines = sepBy(newline)(lines); const keyvalLines = sepBy(newline)(lines);
// body(type=form-url-encoded)
const bodyFormUrlEncoded = regex(/^body\s*\(\s*type\s*=\s*form-url-encoded\s*\)\s*\r?\n/);
// body(type=multipart-form)
const bodyMultipartForm = regex(/^body\s*\(\s*type\s*=\s*multipart-form\s*\)\s*\r?\n/);
// this regex allows the body end tag to start without a newline // this regex allows the body end tag to start without a newline
// currently the line parser consumes the last newline // currently the line parser consumes the last newline
// todo: fix this // todo: fix this
@ -121,10 +124,19 @@ const bodyFormUrlEncodedTag = between(bodyFormUrlEncoded)(bodyEndRelaxed)(keyval
} }
}); });
const bodyMultipartFormTag = between(bodyMultipartForm)(bodyEndRelaxed)(keyvalLines).map(([result]) => {
return {
body: {
multipartForm: result
}
}
});
module.exports = { module.exports = {
bodyJsonTag, bodyJsonTag,
bodyGraphqlTag, bodyGraphqlTag,
bodyTextTag, bodyTextTag,
bodyXmlTag, bodyXmlTag,
bodyFormUrlEncodedTag bodyFormUrlEncodedTag,
bodyMultipartFormTag
}; };

View File

@ -13,7 +13,8 @@ const {
bodyGraphqlTag, bodyGraphqlTag,
bodyTextTag, bodyTextTag,
bodyXmlTag, bodyXmlTag,
bodyFormUrlEncodedTag bodyFormUrlEncodedTag,
bodyMultipartFormTag
} = require('./body-tag'); } = require('./body-tag');
const bruToJson = (fileContents) => { const bruToJson = (fileContents) => {
@ -26,6 +27,7 @@ const bruToJson = (fileContents) => {
bodyTextTag, bodyTextTag,
bodyXmlTag, bodyXmlTag,
bodyFormUrlEncodedTag, bodyFormUrlEncodedTag,
bodyMultipartFormTag,
anyChar anyChar
])); ]));

View File

@ -44,3 +44,8 @@ body(type=form-url-encoded)
1 username john 1 username john
0 password {{password}} 0 password {{password}}
/body /body
body(type=multipart-form)
1 username nash
0 password governingdynamics
/body

View File

@ -68,6 +68,18 @@ describe('bruToJson', () => {
"key": "password", "key": "password",
"value": "{{password}}" "value": "{{password}}"
} }
],
"multipartForm": [
{
"enabled": "1",
"key": "username",
"value": "nash"
},
{
"enabled": "0",
"key": "password",
"value": "governingdynamics"
}
] ]
} }
}); });