forked from extern/bruno
feat: bruno lang now supports parsing for url encoded params
This commit is contained in:
parent
c29ab50a3d
commit
4eed999db1
@ -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
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
]));
|
||||
|
||||
|
@ -40,3 +40,7 @@ body(type=xml)
|
||||
<body>back to the ice age</body>
|
||||
/body
|
||||
|
||||
body(type=form-url-encoded)
|
||||
1 username john
|
||||
0 password {{password}}
|
||||
/body
|
||||
|
@ -56,7 +56,19 @@ describe('bruToJson', () => {
|
||||
"query": " {\n launchesPast {\n launch_success\n }\n }"
|
||||
},
|
||||
"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}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user