chore(#197): ran prettier on packages/bruno-lang

This commit is contained in:
Anoop M D 2023-09-22 00:42:48 +05:30
parent 5af2f68252
commit 116e050987
33 changed files with 732 additions and 741 deletions

View File

@ -1,9 +1,4 @@
const {
bruToJson,
jsonToBru,
bruToEnvJson,
envJsonToBru
} = require('../v1/src');
const { bruToJson, jsonToBru, bruToEnvJson, envJsonToBru } = require('../v1/src');
const bruToJsonV2 = require('../v2/src/bruToJson');
const jsonToBruV2 = require('../v2/src/jsonToBru');

View File

@ -1,8 +1,4 @@
const {
between,
regex,
everyCharUntil
} = require("arcsecond");
const { between, regex, everyCharUntil } = require('arcsecond');
const keyvalLines = require('./key-val-lines');
// body(type=json)
@ -37,7 +33,7 @@ const bodyGraphqlTag = between(bodyGraphqlBegin)(bodyEnd)(everyCharUntil(bodyEnd
query: bodyGraphql
}
}
}
};
});
const bodyGraphqlVarsTag = between(bodyGraphqlVarsBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((varsGraphql) => {
@ -47,7 +43,7 @@ const bodyGraphqlVarsTag = between(bodyGraphqlVarsBegin)(bodyEnd)(everyCharUntil
variables: varsGraphql
}
}
}
};
});
const bodyTextTag = between(bodyTextBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyText) => {
@ -55,7 +51,7 @@ const bodyTextTag = between(bodyTextBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map
body: {
text: bodyText
}
}
};
});
const bodyXmlTag = between(bodyXmlBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((bodyXml) => {
@ -63,7 +59,7 @@ const bodyXmlTag = between(bodyXmlBegin)(bodyEnd)(everyCharUntil(bodyEnd)).map((
body: {
xml: bodyXml
}
}
};
});
/**
@ -90,20 +86,22 @@ const bodyMultipartForm = regex(/^body\s*\(\s*type\s*=\s*multipart-form\s*\)\s*\
// todo: fix this
const bodyEndRelaxed = regex(/^[\r?\n]*\/body\s*[\r?\n]*/);
const bodyFormUrlEncodedTagDeprecated = between(bodyFormUrlEncodedDeprecated)(bodyEndRelaxed)(keyvalLines).map(([result]) => {
return {
body: {
formUrlEncoded: result
}
const bodyFormUrlEncodedTagDeprecated = between(bodyFormUrlEncodedDeprecated)(bodyEndRelaxed)(keyvalLines).map(
([result]) => {
return {
body: {
formUrlEncoded: result
}
};
}
});
);
const bodyFormUrlEncodedTag = between(bodyFormUrlEncoded)(bodyEndRelaxed)(keyvalLines).map(([result]) => {
return {
body: {
formUrlEncoded: result
}
}
};
});
const bodyMultipartFormTag = between(bodyMultipartForm)(bodyEndRelaxed)(keyvalLines).map(([result]) => {
@ -111,7 +109,7 @@ const bodyMultipartFormTag = between(bodyMultipartForm)(bodyEndRelaxed)(keyvalLi
body: {
multipartForm: result
}
}
};
});
module.exports = {

View File

@ -1,7 +1,4 @@
const {
between,
regex,
} = require("arcsecond");
const { between, regex } = require('arcsecond');
const { each } = require('lodash');
const keyValLines = require('./key-val-lines');
@ -10,7 +7,7 @@ const end = regex(/^[\r?\n]*\/vars\s*[\r?\n]*/);
const envVarsTag = between(begin)(end)(keyValLines).map(([variables]) => {
each(variables, (variable) => {
variable.type = "text"
variable.type = 'text';
});
return {

View File

@ -1,7 +1,4 @@
const {
between,
regex
} = require("arcsecond");
const { between, regex } = require('arcsecond');
const keyValLines = require('./key-val-lines');
const begin = regex(/^headers\s*\r?\n/);

View File

@ -1,16 +1,9 @@
const {
many,
choice,
anyChar
} = require("arcsecond");
const { many, choice, anyChar } = require('arcsecond');
const _ = require('lodash');
const {
indentString,
outdentString
} = require('./utils');
const { indentString, outdentString } = require('./utils');
const inlineTag = require('./inline-tag');
const paramsTag = require('./params-tag');
const inlineTag = require('./inline-tag');
const paramsTag = require('./params-tag');
const headersTag = require('./headers-tag');
const {
bodyJsonTag,
@ -22,31 +15,30 @@ const {
bodyFormUrlEncodedTag,
bodyMultipartFormTag
} = require('./body-tag');
const scriptTag = require('./script-tag');
const testsTag = require('./tests-tag');
const scriptTag = require('./script-tag');
const testsTag = require('./tests-tag');
const bruToJson = (fileContents) => {
const parser = many(choice([
inlineTag,
paramsTag,
headersTag,
bodyJsonTag,
bodyGraphqlTag,
bodyGraphqlVarsTag,
bodyTextTag,
bodyXmlTag,
bodyFormUrlEncodedTagDeprecated,
bodyFormUrlEncodedTag,
bodyMultipartFormTag,
scriptTag,
testsTag,
anyChar
]));
const parser = many(
choice([
inlineTag,
paramsTag,
headersTag,
bodyJsonTag,
bodyGraphqlTag,
bodyGraphqlVarsTag,
bodyTextTag,
bodyXmlTag,
bodyFormUrlEncodedTagDeprecated,
bodyFormUrlEncodedTag,
bodyMultipartFormTag,
scriptTag,
testsTag,
anyChar
])
);
const parsed = parser
.run(fileContents)
.result
.reduce((acc, item) => _.merge(acc, item), {});
const parsed = parser.run(fileContents).result.reduce((acc, item) => _.merge(acc, item), {});
const json = {
type: parsed.type || '',
@ -57,7 +49,7 @@ const bruToJson = (fileContents) => {
url: parsed.url || '',
params: parsed.params || [],
headers: parsed.headers || [],
body: parsed.body || {mode: 'none'},
body: parsed.body || { mode: 'none' },
script: parsed.script ? outdentString(parsed.script) : '',
tests: parsed.tests ? outdentString(parsed.tests) : ''
}
@ -65,23 +57,23 @@ const bruToJson = (fileContents) => {
const body = _.get(json, 'request.body');
if(body && body.text) {
if (body && body.text) {
body.text = outdentString(body.text);
}
if(body && body.json) {
if (body && body.json) {
body.json = outdentString(body.json);
}
if(body && body.xml) {
if (body && body.xml) {
body.xml = outdentString(body.xml);
}
if(body && body.graphql && body.graphql.query) {
if (body && body.graphql && body.graphql.query) {
body.graphql.query = outdentString(body.graphql.query);
}
if(body && body.graphql && body.graphql.variables) {
if (body && body.graphql && body.graphql.variables) {
body.graphql.variables = outdentString(body.graphql.variables);
}
@ -93,15 +85,7 @@ const jsonToBru = (json) => {
type,
name,
seq,
request: {
method,
url,
params,
headers,
body,
script,
tests
}
request: { method, url, params, headers, body, script, tests }
} = json;
let bru = `name ${name}
@ -112,23 +96,23 @@ body-mode ${body ? body.mode : 'none'}
seq ${seq ? seq : 1}
`;
if(params && params.length) {
if (params && params.length) {
bru += `
params
${params.map(param => ` ${param.enabled ? 1 : 0} ${param.name} ${param.value}`).join('\n')}
${params.map((param) => ` ${param.enabled ? 1 : 0} ${param.name} ${param.value}`).join('\n')}
/params
`;
}
if(headers && headers.length) {
if (headers && headers.length) {
bru += `
headers
${headers.map(header => ` ${header.enabled ? 1 : 0} ${header.name} ${header.value}`).join('\n')}
${headers.map((header) => ` ${header.enabled ? 1 : 0} ${header.name} ${header.value}`).join('\n')}
/headers
`;
}
if(body && body.json && body.json.length) {
if (body && body.json && body.json.length) {
bru += `
body(type=json)
${indentString(body.json)}
@ -136,7 +120,7 @@ ${indentString(body.json)}
`;
}
if(body && body.graphql && body.graphql.query) {
if (body && body.graphql && body.graphql.query) {
bru += `
body(type=graphql)
${indentString(body.graphql.query)}
@ -144,7 +128,7 @@ ${indentString(body.graphql.query)}
`;
}
if(body && body.graphql && body.graphql.variables) {
if (body && body.graphql && body.graphql.variables) {
bru += `
body(type=graphql-vars)
${indentString(body.graphql.variables)}
@ -152,7 +136,7 @@ ${indentString(body.graphql.variables)}
`;
}
if(body && body.text && body.text.length) {
if (body && body.text && body.text.length) {
bru += `
body(type=text)
${indentString(body.text)}
@ -160,7 +144,7 @@ ${indentString(body.text)}
`;
}
if(body && body.xml && body.xml.length) {
if (body && body.xml && body.xml.length) {
bru += `
body(type=xml)
${indentString(body.xml)}
@ -168,23 +152,23 @@ ${indentString(body.xml)}
`;
}
if(body && body.formUrlEncoded && body.formUrlEncoded.length) {
if (body && body.formUrlEncoded && body.formUrlEncoded.length) {
bru += `
body(type=form-urlencoded)
${body.formUrlEncoded.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
${body.formUrlEncoded.map((item) => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
/body
`;
}
if(body && body.multipartForm && body.multipartForm.length) {
if (body && body.multipartForm && body.multipartForm.length) {
bru += `
body(type=multipart-form)
${body.multipartForm.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
${body.multipartForm.map((item) => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
/body
`;
}
if(script && script.length) {
if (script && script.length) {
bru += `
script
${indentString(script)}
@ -192,7 +176,7 @@ ${indentString(script)}
`;
}
if(tests && tests.length) {
if (tests && tests.length) {
bru += `
tests
${indentString(tests)}
@ -207,15 +191,9 @@ ${indentString(tests)}
const envVarsTag = require('./env-vars-tag');
const bruToEnvJson = (fileContents) => {
const parser = many(choice([
envVarsTag,
anyChar
]));
const parser = many(choice([envVarsTag, anyChar]));
const parsed = parser
.run(fileContents)
.result
.reduce((acc, item) => _.merge(acc, item), {});
const parsed = parser.run(fileContents).result.reduce((acc, item) => _.merge(acc, item), {});
const json = {
variables: parsed.variables || []
@ -225,15 +203,13 @@ const bruToEnvJson = (fileContents) => {
};
const envJsonToBru = (json) => {
const {
variables
} = json;
const { variables } = json;
let bru = '';
if(variables && variables.length) {
if (variables && variables.length) {
bru += `vars
${variables.map(item => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
${variables.map((item) => ` ${item.enabled ? 1 : 0} ${item.name} ${item.value}`).join('\n')}
/vars
`;
}

View File

@ -1,36 +1,19 @@
const {
sequenceOf,
str,
regex,
choice,
endOfInput,
everyCharUntil
} = require("arcsecond");
const { sequenceOf, str, regex, choice, endOfInput, everyCharUntil } = require('arcsecond');
const whitespace = regex(/^[ \t]*/)
const whitespace = regex(/^[ \t]*/);
const newline = regex(/^\r?\n/);
const newLineOrEndOfInput = choice([endOfInput, newline]);
const inlineTag = sequenceOf([
choice([
str('type'),
str('name'),
str('method'),
str('url'),
str('seq'),
str('body-mode')
]),
choice([str('type'), str('name'), str('method'), str('url'), str('seq'), str('body-mode')]),
whitespace,
choice([
newline,
everyCharUntil(newLineOrEndOfInput)
])
choice([newline, everyCharUntil(newLineOrEndOfInput)])
]).map(([key, _, val]) => {
if(val === '\n' || val === '\r\n') {
if (val === '\n' || val === '\r\n') {
val = '';
}
if(key === 'body-mode') {
if (key === 'body-mode') {
return {
body: {
mode: val

View File

@ -1,13 +1,4 @@
const {
sequenceOf,
whitespace,
optionalWhitespace,
choice,
digit,
many,
regex,
sepBy,
} = require("arcsecond");
const { sequenceOf, whitespace, optionalWhitespace, choice, digit, many, regex, sepBy } = require('arcsecond');
const newline = regex(/^\r?\n/);
const wordWithoutWhitespace = regex(/^[^\s\r?\t\n]+/g);
@ -23,37 +14,29 @@ const line = sequenceOf([
wordWithWhitespace
]).map(([_, enabled, __, key, ___, value]) => {
return {
"enabled": Number(enabled) ? true : false,
"name": key ? key.trim() : "",
"value": value ? value.trim() : ""
enabled: Number(enabled) ? true : false,
name: key ? key.trim() : '',
value: value ? value.trim() : ''
};
});
// matching lines like: 1 key follows by [whitespaces] and a newline
const line2 = sequenceOf([
optionalWhitespace,
digit,
whitespace,
wordWithoutWhitespace,
regex(/^\s*\r?\n/)
]).map(([_, enabled, __, key]) => {
return {
"enabled": Number(enabled) ? true : false,
"name": key,
"value": ""
};
});
const line2 = sequenceOf([optionalWhitespace, digit, whitespace, wordWithoutWhitespace, regex(/^\s*\r?\n/)]).map(
([_, enabled, __, key]) => {
return {
enabled: Number(enabled) ? true : false,
name: key,
value: ''
};
}
);
// matching lines like: 1 followed by [whitespaces] and a newline
const line3 = sequenceOf([
optionalWhitespace,
digit,
regex(/^\s*\r?\n/)
]).map(([_, enabled]) => {
const line3 = sequenceOf([optionalWhitespace, digit, regex(/^\s*\r?\n/)]).map(([_, enabled]) => {
return {
"enabled": Number(enabled) ? true : false,
"name": "",
"value": ""
enabled: Number(enabled) ? true : false,
name: '',
value: ''
};
});

View File

@ -1,7 +1,4 @@
const {
between,
regex
} = require("arcsecond");
const { between, regex } = require('arcsecond');
const keyValLines = require('./key-val-lines');
const begin = regex(/^params\s*\r?\n/);

View File

@ -1,8 +1,4 @@
const {
between,
regex,
everyCharUntil
} = require("arcsecond");
const { between, regex, everyCharUntil } = require('arcsecond');
const scriptBegin = regex(/^script\s*\r?\n/);
const scriptEnd = regex(/^[\r?\n]+\/script[\s\r?\n]*/);

View File

@ -1,8 +1,4 @@
const {
between,
regex,
everyCharUntil
} = require("arcsecond");
const { between, regex, everyCharUntil } = require('arcsecond');
const testsBegin = regex(/^tests\s*\r?\n/);
const testsEnd = regex(/^[\r?\n]+\/tests[\s\r?\n]*/);

View File

@ -8,19 +8,25 @@ const safeParseJson = (json) => {
};
const indentString = (str) => {
if(!str || !str.length) {
if (!str || !str.length) {
return str;
}
return str.split("\n").map(line => " " + line).join("\n");
return str
.split('\n')
.map((line) => ' ' + line)
.join('\n');
};
const outdentString = (str) => {
if(!str || !str.length) {
if (!str || !str.length) {
return str;
}
return str.split("\n").map(line => line.replace(/^ /, '')).join("\n");
return str
.split('\n')
.map((line) => line.replace(/^ /, ''))
.join('\n');
};
module.exports = {

View File

@ -1,6 +1,5 @@
const { bodyJsonTag } = require('../src/body-tag');
describe('bodyJsonTag', () => {
const testbodyJson = (input, expected) => {
const result = bodyJsonTag.run(input);

View File

@ -1,9 +1,7 @@
const fs = require('fs');
const path = require('path');
const {
bruToEnvJson
} = require('../src');
const { bruToEnvJson } = require('../src');
describe('bruToEnvJson', () => {
it('should parse .bru file contents', () => {
@ -11,23 +9,26 @@ describe('bruToEnvJson', () => {
const result = bruToEnvJson(requestFile);
expect(result).toEqual({
"variables": [{
"enabled": true,
"name": "host",
"value": "https://www.google.com",
"type": "text"
}, {
"enabled": true,
"name": "jwt",
"value": "secret",
"type": "text"
}, {
"enabled": false,
"name": "Content-type",
"value": "application/json",
"type": "text"
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com',
type: 'text'
},
{
enabled: true,
name: 'jwt',
value: 'secret',
type: 'text'
},
{
enabled: false,
name: 'Content-type',
value: 'application/json',
type: 'text'
}
]
});
});
});

View File

@ -1,9 +1,7 @@
const fs = require('fs');
const path = require('path');
const {
bruToJson
} = require('../src');
const { bruToJson } = require('../src');
describe('bruToJson', () => {
it('should parse .bru file contents', () => {
@ -11,81 +9,81 @@ describe('bruToJson', () => {
const result = bruToJson(requestFile);
expect(result).toEqual({
"type": "http-request",
"name": "Send Bulk SMS",
"seq": 1,
"request": {
"method": "GET",
"url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010",
"params": [
type: 'http-request',
name: 'Send Bulk SMS',
seq: 1,
request: {
method: 'GET',
url: 'https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010',
params: [
{
"enabled": true,
"name": "apiKey",
"value": "secret"
enabled: true,
name: 'apiKey',
value: 'secret'
},
{
"enabled": true,
"name": "numbers",
"value": "998877665"
enabled: true,
name: 'numbers',
value: '998877665'
},
{
"enabled": true,
"name": "message",
"value": "hello"
enabled: true,
name: 'message',
value: 'hello'
}
],
"headers": [
headers: [
{
"enabled": true,
"name": "content-type",
"value": "application/json"
enabled: true,
name: 'content-type',
value: 'application/json'
},
{
"enabled": true,
"name": "accept-language",
"value": "en-US,en;q=0.9,hi;q=0.8"
enabled: true,
name: 'accept-language',
value: 'en-US,en;q=0.9,hi;q=0.8'
},
{
"enabled": false,
"name": "transaction-id",
"value": "{{transactionId}}"
enabled: false,
name: 'transaction-id',
value: '{{transactionId}}'
}
],
"body": {
"mode": "json",
"json": '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}',
"graphql": {
"query": "{\n launchesPast {\n launch_success\n }\n}"
body: {
mode: 'json',
json: '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}',
graphql: {
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>",
"formUrlEncoded": [
text: 'Hello, there. You must be from the past',
xml: '<body>back to the ice age</body>',
formUrlEncoded: [
{
"enabled": true,
"name": "username",
"value": "john"
enabled: true,
name: 'username',
value: 'john'
},
{
"enabled": false,
"name": "password",
"value": "{{password}}"
enabled: false,
name: 'password',
value: '{{password}}'
}
],
"multipartForm": [
multipartForm: [
{
"enabled": true,
"name": "username",
"value": "nash"
enabled: true,
name: 'username',
value: 'nash'
},
{
"enabled": false,
"name": "password",
"value": "governingdynamics"
enabled: false,
name: 'password',
value: 'governingdynamics'
}
]
},
"script": "const foo='bar';",
"tests": "bruno.test('200 ok', () => {});"
script: "const foo='bar';",
tests: "bruno.test('200 ok', () => {});"
}
});
});
@ -113,8 +111,8 @@ seq 1
params: [],
headers: [],
body: { mode: 'none' },
script: "",
tests: ""
script: '',
tests: ''
}
});
});

View File

@ -1,29 +1,31 @@
const fs = require('fs');
const path = require('path');
const {
envJsonToBru
} = require('../src');
const { envJsonToBru } = require('../src');
describe('envJsonToBru', () => {
it('should convert json file into .bru file', () => {
const env = {
"variables": [{
"enabled": true,
"name": "host",
"value": "https://www.google.com",
"type": "text"
}, {
"enabled": true,
"name": "jwt",
"value": "secret",
"type": "text"
}, {
"enabled": false,
"name": "Content-type",
"value": "application/json",
"type": "text"
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com',
type: 'text'
},
{
enabled: true,
name: 'jwt',
value: 'secret',
type: 'text'
},
{
enabled: false,
name: 'Content-type',
value: 'application/json',
type: 'text'
}
]
};
const expectedBruFile = fs.readFileSync(path.join(__dirname, 'fixtures', 'env.bru'), 'utf8');
@ -32,4 +34,3 @@ describe('envJsonToBru', () => {
expect(expectedBruFile).toEqual(actualBruFile);
});
});

View File

@ -1,9 +1,5 @@
const inlineTag = require('../src/inline-tag');
const {
sepBy,
char,
many
} = require('arcsecond');
const { sepBy, char, many } = require('arcsecond');
describe('type', () => {
it('should parse the type', () => {
@ -54,5 +50,5 @@ body-mode json
[{ body: { mode: 'json' } }],
[]
]);
})
});
});

View File

@ -1,88 +1,86 @@
const fs = require('fs');
const path = require('path');
const {
jsonToBru
} = require('../src');
const { jsonToBru } = require('../src');
describe('bruToJson', () => {
it('should convert json file into .bru file', () => {
const request = {
"type": "http-request",
"name": "Send Bulk SMS",
"seq": 1,
"request": {
"method": "GET",
"url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010",
"params": [
type: 'http-request',
name: 'Send Bulk SMS',
seq: 1,
request: {
method: 'GET',
url: 'https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010',
params: [
{
"enabled": true,
"name": "apiKey",
"value": "secret"
enabled: true,
name: 'apiKey',
value: 'secret'
},
{
"enabled": true,
"name": "numbers",
"value": "998877665"
enabled: true,
name: 'numbers',
value: '998877665'
},
{
"enabled": true,
"name": "message",
"value": "hello"
enabled: true,
name: 'message',
value: 'hello'
}
],
"headers": [
headers: [
{
"enabled": true,
"name": "content-type",
"value": "application/json"
enabled: true,
name: 'content-type',
value: 'application/json'
},
{
"enabled": true,
"name": "accept-language",
"value": "en-US,en;q=0.9,hi;q=0.8"
enabled: true,
name: 'accept-language',
value: 'en-US,en;q=0.9,hi;q=0.8'
},
{
"enabled": false,
"name": "transaction-id",
"value": "{{transactionId}}"
enabled: false,
name: 'transaction-id',
value: '{{transactionId}}'
}
],
"body": {
"mode": "json",
"json": '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}',
"graphql": {
"query": "{\n launchesPast {\n launch_success\n }\n}"
body: {
mode: 'json',
json: '{\n "apikey": "secret",\n "numbers": "+91998877665"\n}',
graphql: {
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>",
"formUrlEncoded": [
text: 'Hello, there. You must be from the past',
xml: '<body>back to the ice age</body>',
formUrlEncoded: [
{
"enabled": true,
"name": "username",
"value": "john"
enabled: true,
name: 'username',
value: 'john'
},
{
"enabled": false,
"name": "password",
"value": "{{password}}"
enabled: false,
name: 'password',
value: '{{password}}'
}
],
"multipartForm": [
multipartForm: [
{
"enabled": true,
"name": "username",
"value": "nash"
enabled: true,
name: 'username',
value: 'nash'
},
{
"enabled": false,
"name": "password",
"value": "governingdynamics"
enabled: false,
name: 'password',
value: 'governingdynamics'
}
]
},
"script": "const foo='bar';",
"tests": "bruno.test('200 ok', () => {});"
script: "const foo='bar';",
tests: "bruno.test('200 ok', () => {});"
}
};
@ -92,5 +90,3 @@ describe('bruToJson', () => {
expect(expectedBruFile).toEqual(actualBruFile);
});
});

View File

@ -1,10 +1,4 @@
const {
between,
regex,
anyChar,
many,
choice
} = require("arcsecond");
const { between, regex, anyChar, many, choice } = require('arcsecond');
const _ = require('lodash');
const keyValLines = require('../src/key-val-lines');
@ -19,15 +13,9 @@ const varsTag = between(begin)(end)(keyValLines).map(([variables]) => {
});
const toJson = (fileContents) => {
const parser = many(choice([
varsTag,
anyChar
]));
const parser = many(choice([varsTag, anyChar]));
const parsed = parser
.run(fileContents)
.result
.reduce((acc, item) => _.merge(acc, item), {});
const parsed = parser.run(fileContents).result.reduce((acc, item) => _.merge(acc, item), {});
const json = {
variables: parsed.variables || []
@ -46,11 +34,13 @@ vars
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: 'https://www.google.com'
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com'
}
]
});
});
@ -64,15 +54,18 @@ vars
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: 'https://www.google.com'
}, {
enabled: true,
name: 'auth',
value: 'jwt secret'
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com'
},
{
enabled: true,
name: 'auth',
value: 'jwt secret'
}
]
});
});
@ -87,11 +80,13 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: '',
value: ''
}]
variables: [
{
enabled: true,
name: '',
value: ''
}
]
});
});
@ -104,11 +99,13 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: '',
value: ''
}]
variables: [
{
enabled: true,
name: '',
value: ''
}
]
});
});
@ -121,11 +118,13 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: ''
}]
variables: [
{
enabled: true,
name: 'host',
value: ''
}
]
});
});
@ -138,11 +137,13 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: ''
}]
variables: [
{
enabled: true,
name: 'host',
value: ''
}
]
});
});
@ -157,19 +158,23 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: 'https://www.google.com'
}, {
enabled: true,
name: '',
value: ''
}, {
enabled: false,
name: 'Content-type',
value: 'application/json'
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com'
},
{
enabled: true,
name: '',
value: ''
},
{
enabled: false,
name: 'Content-type',
value: 'application/json'
}
]
});
});
@ -184,19 +189,23 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: 'https://www.google.com'
}, {
enabled: true,
name: '',
value: ''
}, {
enabled: false,
name: 'Content-type',
value: 'application/json'
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com'
},
{
enabled: true,
name: '',
value: ''
},
{
enabled: false,
name: 'Content-type',
value: 'application/json'
}
]
});
});
@ -211,20 +220,23 @@ vars
`;
const result = toJson(file);
expect(result).toEqual({
variables: [{
enabled: true,
name: 'host',
value: 'https://www.google.com'
}, {
enabled: true,
name: 'auth',
value: ''
}, {
enabled: false,
name: 'Content-type',
value: 'application/json'
}]
variables: [
{
enabled: true,
name: 'host',
value: 'https://www.google.com'
},
{
enabled: true,
name: 'auth',
value: ''
},
{
enabled: false,
name: 'Content-type',
value: 'application/json'
}
]
});
});
});

View File

@ -1,9 +1,4 @@
const {
safeParseJson,
indentString,
outdentString,
get
} = require('../src/utils');
const { safeParseJson, indentString, outdentString, get } = require('../src/utils');
describe('utils', () => {
describe('safeParseJson', () => {
@ -22,16 +17,16 @@ describe('utils', () => {
describe('indentString', () => {
it('correctly indents a multiline string', () => {
const input = "line1\nline2\nline3";
const expectedOutput = " line1\n line2\n line3";
const input = 'line1\nline2\nline3';
const expectedOutput = ' line1\n line2\n line3';
expect(indentString(input)).toBe(expectedOutput);
});
});
describe('outdentString', () => {
it('correctly outdents a multiline string', () => {
const input = " line1\n line2\n line3";
const expectedOutput = "line1\nline2\nline3";
const input = ' line1\n line2\n line3';
const expectedOutput = 'line1\nline2\nline3';
expect(outdentString(input)).toBe(expectedOutput);
});
});

View File

@ -1,8 +1,6 @@
const ohm = require("ohm-js");
const ohm = require('ohm-js');
const _ = require('lodash');
const {
outdentString
} = require('../../v1/src/utils');
const { outdentString } = require('../../v1/src/utils');
/**
* A Bru file is made up of blocks.
@ -95,14 +93,14 @@ const grammar = ohm.grammar(`Bru {
}`);
const mapPairListToKeyValPairs = (pairList = []) => {
if(!pairList.length) {
if (!pairList.length) {
return [];
}
return _.map(pairList[0], pair => {
return _.map(pairList[0], (pair) => {
let name = _.keys(pair)[0];
let value = pair[name];
let enabled = true;
if (name && name.length && name.charAt(0) === "~") {
if (name && name.length && name.charAt(0) === '~') {
name = name.slice(1);
enabled = false;
}
@ -122,22 +120,26 @@ const concatArrays = (objValue, srcValue) => {
};
const mapPairListToKeyValPair = (pairList = []) => {
if(!pairList || !pairList.length) {
if (!pairList || !pairList.length) {
return {};
}
return _.merge({}, ...pairList[0]);
}
};
const sem = grammar.createSemantics().addAttribute('ast', {
BruFile(tags) {
if(!tags || !tags.ast || !tags.ast.length) {
if (!tags || !tags.ast || !tags.ast.length) {
return {};
}
return _.reduce(tags.ast, (result, item) => {
return _.mergeWith(result, item, concatArrays);
}, {});
return _.reduce(
tags.ast,
(result, item) => {
return _.mergeWith(result, item, concatArrays);
},
{}
);
},
dictionary(_1, _2, pairlist, _3) {
return pairlist.ast;
@ -185,20 +187,20 @@ const sem = grammar.createSemantics().addAttribute('ast', {
st(_) {
return '';
},
tagend(_1 ,_2) {
tagend(_1, _2) {
return '';
},
_iter(...elements) {
return elements.map(e => e.ast);
return elements.map((e) => e.ast);
},
meta(_1, dictionary) {
let meta = mapPairListToKeyValPair(dictionary.ast);
if(!meta.seq) {
if (!meta.seq) {
meta.seq = 1;
}
if(!meta.type) {
if (!meta.type) {
meta.type = 'http';
}
@ -347,7 +349,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
const vars = mapPairListToKeyValPairs(dictionary.ast);
_.each(vars, (v) => {
let name = v.name;
if (name && name.length && name.charAt(0) === "@") {
if (name && name.length && name.charAt(0) === '@') {
v.name = name.slice(1);
v.local = true;
} else {
@ -365,7 +367,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
const vars = mapPairListToKeyValPairs(dictionary.ast);
_.each(vars, (v) => {
let name = v.name;
if (name && name.length && name.charAt(0) === "@") {
if (name && name.length && name.charAt(0) === '@') {
v.name = name.slice(1);
v.local = true;
} else {
@ -401,7 +403,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
tests(_1, _2, _3, _4, textblock, _5) {
return {
tests: outdentString(textblock.sourceString)
};;
};
},
docs(_1, _2, _3, _4, textblock, _5) {
return {
@ -413,11 +415,11 @@ const sem = grammar.createSemantics().addAttribute('ast', {
const parser = (input) => {
const match = grammar.match(input);
if(match.succeeded()) {
if (match.succeeded()) {
return sem(match).ast;
} else {
throw new Error(match.message);
}
}
};
module.exports = parser;

View File

@ -1,4 +1,4 @@
const ohm = require("ohm-js");
const ohm = require('ohm-js');
const _ = require('lodash');
const grammar = ohm.grammar(`Env {
@ -20,9 +20,13 @@ const concatArrays = (objValue, srcValue) => {
const sem = grammar.createSemantics().addAttribute('ast', {
EnvFile(entries) {
return _.reduce(entries.ast, (result, item) => {
return _.mergeWith(result, item, concatArrays);
}, {});
return _.reduce(
entries.ast,
(result, item) => {
return _.mergeWith(result, item, concatArrays);
},
{}
);
},
entry(_1, key, _2, _3, _4, value, _5, _6) {
return { [key.ast.trim()]: value.ast.trim() };
@ -40,7 +44,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
return '';
},
_iter(...elements) {
return elements.map(e => e.ast);
return elements.map((e) => e.ast);
}
});
@ -53,7 +57,7 @@ const parser = (input) => {
} else {
throw new Error(match.message);
}
}
};
function postProcessEntries(ast) {
const processed = {};

View File

@ -1,4 +1,4 @@
const ohm = require("ohm-js");
const ohm = require('ohm-js');
const _ = require('lodash');
const grammar = ohm.grammar(`Bru {
@ -23,15 +23,15 @@ const grammar = ohm.grammar(`Bru {
}`);
const mapPairListToKeyValPairs = (pairList = []) => {
if(!pairList.length) {
if (!pairList.length) {
return [];
}
return _.map(pairList[0], pair => {
return _.map(pairList[0], (pair) => {
let name = _.keys(pair)[0];
let value = pair[name];
let enabled = true;
if (name && name.length && name.charAt(0) === "~") {
if (name && name.length && name.charAt(0) === '~') {
name = name.slice(1);
enabled = false;
}
@ -52,15 +52,19 @@ const concatArrays = (objValue, srcValue) => {
const sem = grammar.createSemantics().addAttribute('ast', {
BruEnvFile(tags) {
if(!tags || !tags.ast || !tags.ast.length) {
if (!tags || !tags.ast || !tags.ast.length) {
return {
variables: []
};
}
return _.reduce(tags.ast, (result, item) => {
return _.mergeWith(result, item, concatArrays);
}, {});
return _.reduce(
tags.ast,
(result, item) => {
return _.mergeWith(result, item, concatArrays);
},
{}
);
},
dictionary(_1, _2, pairlist, _3) {
return pairlist.ast;
@ -85,11 +89,11 @@ const sem = grammar.createSemantics().addAttribute('ast', {
st(_) {
return '';
},
tagend(_1 ,_2) {
tagend(_1, _2) {
return '';
},
_iter(...elements) {
return elements.map(e => e.ast);
return elements.map((e) => e.ast);
},
vars(_1, dictionary) {
const vars = mapPairListToKeyValPairs(dictionary.ast);
@ -102,11 +106,11 @@ const sem = grammar.createSemantics().addAttribute('ast', {
const parser = (input) => {
const match = grammar.match(input);
if(match.succeeded()) {
if (match.succeeded()) {
return sem(match).ast;
} else {
throw new Error(match.message);
}
}
};
module.exports = parser;

View File

@ -1,36 +1,23 @@
const _ = require('lodash');
const {
indentString,
} = require('../../v1/src/utils');
const { indentString } = require('../../v1/src/utils');
const enabled = (items = []) => items.filter(item => item.enabled);
const disabled = (items = []) => items.filter(item => !item.enabled);
const enabled = (items = []) => items.filter((item) => item.enabled);
const disabled = (items = []) => items.filter((item) => !item.enabled);
// remove the last line if two new lines are found
const stripLastLine = (text) => {
if(!text || !text.length) return text;
if (!text || !text.length) return text;
return text.replace(/(\r?\n)$/, '');
};
const jsonToBru = (json) => {
const {
meta,
http,
query,
headers,
body,
script,
tests,
vars,
assertions,
docs
} = json;
const { meta, http, query, headers, body, script, tests, vars, assertions, docs } = json;
let bru = '';
if(meta) {
if (meta) {
bru += 'meta {\n';
for (const key in meta) {
bru += ` ${key}: ${meta[key]}\n`;
@ -38,11 +25,11 @@ const jsonToBru = (json) => {
bru += '}\n\n';
}
if(http && http.method) {
if (http && http.method) {
bru += `${http.method} {
url: ${http.url}`;
if(http.body && http.body.length) {
if (http.body && http.body.length) {
bru += `
body: ${http.body}`;
}
@ -53,97 +40,129 @@ const jsonToBru = (json) => {
`;
}
if(query && query.length) {
if (query && query.length) {
bru += 'query {';
if(enabled(query).length) {
bru += `\n${indentString(enabled(query).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (enabled(query).length) {
bru += `\n${indentString(
enabled(query)
.map((item) => `${item.name}: ${item.value}`)
.join('\n')
)}`;
}
if(disabled(query).length) {
bru += `\n${indentString(disabled(query).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (disabled(query).length) {
bru += `\n${indentString(
disabled(query)
.map((item) => `~${item.name}: ${item.value}`)
.join('\n')
)}`;
}
bru += '\n}\n\n';
}
if(headers && headers.length) {
if (headers && headers.length) {
bru += 'headers {';
if(enabled(headers).length) {
bru += `\n${indentString(enabled(headers).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (enabled(headers).length) {
bru += `\n${indentString(
enabled(headers)
.map((item) => `${item.name}: ${item.value}`)
.join('\n')
)}`;
}
if(disabled(headers).length) {
bru += `\n${indentString(disabled(headers).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (disabled(headers).length) {
bru += `\n${indentString(
disabled(headers)
.map((item) => `~${item.name}: ${item.value}`)
.join('\n')
)}`;
}
bru += '\n}\n\n';
}
if(body && body.json && body.json.length) {
bru += `body:json {
if (body && body.json && body.json.length) {
bru += `body:json {
${indentString(body.json)}
}
`;
}
if(body && body.text && body.text.length) {
bru += `body:text {
if (body && body.text && body.text.length) {
bru += `body:text {
${indentString(body.text)}
}
`;
}
if(body && body.xml && body.xml.length) {
bru += `body:xml {
if (body && body.xml && body.xml.length) {
bru += `body:xml {
${indentString(body.xml)}
}
`;
}
if(body && body.formUrlEncoded && body.formUrlEncoded.length) {
if (body && body.formUrlEncoded && body.formUrlEncoded.length) {
bru += `body:form-urlencoded {`;
if(enabled(body.formUrlEncoded).length) {
bru += `\n${indentString(enabled(body.formUrlEncoded).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (enabled(body.formUrlEncoded).length) {
bru += `\n${indentString(
enabled(body.formUrlEncoded)
.map((item) => `${item.name}: ${item.value}`)
.join('\n')
)}`;
}
if(disabled(body.formUrlEncoded).length) {
bru += `\n${indentString(disabled(body.formUrlEncoded).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (disabled(body.formUrlEncoded).length) {
bru += `\n${indentString(
disabled(body.formUrlEncoded)
.map((item) => `~${item.name}: ${item.value}`)
.join('\n')
)}`;
}
bru += '\n}\n\n';
}
if(body && body.multipartForm && body.multipartForm.length) {
if (body && body.multipartForm && body.multipartForm.length) {
bru += `body:multipart-form {`;
if(enabled(body.multipartForm).length) {
bru += `\n${indentString(enabled(body.multipartForm).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (enabled(body.multipartForm).length) {
bru += `\n${indentString(
enabled(body.multipartForm)
.map((item) => `${item.name}: ${item.value}`)
.join('\n')
)}`;
}
if(disabled(body.multipartForm).length) {
bru += `\n${indentString(disabled(body.multipartForm).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (disabled(body.multipartForm).length) {
bru += `\n${indentString(
disabled(body.multipartForm)
.map((item) => `~${item.name}: ${item.value}`)
.join('\n')
)}`;
}
bru += '\n}\n\n';
}
if(body && body.graphql && body.graphql.query) {
if (body && body.graphql && body.graphql.query) {
bru += `body:graphql {\n`;
bru += `${indentString(body.graphql.query)}`;
bru += '\n}\n\n';
}
if(body && body.graphql && body.graphql.variables) {
if (body && body.graphql && body.graphql.variables) {
bru += `body:graphql:vars {\n`;
bru += `${indentString(body.graphql.variables)}`
bru += `${indentString(body.graphql.variables)}`;
bru += '\n}\n\n';
}
let reqvars = _.get(vars, 'req');
let resvars = _.get(vars, 'res');
if(reqvars && reqvars.length) {
if (reqvars && reqvars.length) {
const varsEnabled = _.filter(reqvars, (v) => v.enabled && !v.local);
const varsDisabled = _.filter(reqvars, (v) => !v.enabled && !v.local);
const varsLocalEnabled = _.filter(reqvars, (v) => v.enabled && v.local);
@ -151,25 +170,25 @@ ${indentString(body.xml)}
bru += `vars:pre-request {`;
if(varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map((item) => `${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalEnabled.length) {
bru += `\n${indentString(varsLocalEnabled.map(item => `@${item.name}: ${item.value}`).join('\n'))}`;
if (varsLocalEnabled.length) {
bru += `\n${indentString(varsLocalEnabled.map((item) => `@${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsDisabled.length) {
bru += `\n${indentString(varsDisabled.map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (varsDisabled.length) {
bru += `\n${indentString(varsDisabled.map((item) => `~${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalDisabled.length) {
bru += `\n${indentString(varsLocalDisabled.map(item => `~@${item.name}: ${item.value}`).join('\n'))}`;
if (varsLocalDisabled.length) {
bru += `\n${indentString(varsLocalDisabled.map((item) => `~@${item.name}: ${item.value}`).join('\n'))}`;
}
bru += '\n}\n\n';
}
if(resvars && resvars.length) {
if (resvars && resvars.length) {
const varsEnabled = _.filter(resvars, (v) => v.enabled && !v.local);
const varsDisabled = _.filter(resvars, (v) => !v.enabled && !v.local);
const varsLocalEnabled = _.filter(resvars, (v) => v.enabled && v.local);
@ -177,65 +196,73 @@ ${indentString(body.xml)}
bru += `vars:post-response {`;
if(varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map((item) => `${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalEnabled.length) {
bru += `\n${indentString(varsLocalEnabled.map(item => `@${item.name}: ${item.value}`).join('\n'))}`;
if (varsLocalEnabled.length) {
bru += `\n${indentString(varsLocalEnabled.map((item) => `@${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsDisabled.length) {
bru += `\n${indentString(varsDisabled.map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (varsDisabled.length) {
bru += `\n${indentString(varsDisabled.map((item) => `~${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalDisabled.length) {
bru += `\n${indentString(varsLocalDisabled.map(item => `~@${item.name}: ${item.value}`).join('\n'))}`;
if (varsLocalDisabled.length) {
bru += `\n${indentString(varsLocalDisabled.map((item) => `~@${item.name}: ${item.value}`).join('\n'))}`;
}
bru += '\n}\n\n';
}
if(assertions && assertions.length) {
if (assertions && assertions.length) {
bru += `assert {`;
if(enabled(assertions).length) {
bru += `\n${indentString(enabled(assertions).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
if (enabled(assertions).length) {
bru += `\n${indentString(
enabled(assertions)
.map((item) => `${item.name}: ${item.value}`)
.join('\n')
)}`;
}
if(disabled(assertions).length) {
bru += `\n${indentString(disabled(assertions).map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
if (disabled(assertions).length) {
bru += `\n${indentString(
disabled(assertions)
.map((item) => `~${item.name}: ${item.value}`)
.join('\n')
)}`;
}
bru += '\n}\n\n';
}
if(script && script.req && script.req.length) {
bru += `script:pre-request {
if (script && script.req && script.req.length) {
bru += `script:pre-request {
${indentString(script.req)}
}
`;
}
if(script && script.res && script.res.length) {
bru += `script:post-response {
if (script && script.res && script.res.length) {
bru += `script:post-response {
${indentString(script.res)}
}
`;
}
if(tests && tests.length) {
bru += `tests {
if (tests && tests.length) {
bru += `tests {
${indentString(tests)}
}
`;
}
if(docs && docs.length) {
bru += `docs {
if (docs && docs.length) {
bru += `docs {
${indentString(docs)}
}

View File

@ -8,7 +8,7 @@ const envToJson = (json) => {
return ` ${prefix}${name}: ${value}`;
});
if(!vars || !vars.length) {
if (!vars || !vars.length) {
return `vars {
}
`;

View File

@ -1,10 +1,10 @@
/**
* This test file is used to test the text parser.
*/
const parser = require("../src/bruToJson");
const parser = require('../src/bruToJson');
describe("assert parser", () => {
it("should parse assert statement", () => {
describe('assert parser', () => {
it('should parse assert statement', () => {
const input = `
assert {
res("data.airports").filter(a => a.code ==="BLR").name: "Bangalore International Airport"
@ -13,11 +13,13 @@ assert {
const output = parser(input);
const expected = {
"assertions": [{
name: "res(\"data.airports\").filter(a => a.code ===\"BLR\").name",
value: '"Bangalore International Airport"',
enabled: true
}]
assertions: [
{
name: 'res("data.airports").filter(a => a.code ==="BLR").name',
value: '"Bangalore International Airport"',
enabled: true
}
]
};
expect(output).toEqual(expected);
});

View File

@ -1,7 +1,7 @@
const bruToJson = require("../src/bruToJson");
const bruToJson = require('../src/bruToJson');
describe("defaults", () => {
it("should parse the default type and seq", () => {
describe('defaults', () => {
it('should parse the default type and seq', () => {
const input = `
meta {
name: Create user
@ -12,21 +12,21 @@ post {
}
`;
const expected = {
"meta": {
"name": "Create user",
"seq": 1,
"type": "http"
meta: {
name: 'Create user',
seq: 1,
type: 'http'
},
"http": {
"method": "post",
"url": "/users"
http: {
method: 'post',
url: '/users'
}
};
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it("should parse the default body mode as json if the body is found", () => {
it('should parse the default body mode as json if the body is found', () => {
const input = `
meta {
name: Create user
@ -45,18 +45,18 @@ body {
`;
const expected = {
"meta": {
"name": "Create user",
"seq": 1,
"type": "http"
meta: {
name: 'Create user',
seq: 1,
type: 'http'
},
"http": {
"method": "post",
"url": "/users",
"body": "json"
http: {
method: 'post',
url: '/users',
body: 'json'
},
"body": {
"json": "{\n name: John\n age: 30\n}"
body: {
json: '{\n name: John\n age: 30\n}'
}
};

View File

@ -2,35 +2,37 @@
* This test file is used to test the dictionary parser.
*/
const parser = require("../src/bruToJson");
const parser = require('../src/bruToJson');
const assertSingleHeader = (input) => {
const output = parser(input);
const expected = {
"headers": [{
"name": "hello",
"value": "world",
"enabled": true
}]
headers: [
{
name: 'hello',
value: 'world',
enabled: true
}
]
};
expect(output).toEqual(expected);
};
describe("headers parser", () => {
it("should parse empty header", () => {
describe('headers parser', () => {
it('should parse empty header', () => {
const input = `
headers {
}`;
const output = parser(input);
const expected = {
"headers": []
headers: []
};
expect(output).toEqual(expected);
});
it("should parse single header", () => {
it('should parse single header', () => {
const input = `
headers {
hello: world
@ -39,7 +41,7 @@ headers {
assertSingleHeader(input);
});
it("should parse single header with spaces", () => {
it('should parse single header with spaces', () => {
const input = `
headers {
hello: world
@ -48,7 +50,7 @@ headers {
assertSingleHeader(input);
});
it("should parse single header with spaces and newlines", () => {
it('should parse single header with spaces and newlines', () => {
const input = `
headers {
@ -60,7 +62,7 @@ headers {
assertSingleHeader(input);
});
it("should parse single header with empty value", () => {
it('should parse single header with empty value', () => {
const input = `
headers {
hello:
@ -68,16 +70,18 @@ headers {
const output = parser(input);
const expected = {
"headers": [{
"name": "hello",
"value": "",
"enabled": true
}]
headers: [
{
name: 'hello',
value: '',
enabled: true
}
]
};
expect(output).toEqual(expected);
});
it("should parse multi headers", () => {
it('should parse multi headers', () => {
const input = `
headers {
content-type: application/json
@ -87,20 +91,23 @@ headers {
const output = parser(input);
const expected = {
"headers": [{
"name": "content-type",
"value": "application/json",
"enabled": true
}, {
"name": "Authorization",
"value": "JWT secret",
"enabled": true
}]
headers: [
{
name: 'content-type',
value: 'application/json',
enabled: true
},
{
name: 'Authorization',
value: 'JWT secret',
enabled: true
}
]
};
expect(output).toEqual(expected);
});
it("should parse disabled headers", () => {
it('should parse disabled headers', () => {
const input = `
headers {
~content-type: application/json
@ -108,16 +115,18 @@ headers {
const output = parser(input);
const expected = {
"headers": [{
"name": "content-type",
"value": "application/json",
"enabled": false
}]
headers: [
{
name: 'content-type',
value: 'application/json',
enabled: false
}
]
};
expect(output).toEqual(expected);
});
it("should parse empty url", () => {
it('should parse empty url', () => {
const input = `
get {
url:
@ -126,16 +135,16 @@ get {
const output = parser(input);
const expected = {
"http": {
"url": "",
"method": "get",
"body": "json",
http: {
url: '',
method: 'get',
body: 'json'
}
};
expect(output).toEqual(expected);
});
it("should throw error on invalid header", () => {
it('should throw error on invalid header', () => {
const input = `
headers {
hello: world
@ -145,7 +154,7 @@ headers {
expect(() => parser(input)).toThrow();
});
it("should throw error on invalid header", () => {
it('should throw error on invalid header', () => {
const input = `
headers {
hello: world
@ -154,4 +163,3 @@ headers {
expect(() => parser(input)).toThrow();
});
});

View File

@ -27,7 +27,7 @@ BEEP=false
const expected = {
FOO: 'bar',
BAZ: 2,
BEEP: false,
BEEP: false
};
const output = parser(input);
expect(output).toEqual(expected);

View File

@ -1,20 +1,20 @@
const parser = require("../src/envToJson");
const parser = require('../src/envToJson');
describe("env parser", () => {
it("should parse empty vars", () => {
describe('env parser', () => {
it('should parse empty vars', () => {
const input = `
vars {
}`;
const output = parser(input);
const expected = {
"variables": []
variables: []
};
expect(output).toEqual(expected);
});
it("should parse single var line", () => {
it('should parse single var line', () => {
const input = `
vars {
url: http://localhost:3000
@ -22,17 +22,19 @@ vars {
const output = parser(input);
const expected = {
"variables": [{
"name": "url",
"value": "http://localhost:3000",
"enabled" : true,
}]
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
}
]
};
expect(output).toEqual(expected);
});
it("should parse multiple var lines", () => {
it('should parse multiple var lines', () => {
const input = `
vars {
url: http://localhost:3000
@ -42,25 +44,29 @@ vars {
const output = parser(input);
const expected = {
"variables": [{
"name": "url",
"value": "http://localhost:3000",
"enabled" : true
}, {
"name": "port",
"value": "3000",
"enabled" : true
}, {
"name": "token",
"value": "secret",
"enabled" : false
}]
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
},
{
name: 'port',
value: '3000',
enabled: true
},
{
name: 'token',
value: 'secret',
enabled: false
}
]
};
expect(output).toEqual(expected);
});
it("should gracefully handle empty lines and spaces", () => {
it('should gracefully handle empty lines and spaces', () => {
const input = `
vars {
@ -72,21 +78,24 @@ vars {
const output = parser(input);
const expected = {
"variables": [{
"name": "url",
"value": "http://localhost:3000",
"enabled" : true,
}, {
"name": "port",
"value": "3000",
"enabled" : true,
}]
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
},
{
name: 'port',
value: '3000',
enabled: true
}
]
};
expect(output).toEqual(expected);
});
it("should parse vars with empty values", () => {
it('should parse vars with empty values', () => {
const input = `
vars {
url:
@ -97,19 +106,23 @@ vars {
const output = parser(input);
const expected = {
"variables": [{
"name": "url",
"value": "",
"enabled" : true,
}, {
"name": "phone",
"value": "",
"enabled" : true,
}, {
"name": "api-key",
"value": "",
"enabled" : true,
}]
variables: [
{
name: 'url',
value: '',
enabled: true
},
{
name: 'phone',
value: '',
enabled: true
},
{
name: 'api-key',
value: '',
enabled: true
}
]
};
expect(output).toEqual(expected);

View File

@ -9,19 +9,23 @@
"url": "https://api.textlocal.in/send",
"body": "json"
},
"query": [{
"name": "apiKey",
"value": "secret",
"enabled": true
}, {
"name": "numbers",
"value": "998877665",
"enabled": true
}, {
"name": "message",
"value": "hello",
"enabled": false
}],
"query": [
{
"name": "apiKey",
"value": "secret",
"enabled": true
},
{
"name": "numbers",
"value": "998877665",
"enabled": true
},
{
"name": "message",
"value": "hello",
"enabled": false
}
],
"headers": [
{
"name": "content-type",
@ -66,7 +70,7 @@
],
"multipartForm": [
{
"name": "apikey",
"name": "apikey",
"value": "secret",
"enabled": true
},

View File

@ -1,21 +1,21 @@
const fs = require("fs");
const path = require("path");
const bruToJson = require("../src/bruToJson");
const jsonToBru = require("../src/jsonToBru");
const fs = require('fs');
const path = require('path');
const bruToJson = require('../src/bruToJson');
const jsonToBru = require('../src/jsonToBru');
describe("bruToJson", () => {
it("should parse the bru file", () => {
describe('bruToJson', () => {
it('should parse the bru file', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const expected = require("./fixtures/request.json");
const expected = require('./fixtures/request.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
});
describe("jsonToBru", () => {
it("should parse the bru file", () => {
const input = require("./fixtures/request.json");
describe('jsonToBru', () => {
it('should parse the bru file', () => {
const input = require('./fixtures/request.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'request.bru'), 'utf8');
const output = jsonToBru(input);

View File

@ -1,9 +1,9 @@
const parser = require("../src/jsonToEnv");
const parser = require('../src/jsonToEnv');
describe("env parser", () => {
it("should parse empty vars", () => {
describe('env parser', () => {
it('should parse empty vars', () => {
const input = {
"variables": []
variables: []
};
const output = parser(input);
@ -14,13 +14,15 @@ describe("env parser", () => {
expect(output).toEqual(expected);
});
it("should parse single var line", () => {
it('should parse single var line', () => {
const input = {
"variables": [{
"name": "url",
"value": "http://localhost:3000",
"enabled" : true,
}]
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
}
]
};
const output = parser(input);
@ -31,17 +33,20 @@ describe("env parser", () => {
expect(output).toEqual(expected);
});
it("should parse multiple var lines", () => {
it('should parse multiple var lines', () => {
const input = {
"variables": [{
"name": "url",
"value": "http://localhost:3000",
"enabled" : true
}, {
"name": "port",
"value": "3000",
"enabled" : false
}]
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
},
{
name: 'port',
value: '3000',
enabled: false
}
]
};
const expected = `vars {

View File

@ -1,10 +1,10 @@
/**
* This test file is used to test the text parser.
*/
const parser = require("../src/bruToJson");
const parser = require('../src/bruToJson');
describe("script parser", () => {
it("should parse request script", () => {
describe('script parser', () => {
it('should parse request script', () => {
const input = `
script:pre-request {
$req.setHeader('Content-Type', 'application/json');
@ -13,14 +13,14 @@ script:pre-request {
const output = parser(input);
const expected = {
"script": {
"req": "$req.setHeader('Content-Type', 'application/json');"
script: {
req: "$req.setHeader('Content-Type', 'application/json');"
}
};
expect(output).toEqual(expected);
});
it("should parse response script", () => {
it('should parse response script', () => {
const input = `
script:post-response {
expect(response.status).to.equal(200);
@ -29,8 +29,8 @@ script:post-response {
const output = parser(input);
const expected = {
"script": {
"res": "expect(response.status).to.equal(200);"
script: {
res: 'expect(response.status).to.equal(200);'
}
};
expect(output).toEqual(expected);