mirror of
https://github.com/usebruno/bruno.git
synced 2025-01-09 15:38:34 +01:00
feat: bruno-lang now supprts parsing body json
This commit is contained in:
parent
0750af4c68
commit
87a4778a91
59
packages/bruno-lang/src/body-tag.js
Normal file
59
packages/bruno-lang/src/body-tag.js
Normal 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
|
||||
};
|
@ -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
|
||||
}
|
||||
};
|
||||
|
||||
|
12
packages/bruno-lang/src/utils.js
Normal file
12
packages/bruno-lang/src/utils.js
Normal file
@ -0,0 +1,12 @@
|
||||
// safely parse json
|
||||
const safeParseJson = (json) => {
|
||||
try {
|
||||
return JSON.parse(json);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
safeParseJson
|
||||
};
|
46
packages/bruno-lang/tests/body-tag.spec.js
Normal file
46
packages/bruno-lang/tests/body-tag.spec.js
Normal 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);
|
||||
});
|
||||
});
|
@ -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
|
||||
|
||||
|
@ -49,7 +49,8 @@ describe('bruToJson', () => {
|
||||
"key": "transaction-id",
|
||||
"value": "{{transactionId}}"
|
||||
}
|
||||
]
|
||||
],
|
||||
"bodyJson": '{"apikey":"secret","numbers":"+91998877665"}'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user