feat: bruno-lang now supprts parsing body json

This commit is contained in:
Anoop M D 2023-01-15 00:45:01 +05:30
parent 0750af4c68
commit 87a4778a91
6 changed files with 134 additions and 3 deletions

View File

@ -0,0 +1,59 @@
const {
str,
sequenceOf,
optionalWhitespace,
choice,
endOfInput,
between,
regex,
everyCharUntil
} = require("arcsecond");
const { safeParseJson } = require('./utils');
const newline = regex(/^\r?\n/);
const newLineOrEndOfInput = choice([newline, endOfInput]);
// body(type=json)
const bodyJsonBegin = sequenceOf([
str('body'),
optionalWhitespace,
str('('),
optionalWhitespace,
str('type'),
optionalWhitespace,
str('='),
optionalWhitespace,
str('json'),
optionalWhitespace,
regex(/^\)\s*\r?\n/),
]);
const bodyEnd = sequenceOf([
regex(/^\/body\s*/),
newLineOrEndOfInput
]);
const bodyJsonTag = between(bodyJsonBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyJson) => {
if(bodyJson && bodyJson.length) {
bodyJson = bodyJson.trim();
const safelyParsed = safeParseJson(bodyJson);
if(!safelyParsed) {
return {
bodyJson
}
}
return {
bodyJson: JSON.stringify(safelyParsed)
};
}
return {
bodyJson
};
});
module.exports = {
bodyJsonTag
};

View File

@ -7,12 +7,16 @@ const {
const inlineTag = require('./inline-tag');
const paramsTag = require('./params-tag');
const headersTag = require('./headers-tag');
const {
bodyJsonTag
} = require('./body-tag');
const bruToJson = (fileContents) => {
const parser = many(choice([
inlineTag,
paramsTag,
headersTag,
bodyJsonTag,
anyChar
]));
@ -33,7 +37,8 @@ const bruToJson = (fileContents) => {
method: parsed.method,
url: parsed.url,
params: parsed.params,
headers: parsed.headers
headers: parsed.headers,
bodyJson: parsed.bodyJson
}
};

View File

@ -0,0 +1,12 @@
// safely parse json
const safeParseJson = (json) => {
try {
return JSON.parse(json);
} catch (e) {
return null;
}
};
module.exports = {
safeParseJson
};

View File

@ -0,0 +1,46 @@
const { bodyJsonTag } = require('../src/body-tag');
describe('bodyJsonTag', () => {
const testbodyJson = (input, expected) => {
const result = bodyJsonTag.run(input);
expect(result.isError).toBe(false);
expect(result.result.bodyJson).toEqual('{"foo":"bar"}');
};
// simple case
it('should parse json body tag - 1', () => {
const input = 'body(type=json)\n{ "foo": "bar" }\n/body';
testbodyJson(input, '{ "foo": "bar" }\n');
});
// space between body and args
it('should parse json body tag - 2', () => {
const input = 'body (type = json)\n{ "foo": "bar" }\n/body';
testbodyJson(input, '{ "foo": "bar" }\n');
});
// space after body tag
it('should parse json body tag - 3', () => {
const input = 'body (type = json) \n{ "foo": "bar" }\n/body';
testbodyJson(input, '{ "foo": "bar" }\n');
});
// space after body tag
it('should parse json body tag - 4', () => {
const input = 'body (type = json) \n{ "foo": "bar" }\n/body ';
testbodyJson(input, '{ "foo": "bar" }\n');
});
it('should fail to parse when body tag is missing', () => {
const input = '{ "foo": "bar" }\n/body';
const result = bodyJsonTag.run(input);
expect(result.isError).toBe(true);
});
it('should fail to parse when body end tag is missing', () => {
const input = 'body (type = json)\n{ "foo": "bar" }';
const result = bodyJsonTag.run(input);
expect(result.isError).toBe(true);
});
});

View File

@ -16,3 +16,11 @@ headers
1 accept-language en-US,en;q=0.9,hi;q=0.8
0 transaction-id {{transactionId}}
/headers
body(type=json)
{
"apikey": "secret",
"numbers": "+91998877665"
}
/body

View File

@ -49,7 +49,8 @@ describe('bruToJson', () => {
"key": "transaction-id",
"value": "{{transactionId}}"
}
]
],
"bodyJson": '{"apikey":"secret","numbers":"+91998877665"}'
});
});
});