diff --git a/packages/bruno-lang/src/body-tag.js b/packages/bruno-lang/src/body-tag.js index 22824b5cd..7910ca6fb 100644 --- a/packages/bruno-lang/src/body-tag.js +++ b/packages/bruno-lang/src/body-tag.js @@ -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 }; }); diff --git a/packages/bruno-lang/src/headers-tag.js b/packages/bruno-lang/src/headers-tag.js index 0dd7a617a..e9d1f3947 100644 --- a/packages/bruno-lang/src/headers-tag.js +++ b/packages/bruno-lang/src/headers-tag.js @@ -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 }; }); diff --git a/packages/bruno-lang/src/index.js b/packages/bruno-lang/src/index.js index 01a25785d..14cac214e 100644 --- a/packages/bruno-lang/src/index.js +++ b/packages/bruno-lang/src/index.js @@ -41,41 +41,42 @@ const bruToJson = (fileContents) => { .reduce((acc, item) => _.merge(acc, item), {}); return { - ver: parsed.ver, type: parsed.type || '', name: parsed.name || '', - method: parsed.method || '', - url: parsed.url || '', - params: parsed.params || [], - headers: parsed.headers || [], - body: parsed.body || {mode: 'none'} + 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, - method, - url, - params, - headers, - body + 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 `; } diff --git a/packages/bruno-lang/src/inline-tag.js b/packages/bruno-lang/src/inline-tag.js index cb7be2d7f..a16f3338d 100644 --- a/packages/bruno-lang/src/inline-tag.js +++ b/packages/bruno-lang/src/inline-tag.js @@ -13,7 +13,6 @@ const newLineOrEndOfInput = choice([endOfInput, newline]); const inlineTag = sequenceOf([ choice([ - str('ver'), str('type'), str('name'), str('method'), diff --git a/packages/bruno-lang/src/params-tag.js b/packages/bruno-lang/src/params-tag.js index d2cdbb02a..ac025bafd 100644 --- a/packages/bruno-lang/src/params-tag.js +++ b/packages/bruno-lang/src/params-tag.js @@ -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 }; }); diff --git a/packages/bruno-lang/tests/bru-to-json.spec.js b/packages/bruno-lang/tests/bru-to-json.spec.js index 25dc257d9..fb7e69d8b 100644 --- a/packages/bruno-lang/tests/bru-to-json.spec.js +++ b/packages/bruno-lang/tests/bru-to-json.spec.js @@ -2,90 +2,89 @@ 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", - "method": "GET", - "url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010", - "params": [ - { - "enabled": "1", - "key": "apiKey", - "value": "secret" - }, - { - "enabled": "1", - "key": "numbers", - "value": "998877665" - }, - { - "enabled": "1", - "key": "message", - "value": "hello" - } - ], - "headers": [ - { - "enabled": "1", - "key": "content-type", - "value": "application/json" - }, - { - "enabled": "1", - "key": "accept-language", - "value": "en-US,en;q=0.9,hi;q=0.8" - }, - { - "enabled": "0", - "key": "transaction-id", - "value": "{{transactionId}}" - } - ], - "body": { - "mode": "json", - "json": '{"apikey":"secret","numbers":"+91998877665"}', - "graphql": { - "query": "{\n launchesPast {\n launch_success\n }\n}" - }, - "text": "Hello, there. You must be from the past", - "xml": "back to the ice age", - "formUrlEncoded": [ + "request": { + "method": "GET", + "url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010", + "params": [ { - "enabled": "1", - "key": "username", - "value": "john" + "enabled": true, + "name": "apiKey", + "value": "secret" }, { - "enabled": "0", - "key": "password", - "value": "{{password}}" + "enabled": true, + "name": "numbers", + "value": "998877665" + }, + { + "enabled": true, + "name": "message", + "value": "hello" } ], - "multipartForm": [ + "headers": [ { - "enabled": "1", - "key": "username", - "value": "nash" + "enabled": true, + "name": "content-type", + "value": "application/json" }, { - "enabled": "0", - "key": "password", - "value": "governingdynamics" + "enabled": true, + "name": "accept-language", + "value": "en-US,en;q=0.9,hi;q=0.8" + }, + { + "enabled": false, + "name": "transaction-id", + "value": "{{transactionId}}" } - ] + ], + "body": { + "mode": "json", + "json": '{"apikey":"secret","numbers":"+91998877665"}', + "graphql": { + "query": " {\n launchesPast {\n launch_success\n }\n }" + }, + "text": " Hello, there. You must be from the past", + "xml": " back to the ice age", + "formUrlEncoded": [ + { + "enabled": true, + "name": "username", + "value": "john" + }, + { + "enabled": false, + "name": "password", + "value": "{{password}}" + } + ], + "multipartForm": [ + { + "enabled": true, + "name": "username", + "value": "nash" + }, + { + "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); + }); }); }); diff --git a/packages/bruno-lang/tests/fixtures/request.bru b/packages/bruno-lang/tests/fixtures/request.bru index 2e47885b0..9dba34f64 100644 --- a/packages/bruno-lang/tests/fixtures/request.bru +++ b/packages/bruno-lang/tests/fixtures/request.bru @@ -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 diff --git a/packages/bruno-lang/tests/inline-tag.spec.js b/packages/bruno-lang/tests/inline-tag.spec.js index 334568cb1..bf98a7db3 100644 --- a/packages/bruno-lang/tests/inline-tag.spec.js +++ b/packages/bruno-lang/tests/inline-tag.spec.js @@ -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' }], diff --git a/packages/bruno-lang/tests/json-to-bru.spec.js b/packages/bruno-lang/tests/json-to-bru.spec.js index 4c7cb8309..b28530ca5 100644 --- a/packages/bruno-lang/tests/json-to-bru.spec.js +++ b/packages/bruno-lang/tests/json-to-bru.spec.js @@ -2,88 +2,91 @@ 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", - "method": "GET", - "url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010", - "params": [ - { - "enabled": "1", - "key": "apiKey", - "value": "secret" - }, - { - "enabled": "1", - "key": "numbers", - "value": "998877665" - }, - { - "enabled": "1", - "key": "message", - "value": "hello" - } - ], - "headers": [ - { - "enabled": "1", - "key": "content-type", - "value": "application/json" - }, - { - "enabled": "1", - "key": "accept-language", - "value": "en-US,en;q=0.9,hi;q=0.8" - }, - { - "enabled": "0", - "key": "transaction-id", - "value": "{{transactionId}}" - } - ], - "body": { - "mode": "json", - "json": '{"apikey":"secret","numbers":"+91998877665"}', - "graphql": { - "query": " {\n launchesPast {\n launch_success\n }\n }" - }, - "text": " Hello, there. You must be from the past", - "xml": " back to the ice age", - "formUrlEncoded": [ + "request": { + "method": "GET", + "url": "https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010", + "params": [ { - "enabled": "1", - "key": "username", - "value": "john" + "enabled": true, + "name": "apiKey", + "value": "secret" }, { - "enabled": "0", - "key": "password", - "value": "{{password}}" + "enabled": true, + "name": "numbers", + "value": "998877665" + }, + { + "enabled": true, + "name": "message", + "value": "hello" } ], - "multipartForm": [ + "headers": [ { - "enabled": "1", - "key": "username", - "value": "nash" + "enabled": true, + "name": "content-type", + "value": "application/json" }, { - "enabled": "0", - "key": "password", - "value": "governingdynamics" + "enabled": true, + "name": "accept-language", + "value": "en-US,en;q=0.9,hi;q=0.8" + }, + { + "enabled": false, + "name": "transaction-id", + "value": "{{transactionId}}" } - ] + ], + "body": { + "mode": "json", + "json": '{"apikey":"secret","numbers":"+91998877665"}', + "graphql": { + "query": "{\n launchesPast {\n launch_success\n }\n}" + }, + "text": "Hello, there. You must be from the past", + "xml": "back to the ice age", + "formUrlEncoded": [ + { + "enabled": true, + "name": "username", + "value": "john" + }, + { + "enabled": false, + "name": "password", + "value": "{{password}}" + } + ], + "multipartForm": [ + { + "enabled": true, + "name": "username", + "value": "nash" + }, + { + "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); }); });