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 newLineOrEndOfInput
]).map(([_, enabled, __, key, ___, value]) => { ]).map(([_, enabled, __, key, ___, value]) => {
return { return {
"enabled": enabled, "enabled": Number(enabled) ? true : false,
"key": key, "name": key,
"value": value "value": value
}; };
}); });

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,32 +2,9 @@ const inlineTag = require('../src/inline-tag');
const { const {
sepBy, sepBy,
char, char,
many, many
choice
} = require('arcsecond'); } = 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', () => { describe('type', () => {
it('should parse the type', () => { it('should parse the type', () => {
const input = 'type http-request'; const input = 'type http-request';
@ -53,7 +30,6 @@ describe('type', () => {
describe('multiple inline tags', () => { describe('multiple inline tags', () => {
it('should parse the multiple inline tags', () => { it('should parse the multiple inline tags', () => {
const input = ` const input = `
ver 1.0
type http-request type http-request
name Send Bulk SMS name Send Bulk SMS
method GET method GET
@ -71,7 +47,6 @@ body-mode json
expect(result.isError).toBe(false); expect(result.isError).toBe(false);
expect(result.result).toEqual([ expect(result.result).toEqual([
[], [],
[{ ver: '1.0' }],
[{ type: 'http-request' }], [{ type: 'http-request' }],
[{ name: 'Send Bulk SMS' }], [{ name: 'Send Bulk SMS' }],
[{ method: 'GET' }], [{ method: 'GET' }],

View File

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