From 2aa073c69a79fd80637a2be6ec60b96eea14d7d0 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sat, 30 Dec 2023 19:09:17 +0530 Subject: [PATCH] feat(#1296): toml tests: simple header --- packages/bruno-toml/src/jsonToToml.js | 22 +++++++++++- packages/bruno-toml/src/tomlToJson.js | 32 +++++++++++++++++ .../tests/headers/simple/request.json | 23 ++++++++++++ .../tests/headers/simple/request.toml | 12 +++++++ packages/bruno-toml/tests/index.spec.js | 17 +++++++-- .../jsonToToml/headers/headers-simple.spec.js | 35 ------------------- .../tests/jsonToToml/simple-get.spec.js | 29 --------------- 7 files changed, 103 insertions(+), 67 deletions(-) create mode 100644 packages/bruno-toml/src/tomlToJson.js create mode 100644 packages/bruno-toml/tests/headers/simple/request.json create mode 100644 packages/bruno-toml/tests/headers/simple/request.toml delete mode 100644 packages/bruno-toml/tests/jsonToToml/headers/headers-simple.spec.js delete mode 100644 packages/bruno-toml/tests/jsonToToml/simple-get.spec.js diff --git a/packages/bruno-toml/src/jsonToToml.js b/packages/bruno-toml/src/jsonToToml.js index 04d07b5b..8e87b661 100644 --- a/packages/bruno-toml/src/jsonToToml.js +++ b/packages/bruno-toml/src/jsonToToml.js @@ -1,7 +1,27 @@ const stringify = require('../lib/stringify'); +const { get, each } = require('lodash'); const jsonToToml = (json) => { - return stringify(json); + const formattedJson = { + meta: { + name: get(json, 'meta.name'), + type: get(json, 'meta.type'), + seq: get(json, 'meta.seq') + }, + http: { + method: get(json, 'http.method'), + url: get(json, 'http.url', '') + } + }; + + if (json.headers && json.headers.length) { + formattedJson.headers = {}; + each(json.headers, (header) => { + formattedJson.headers[header.name] = header.value; + }); + } + + return stringify(formattedJson); }; module.exports = jsonToToml; diff --git a/packages/bruno-toml/src/tomlToJson.js b/packages/bruno-toml/src/tomlToJson.js new file mode 100644 index 00000000..92c3fafa --- /dev/null +++ b/packages/bruno-toml/src/tomlToJson.js @@ -0,0 +1,32 @@ +const Toml = require('@iarna/toml'); + +const tomlToJson = (toml) => { + const json = Toml.parse(toml); + + const formattedJson = { + meta: { + name: json.meta.name, + type: json.meta.type, + seq: json.meta.seq + }, + http: { + method: json.http.method, + url: json.http.url + } + }; + + if (json.headers) { + formattedJson.headers = []; + Object.keys(json.headers).forEach((key) => { + formattedJson.headers.push({ + name: key, + value: json.headers[key], + enabled: true + }); + }); + } + + return formattedJson; +}; + +module.exports = tomlToJson; diff --git a/packages/bruno-toml/tests/headers/simple/request.json b/packages/bruno-toml/tests/headers/simple/request.json new file mode 100644 index 00000000..5e1b3bf1 --- /dev/null +++ b/packages/bruno-toml/tests/headers/simple/request.json @@ -0,0 +1,23 @@ +{ + "meta": { + "name": "Get users", + "type": "http", + "seq": 1 + }, + "http": { + "method": "GET", + "url": "https://reqres.in/api/users" + }, + "headers": [ + { + "name": "Content-Type", + "value": "application/json", + "enabled": true + }, + { + "name": "Cookie", + "value": "foo=bar", + "enabled": true + } + ] +} diff --git a/packages/bruno-toml/tests/headers/simple/request.toml b/packages/bruno-toml/tests/headers/simple/request.toml new file mode 100644 index 00000000..3c2ec3bd --- /dev/null +++ b/packages/bruno-toml/tests/headers/simple/request.toml @@ -0,0 +1,12 @@ +[meta] +name = 'Get users' +type = 'http' +seq = 1 + +[http] +method = 'GET' +url = 'https://reqres.in/api/users' + +[headers] +Content-Type = 'application/json' +Cookie = 'foo=bar' diff --git a/packages/bruno-toml/tests/index.spec.js b/packages/bruno-toml/tests/index.spec.js index 10952e82..2942a3e1 100644 --- a/packages/bruno-toml/tests/index.spec.js +++ b/packages/bruno-toml/tests/index.spec.js @@ -1,18 +1,31 @@ const fs = require('fs'); const path = require('path'); const jsonToToml = require('../src/jsonToToml'); -const { describe } = require('@jest/globals'); +const tomlToJson = require('../src/tomlToJson'); -const fixtures = ['methods/get', 'methods/delete']; +const fixtures = ['methods/get', 'methods/delete', 'headers/simple']; describe('bruno toml', () => { fixtures.forEach((fixture) => { describe(fixture, () => { const json = require(`./${fixture}/request.json`); const toml = fs.readFileSync(path.join(__dirname, fixture, 'request.toml'), 'utf8'); + + if (process.env.DEBUG === 'true') { + console.log(`DEBUG: Running ${fixture} tests`); + console.log('json', JSON.stringify(json, null, 2)); + console.log('toml', toml); + console.log('jsonToToml', jsonToToml(json)); + console.log('tomlToJson', JSON.stringify(tomlToJson(toml), null, 2)); + } + it(`should convert json to toml`, () => { expect(toml).toEqual(jsonToToml(json)); }); + + it(`should convert toml to json`, () => { + expect(json).toEqual(tomlToJson(toml)); + }); }); }); }); diff --git a/packages/bruno-toml/tests/jsonToToml/headers/headers-simple.spec.js b/packages/bruno-toml/tests/jsonToToml/headers/headers-simple.spec.js deleted file mode 100644 index d8085477..00000000 --- a/packages/bruno-toml/tests/jsonToToml/headers/headers-simple.spec.js +++ /dev/null @@ -1,35 +0,0 @@ -const jsonToToml = require('../../src/jsonToToml'); - -const json = { - meta: { - name: 'Get users', - type: 'http', - seq: '1' - }, - http: { - method: 'get', - url: 'https://reqres.in/api/users' - }, - headers: { - Accept: 'application/json' - } -}; - -const toml = `[meta] -name = 'Get users' -type = 'http' -seq = '1' - -[http] -method = 'get' -url = 'https://reqres.in/api/users' - -[headers] -Accept = 'application/json' -`; - -describe('jsonToToml - simple get', () => { - it('should parse the json file', () => { - expect(jsonToToml(json)).toEqual(toml); - }); -}); diff --git a/packages/bruno-toml/tests/jsonToToml/simple-get.spec.js b/packages/bruno-toml/tests/jsonToToml/simple-get.spec.js deleted file mode 100644 index 814bda81..00000000 --- a/packages/bruno-toml/tests/jsonToToml/simple-get.spec.js +++ /dev/null @@ -1,29 +0,0 @@ -const jsonToToml = require('../../src/jsonToToml'); - -const json = { - meta: { - name: 'Get users', - type: 'http', - seq: '1' - }, - http: { - method: 'get', - url: 'https://reqres.in/api/users' - } -}; - -const toml = `[meta] -name = 'Get users' -type = 'http' -seq = '1' - -[http] -method = 'get' -url = 'https://reqres.in/api/users' -`; - -describe('jsonToToml - simple get', () => { - it('should parse the json', () => { - expect(jsonToToml(json)).toEqual(toml); - }); -});