feat: fix bugs in bruno-lang in data format

This commit is contained in:
Anoop M D 2023-01-17 00:20:22 +05:30
parent e1e7b37ce5
commit b5116b54af
9 changed files with 173 additions and 198 deletions

View File

@ -96,8 +96,8 @@ const line = sequenceOf([
newLineOrEndOfInput
]).map(([_, enabled, __, key, ___, value]) => {
return {
"enabled": enabled,
"key": key,
"enabled": Number(enabled) ? true : false,
"name": key,
"value": value
};
});

View File

@ -4,7 +4,6 @@ const {
optionalWhitespace,
choice,
endOfInput,
everyCharUntil,
between,
digit,
many,
@ -29,8 +28,8 @@ const line = sequenceOf([
newLineOrEndOfInput
]).map(([_, enabled, __, key, ___, value]) => {
return {
"enabled": enabled,
"key": key,
"enabled": Number(enabled) ? true : false,
"name": key,
"value": value
};
});

View File

@ -41,41 +41,42 @@ const bruToJson = (fileContents) => {
.reduce((acc, item) => _.merge(acc, item), {});
return {
ver: parsed.ver,
type: parsed.type || '',
name: parsed.name || '',
request: {
method: parsed.method || '',
url: parsed.url || '',
params: parsed.params || [],
headers: parsed.headers || [],
body: parsed.body || {mode: 'none'}
}
}
};
const jsonToBru = (json) => {
const {
ver,
type,
name,
request: {
method,
url,
params,
headers,
body
}
} = json;
let bru = `ver ${ver}
type ${type}
name ${name}
let bru = `name ${name}
method ${method}
url ${url}
body-mode ${body.mode}
type ${type}
body-mode ${body ? body.mode : 'none'}
`;
if(params && params.length) {
bru += `
params
${params.map(param => ` ${param.enabled} ${param.key} ${param.value}`).join('\n')}
${params.map(param => ` ${param.enabled ? 1 : 0} ${param.name} ${param.value}`).join('\n')}
/params
`;
}
@ -83,12 +84,12 @@ ${params.map(param => ` ${param.enabled} ${param.key} ${param.value}`).join('\n
if(headers && headers.length) {
bru += `
headers
${headers.map(header => ` ${header.enabled} ${header.key} ${header.value}`).join('\n')}
${headers.map(header => ` ${header.enabled ? 1 : 0} ${header.name} ${header.value}`).join('\n')}
/headers
`;
}
if(body.json && body.json.length) {
if(body && body.json && body.json.length) {
let jsonText = '';
let bodyJson = body.json;
if(bodyJson && bodyJson.length) {
@ -108,7 +109,7 @@ ${indentString(jsonText)}
`;
}
if(body.graphql && body.graphql.query) {
if(body && body.graphql && body.graphql.query) {
bru += `
body(type=graphql)
${indentString(body.graphql.query)}
@ -116,7 +117,7 @@ ${indentString(body.graphql.query)}
`;
}
if(body.text && body.text.length) {
if(body && body.text && body.text.length) {
bru += `
body(type=text)
${indentString(body.text)}
@ -124,7 +125,7 @@ ${indentString(body.text)}
`;
}
if(body.xml && body.xml.length) {
if(body && body.xml && body.xml.length) {
bru += `
body(type=xml)
${indentString(body.xml)}
@ -132,18 +133,18 @@ ${indentString(body.xml)}
`;
}
if(body.formUrlEncoded && body.formUrlEncoded.length) {
if(body && body.formUrlEncoded && body.formUrlEncoded.length) {
bru += `
body(type=form-url-encoded)
${body.formUrlEncoded.map(item => ` ${item.enabled} ${item.key} ${item.value}`).join('\n')}
${body.formUrlEncoded.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
/body
`;
}
if(body.multipartForm && body.multipartForm.length) {
if(body && body.multipartForm && body.multipartForm.length) {
bru += `
body(type=multipart-form)
${body.multipartForm.map(item => ` ${item.enabled} ${item.key} ${item.value}`).join('\n')}
${body.multipartForm.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
/body
`;
}

View File

@ -13,7 +13,6 @@ const newLineOrEndOfInput = choice([endOfInput, newline]);
const inlineTag = sequenceOf([
choice([
str('ver'),
str('type'),
str('name'),
str('method'),

View File

@ -29,8 +29,8 @@ const line = sequenceOf([
newLineOrEndOfInput
]).map(([_, enabled, __, key, ___, value]) => {
return {
"enabled": enabled,
"key": key,
"enabled": Number(enabled) ? true : false,
"name": key,
"value": value
};
});

View File

@ -2,48 +2,51 @@ const fs = require('fs');
const path = require('path');
const {
jsonToBru
bruToJson
} = require('../src');
describe('bruToJson', () => {
it('should convert json file into .bru file', () => {
const request = {
"ver": "1.0",
it('should parse .bru file contents', () => {
const requestFile = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const result = bruToJson(requestFile);
expect(result).toEqual({
"type": "http-request",
"name": "Send Bulk SMS",
"request": {
"method": "GET",
"url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010",
"params": [
{
"enabled": "1",
"key": "apiKey",
"enabled": true,
"name": "apiKey",
"value": "secret"
},
{
"enabled": "1",
"key": "numbers",
"enabled": true,
"name": "numbers",
"value": "998877665"
},
{
"enabled": "1",
"key": "message",
"enabled": true,
"name": "message",
"value": "hello"
}
],
"headers": [
{
"enabled": "1",
"key": "content-type",
"enabled": true,
"name": "content-type",
"value": "application/json"
},
{
"enabled": "1",
"key": "accept-language",
"enabled": true,
"name": "accept-language",
"value": "en-US,en;q=0.9,hi;q=0.8"
},
{
"enabled": "0",
"key": "transaction-id",
"enabled": false,
"name": "transaction-id",
"value": "{{transactionId}}"
}
],
@ -51,41 +54,37 @@ describe('bruToJson', () => {
"mode": "json",
"json": '{"apikey":"secret","numbers":"+91998877665"}',
"graphql": {
"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",
"xml": "<body>back to the ice age</body>",
"text": " Hello, there. You must be from the past",
"xml": " <body>back to the ice age</body>",
"formUrlEncoded": [
{
"enabled": "1",
"key": "username",
"enabled": true,
"name": "username",
"value": "john"
},
{
"enabled": "0",
"key": "password",
"enabled": false,
"name": "password",
"value": "{{password}}"
}
],
"multipartForm": [
{
"enabled": "1",
"key": "username",
"enabled": true,
"name": "username",
"value": "nash"
},
{
"enabled": "0",
"key": "password",
"enabled": false,
"name": "password",
"value": "governingdynamics"
}
]
}
};
const expectedBruFile = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const actualBruFile = jsonToBru(request);
expect(expectedBruFile).toEqual(actualBruFile);
}
});
});
});

View File

@ -1,8 +1,7 @@
ver 1.0
type http-request
name Send Bulk SMS
method GET
url https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010
type http-request
body-mode json
params

View File

@ -2,32 +2,9 @@ const inlineTag = require('../src/inline-tag');
const {
sepBy,
char,
many,
choice
many
} = require('arcsecond');
describe('version', () => {
it('should parse version number', () => {
const input = 'ver 1.0';
const result = inlineTag.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual({ ver: '1.0' });
});
it('should allow whitespaces while parsing version number', () => {
const input = 'ver 1.0';
const result = inlineTag.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual({ ver: '1.0' });
});
it('should fail to parse when version number is missing', () => {
const input = 'ver';
const result = inlineTag.run(input);
expect(result.isError).toBe(true);
});
});
describe('type', () => {
it('should parse the type', () => {
const input = 'type http-request';
@ -53,7 +30,6 @@ describe('type', () => {
describe('multiple inline tags', () => {
it('should parse the multiple inline tags', () => {
const input = `
ver 1.0
type http-request
name Send Bulk SMS
method GET
@ -71,7 +47,6 @@ body-mode json
expect(result.isError).toBe(false);
expect(result.result).toEqual([
[],
[{ ver: '1.0' }],
[{ type: 'http-request' }],
[{ name: 'Send Bulk SMS' }],
[{ method: 'GET' }],

View File

@ -2,51 +2,48 @@ const fs = require('fs');
const path = require('path');
const {
bruToJson
jsonToBru
} = require('../src');
describe('bruToJson', () => {
it('should parse .bru file contents', () => {
const requestFile = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const result = bruToJson(requestFile);
expect(result).toEqual({
"ver": "1.0",
it('should convert json file into .bru file', () => {
const request = {
"type": "http-request",
"name": "Send Bulk SMS",
"request": {
"method": "GET",
"url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010",
"params": [
{
"enabled": "1",
"key": "apiKey",
"enabled": true,
"name": "apiKey",
"value": "secret"
},
{
"enabled": "1",
"key": "numbers",
"enabled": true,
"name": "numbers",
"value": "998877665"
},
{
"enabled": "1",
"key": "message",
"enabled": true,
"name": "message",
"value": "hello"
}
],
"headers": [
{
"enabled": "1",
"key": "content-type",
"enabled": true,
"name": "content-type",
"value": "application/json"
},
{
"enabled": "1",
"key": "accept-language",
"enabled": true,
"name": "accept-language",
"value": "en-US,en;q=0.9,hi;q=0.8"
},
{
"enabled": "0",
"key": "transaction-id",
"enabled": false,
"name": "transaction-id",
"value": "{{transactionId}}"
}
],
@ -54,36 +51,42 @@ describe('bruToJson', () => {
"mode": "json",
"json": '{"apikey":"secret","numbers":"+91998877665"}',
"graphql": {
"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",
"xml": " <body>back to the ice age</body>",
"text": "Hello, there. You must be from the past",
"xml": "<body>back to the ice age</body>",
"formUrlEncoded": [
{
"enabled": "1",
"key": "username",
"enabled": true,
"name": "username",
"value": "john"
},
{
"enabled": "0",
"key": "password",
"enabled": false,
"name": "password",
"value": "{{password}}"
}
],
"multipartForm": [
{
"enabled": "1",
"key": "username",
"enabled": true,
"name": "username",
"value": "nash"
},
{
"enabled": "0",
"key": "password",
"enabled": false,
"name": "password",
"value": "governingdynamics"
}
]
}
});
}
};
const expectedBruFile = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const actualBruFile = jsonToBru(request);
expect(expectedBruFile).toEqual(actualBruFile);
});
});