From bc01188c98cfc430b59797597fcca066c098799e Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sat, 30 Dec 2023 21:27:15 +0530 Subject: [PATCH] feat(#1303): toml handle duplicate headers --- packages/bruno-toml/src/jsonToToml.js | 41 +++++++++++----- packages/bruno-toml/src/tomlToJson.js | 47 ++++++++++++------- .../tests/headers/dotted-header/request.json | 23 +++++++++ .../tests/headers/dotted-header/request.toml | 12 +++++ .../headers/duplicate-header/request.json | 23 +++++++++ .../headers/duplicate-header/request.toml | 24 ++++++++++ .../{simple => simple-header}/request.json | 0 .../{simple => simple-header}/request.toml | 0 packages/bruno-toml/tests/index.spec.js | 6 ++- 9 files changed, 147 insertions(+), 29 deletions(-) create mode 100644 packages/bruno-toml/tests/headers/dotted-header/request.json create mode 100644 packages/bruno-toml/tests/headers/dotted-header/request.toml create mode 100644 packages/bruno-toml/tests/headers/duplicate-header/request.json create mode 100644 packages/bruno-toml/tests/headers/duplicate-header/request.toml rename packages/bruno-toml/tests/headers/{simple => simple-header}/request.json (100%) rename packages/bruno-toml/tests/headers/{simple => simple-header}/request.toml (100%) diff --git a/packages/bruno-toml/src/jsonToToml.js b/packages/bruno-toml/src/jsonToToml.js index de62fd611..8889fb4b8 100644 --- a/packages/bruno-toml/src/jsonToToml.js +++ b/packages/bruno-toml/src/jsonToToml.js @@ -1,6 +1,17 @@ const stringify = require('../lib/stringify'); const { get, each, filter } = require('lodash'); +const keyValPairHasDuplicateKeys = (keyValPair) => { + if (!keyValPair || !Array.isArray(keyValPair) || !keyValPair.length) { + return false; + } + + const names = keyValPair.map((pair) => pair.name); + const uniqueNames = new Set(names); + + return names.length !== uniqueNames.size; +}; + const jsonToToml = (json) => { const formattedJson = { meta: { @@ -15,17 +26,25 @@ const jsonToToml = (json) => { }; if (json.headers && json.headers.length) { - const enabledHeaders = filter(json.headers, (header) => header.enabled); - const disabledHeaders = filter(json.headers, (header) => !header.enabled); - each(enabledHeaders, (header) => { - formattedJson.headers = formattedJson.headers || {}; - formattedJson.headers[header.name] = header.value; - }); - each(disabledHeaders, (header) => { - formattedJson.headers = formattedJson.headers || {}; - formattedJson.headers.disabled = formattedJson.headers.disabled || {}; - formattedJson.headers.disabled[header.name] = header.value; - }); + const hasDuplicateHeaders = keyValPairHasDuplicateKeys(json.headers); + + if (!hasDuplicateHeaders) { + const enabledHeaders = filter(json.headers, (header) => header.enabled); + const disabledHeaders = filter(json.headers, (header) => !header.enabled); + each(enabledHeaders, (header) => { + formattedJson.headers = formattedJson.headers || {}; + formattedJson.headers[header.name] = header.value; + }); + each(disabledHeaders, (header) => { + formattedJson.headers = formattedJson.headers || {}; + formattedJson.headers.disabled = formattedJson.headers.disabled || {}; + formattedJson.headers.disabled[header.name] = header.value; + }); + } else { + formattedJson.headers = { + raw: JSON.stringify(json.headers, null, 2) + }; + } } return stringify(formattedJson); diff --git a/packages/bruno-toml/src/tomlToJson.js b/packages/bruno-toml/src/tomlToJson.js index 9c72fdb49..0d7ca8877 100644 --- a/packages/bruno-toml/src/tomlToJson.js +++ b/packages/bruno-toml/src/tomlToJson.js @@ -1,4 +1,5 @@ const Toml = require('@iarna/toml'); +const { has, each } = require('lodash'); const tomlToJson = (toml) => { const json = Toml.parse(toml); @@ -17,24 +18,38 @@ const tomlToJson = (toml) => { if (json.headers) { formattedJson.headers = []; - Object.keys(json.headers).forEach((key) => { - if (key === 'disabled') { - Object.keys(json.headers['disabled']).forEach((disabledKey) => { - formattedJson.headers.push({ - name: disabledKey, - value: json.headers[key][disabledKey], - enabled: false - }); - }); - return; - } - formattedJson.headers.push({ - name: key, - value: json.headers[key], - enabled: true + // headers are stored in raw format if they contain duplicate keys + if (has(json.headers, 'raw')) { + let parsedHeaders = JSON.parse(json.headers.raw); + + each(parsedHeaders, (header) => { + formattedJson.headers.push({ + name: header.name, + value: header.value, + enabled: header.enabled + }); }); - }); + } else { + Object.keys(json.headers).forEach((key) => { + if (key === 'disabled') { + Object.keys(json.headers['disabled']).forEach((disabledKey) => { + formattedJson.headers.push({ + name: disabledKey, + value: json.headers[key][disabledKey], + enabled: false + }); + }); + return; + } + + formattedJson.headers.push({ + name: key, + value: json.headers[key], + enabled: true + }); + }); + } } return formattedJson; diff --git a/packages/bruno-toml/tests/headers/dotted-header/request.json b/packages/bruno-toml/tests/headers/dotted-header/request.json new file mode 100644 index 000000000..c1b6ecefa --- /dev/null +++ b/packages/bruno-toml/tests/headers/dotted-header/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": "Dots.In.Header.Key", + "value": "Dots.In.Header.Value", + "enabled": true + } + ] +} diff --git a/packages/bruno-toml/tests/headers/dotted-header/request.toml b/packages/bruno-toml/tests/headers/dotted-header/request.toml new file mode 100644 index 000000000..fb4af38f5 --- /dev/null +++ b/packages/bruno-toml/tests/headers/dotted-header/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' +'Dots.In.Header.Key' = 'Dots.In.Header.Value' diff --git a/packages/bruno-toml/tests/headers/duplicate-header/request.json b/packages/bruno-toml/tests/headers/duplicate-header/request.json new file mode 100644 index 000000000..247a486dd --- /dev/null +++ b/packages/bruno-toml/tests/headers/duplicate-header/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": "Content-Type", + "value": "application/ld+json", + "enabled": true + } + ] +} diff --git a/packages/bruno-toml/tests/headers/duplicate-header/request.toml b/packages/bruno-toml/tests/headers/duplicate-header/request.toml new file mode 100644 index 000000000..629cadc97 --- /dev/null +++ b/packages/bruno-toml/tests/headers/duplicate-header/request.toml @@ -0,0 +1,24 @@ +[meta] +name = 'Get users' +type = 'http' +seq = 1 + +[http] +method = 'GET' +url = 'https://reqres.in/api/users' + +[headers] +raw = ''' +[ + { + "name": "Content-Type", + "value": "application/json", + "enabled": true + }, + { + "name": "Content-Type", + "value": "application/ld+json", + "enabled": true + } +] +''' diff --git a/packages/bruno-toml/tests/headers/simple/request.json b/packages/bruno-toml/tests/headers/simple-header/request.json similarity index 100% rename from packages/bruno-toml/tests/headers/simple/request.json rename to packages/bruno-toml/tests/headers/simple-header/request.json diff --git a/packages/bruno-toml/tests/headers/simple/request.toml b/packages/bruno-toml/tests/headers/simple-header/request.toml similarity index 100% rename from packages/bruno-toml/tests/headers/simple/request.toml rename to packages/bruno-toml/tests/headers/simple-header/request.toml diff --git a/packages/bruno-toml/tests/index.spec.js b/packages/bruno-toml/tests/index.spec.js index f03a0842d..4ed27070b 100644 --- a/packages/bruno-toml/tests/index.spec.js +++ b/packages/bruno-toml/tests/index.spec.js @@ -6,11 +6,13 @@ const tomlToJson = require('../src/tomlToJson'); const fixtures = [ 'methods/get', 'methods/delete', - 'headers/simple', + 'headers/simple-header', 'headers/empty-header', 'headers/spaces-in-header', 'headers/unicode-in-header', - 'headers/disabled-header' + 'headers/disabled-header', + 'headers/dotted-header', + 'headers/duplicate-header' ]; describe('bruno toml', () => {