forked from extern/bruno
feat(#1303): toml handle duplicate headers
This commit is contained in:
parent
1754ea9f59
commit
bc01188c98
@ -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);
|
||||
|
@ -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;
|
||||
|
23
packages/bruno-toml/tests/headers/dotted-header/request.json
Normal file
23
packages/bruno-toml/tests/headers/dotted-header/request.json
Normal file
@ -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
|
||||
}
|
||||
]
|
||||
}
|
12
packages/bruno-toml/tests/headers/dotted-header/request.toml
Normal file
12
packages/bruno-toml/tests/headers/dotted-header/request.toml
Normal file
@ -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'
|
@ -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
|
||||
}
|
||||
]
|
||||
}
|
@ -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
|
||||
}
|
||||
]
|
||||
'''
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user