From abb24c93c5a15c9b5bf7ffee4df1afc3838d2245 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sun, 31 Dec 2023 02:52:41 +0530 Subject: [PATCH] feat(#1303): toml parser for scripts and tests --- packages/bruno-toml/lib/stringify | 2 +- packages/bruno-toml/src/jsonToToml.js | 36 +++++++++++++++++-- packages/bruno-toml/src/tomlToJson.js | 32 ++++++++++++++--- packages/bruno-toml/tests/index.spec.js | 7 ++-- .../tests/scripts/post-response/request.json | 14 ++++++++ .../tests/scripts/post-response/request.toml | 14 ++++++++ .../tests/scripts/pre-request/request.json | 14 ++++++++ .../tests/scripts/pre-request/request.toml | 13 +++++++ .../tests/scripts/tests/request.json | 12 +++++++ .../tests/scripts/tests/request.toml | 15 ++++++++ 10 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 packages/bruno-toml/tests/scripts/post-response/request.json create mode 100644 packages/bruno-toml/tests/scripts/post-response/request.toml create mode 100644 packages/bruno-toml/tests/scripts/pre-request/request.json create mode 100644 packages/bruno-toml/tests/scripts/pre-request/request.toml create mode 100644 packages/bruno-toml/tests/scripts/tests/request.json create mode 100644 packages/bruno-toml/tests/scripts/tests/request.toml diff --git a/packages/bruno-toml/lib/stringify b/packages/bruno-toml/lib/stringify index 116680f68..0c58286a3 100644 --- a/packages/bruno-toml/lib/stringify +++ b/packages/bruno-toml/lib/stringify @@ -177,7 +177,7 @@ function stringifyMultilineString (str) { return escapeString(str).replace(/'(?='')/g, "\\'") }).join('\n') if (escaped.slice(-1) === "'") escaped += '\\\n' - return "'''\n" + escaped + "\n'''" + return "'''\n" + escaped + "'''" } function stringifyAnyInline (value, multilineOk) { diff --git a/packages/bruno-toml/src/jsonToToml.js b/packages/bruno-toml/src/jsonToToml.js index b998ddc7b..c61191ad8 100644 --- a/packages/bruno-toml/src/jsonToToml.js +++ b/packages/bruno-toml/src/jsonToToml.js @@ -18,12 +18,22 @@ const keyValPairHasReservedKeys = (keyValPair) => { return false; } - const reservedKeys = ['disabled', 'description', 'enum']; + const reservedKeys = ['disabled', 'description', 'enum', 'bru']; const names = keyValPair.map((pair) => pair.name); return names.some((name) => reservedKeys.includes(name)); }; +/** + * Json to Toml + * + * Note: Bruno always append a new line at the end of text blocks + * This is to aid readability when viewing the toml representation of the request + * The newline is removed when converting back to json + * + * @param {object} json + * @returns string + */ const jsonToToml = (json) => { const formattedJson = { meta: { @@ -55,11 +65,33 @@ const jsonToToml = (json) => { }); } else { formattedJson.headers = { - bru: JSON.stringify(json.headers, null, 2) + bru: JSON.stringify(json.headers, null, 2) + '\n' }; } } + if (json.script) { + let preRequestScript = get(json, 'script.req', ''); + if (preRequestScript.trim().length > 0) { + formattedJson.script = formattedJson.script || {}; + formattedJson.script['pre-request'] = preRequestScript + '\n'; + } + + let postResponseScript = get(json, 'script.res', ''); + if (postResponseScript.trim().length > 0) { + formattedJson.script = formattedJson.script || {}; + formattedJson.script['post-response'] = postResponseScript + '\n'; + } + } + + if (json.tests) { + let testsScript = get(json, 'tests', ''); + if (testsScript.trim().length > 0) { + formattedJson.script = formattedJson.script || {}; + formattedJson.script['tests'] = testsScript + '\n'; + } + } + return stringify(formattedJson); }; diff --git a/packages/bruno-toml/src/tomlToJson.js b/packages/bruno-toml/src/tomlToJson.js index 43504ba4f..37b50ad39 100644 --- a/packages/bruno-toml/src/tomlToJson.js +++ b/packages/bruno-toml/src/tomlToJson.js @@ -1,14 +1,22 @@ const Toml = require('@iarna/toml'); -const { has, each } = require('lodash'); +const { has, each, get } = require('lodash'); + +const stripNewlineAtEnd = (str) => { + if (!str || typeof str !== 'string') { + return ''; + } + + return str.replace(/\n$/, ''); +}; const tomlToJson = (toml) => { const json = Toml.parse(toml); const formattedJson = { meta: { - name: json.meta.name, - type: json.meta.type, - seq: json.meta.seq + name: get(json, 'meta.name', ''), + type: get(json, 'meta.type', ''), + seq: get(json, 'meta.seq', 0) }, http: { method: json.http.method, @@ -53,6 +61,22 @@ const tomlToJson = (toml) => { } } + if (json.script) { + if (json.script['pre-request']) { + formattedJson.script = formattedJson.script || {}; + formattedJson.script.req = stripNewlineAtEnd(json.script['pre-request']); + } + + if (json.script['post-response']) { + formattedJson.script = formattedJson.script || {}; + formattedJson.script.res = stripNewlineAtEnd(json.script['post-response']); + } + + if (json.script['tests']) { + formattedJson.tests = stripNewlineAtEnd(json.script['tests']); + } + } + return formattedJson; }; diff --git a/packages/bruno-toml/tests/index.spec.js b/packages/bruno-toml/tests/index.spec.js index f3a985276..bcd00b77f 100644 --- a/packages/bruno-toml/tests/index.spec.js +++ b/packages/bruno-toml/tests/index.spec.js @@ -13,7 +13,10 @@ const fixtures = [ 'headers/disabled-header', 'headers/dotted-header', 'headers/duplicate-header', - 'headers/reserved-header' + 'headers/reserved-header', + 'scripts/pre-request', + 'scripts/post-response', + 'scripts/tests' ]; describe('bruno toml', () => { @@ -35,7 +38,7 @@ describe('bruno toml', () => { }); it(`should convert toml to json`, () => { - // expect(json).toEqual(tomlToJson(toml)); + expect(json).toEqual(tomlToJson(toml)); }); }); }); diff --git a/packages/bruno-toml/tests/scripts/post-response/request.json b/packages/bruno-toml/tests/scripts/post-response/request.json new file mode 100644 index 000000000..a8fc163d7 --- /dev/null +++ b/packages/bruno-toml/tests/scripts/post-response/request.json @@ -0,0 +1,14 @@ +{ + "meta": { + "name": "Get users", + "type": "http", + "seq": 1 + }, + "http": { + "method": "GET", + "url": "https://reqres.in/api/users" + }, + "script": { + "res": "bru.setVar('token', res.body.token);\nconsole.log('token: ' + res.body.token);" + } +} diff --git a/packages/bruno-toml/tests/scripts/post-response/request.toml b/packages/bruno-toml/tests/scripts/post-response/request.toml new file mode 100644 index 000000000..2467d58a5 --- /dev/null +++ b/packages/bruno-toml/tests/scripts/post-response/request.toml @@ -0,0 +1,14 @@ +[meta] +name = 'Get users' +type = 'http' +seq = 1 + +[http] +method = 'GET' +url = 'https://reqres.in/api/users' + +[script] +post-response = ''' +bru.setVar('token', res.body.token); +console.log('token: ' + res.body.token); +''' diff --git a/packages/bruno-toml/tests/scripts/pre-request/request.json b/packages/bruno-toml/tests/scripts/pre-request/request.json new file mode 100644 index 000000000..7155ec3a4 --- /dev/null +++ b/packages/bruno-toml/tests/scripts/pre-request/request.json @@ -0,0 +1,14 @@ +{ + "meta": { + "name": "Get users", + "type": "http", + "seq": 1 + }, + "http": { + "method": "GET", + "url": "https://reqres.in/api/users" + }, + "script": { + "req": "req.body.id = uuid();" + } +} diff --git a/packages/bruno-toml/tests/scripts/pre-request/request.toml b/packages/bruno-toml/tests/scripts/pre-request/request.toml new file mode 100644 index 000000000..27bc485d0 --- /dev/null +++ b/packages/bruno-toml/tests/scripts/pre-request/request.toml @@ -0,0 +1,13 @@ +[meta] +name = 'Get users' +type = 'http' +seq = 1 + +[http] +method = 'GET' +url = 'https://reqres.in/api/users' + +[script] +pre-request = ''' +req.body.id = uuid(); +''' diff --git a/packages/bruno-toml/tests/scripts/tests/request.json b/packages/bruno-toml/tests/scripts/tests/request.json new file mode 100644 index 000000000..000e3109f --- /dev/null +++ b/packages/bruno-toml/tests/scripts/tests/request.json @@ -0,0 +1,12 @@ +{ + "meta": { + "name": "Get users", + "type": "http", + "seq": 1 + }, + "http": { + "method": "GET", + "url": "https://reqres.in/api/users" + }, + "tests": "test('Status code is 200', function () {\n expect(res.statusCode).to.eql(200);\n});" +} diff --git a/packages/bruno-toml/tests/scripts/tests/request.toml b/packages/bruno-toml/tests/scripts/tests/request.toml new file mode 100644 index 000000000..ce8158325 --- /dev/null +++ b/packages/bruno-toml/tests/scripts/tests/request.toml @@ -0,0 +1,15 @@ +[meta] +name = 'Get users' +type = 'http' +seq = 1 + +[http] +method = 'GET' +url = 'https://reqres.in/api/users' + +[script] +tests = ''' +test('Status code is 200', function () { + expect(res.statusCode).to.eql(200); +}); +'''