feat(#1296): toml tests: different kinds of headers

This commit is contained in:
Anoop M D 2023-12-30 20:29:31 +05:30
parent 2aa073c69a
commit 1754ea9f59
12 changed files with 186 additions and 8 deletions

View File

@ -17,8 +17,6 @@
* You can search for "modified for bruno-toml" to see the changes
*/
'use strict'
module.exports = stringify
module.exports.value = stringifyInline
@ -73,7 +71,14 @@ function stringifyObject (prefix, indent, obj) {
}
})
if (result.length > 0) result.push('')
const complexIndent = prefix && inlineKeys.length > 0 ? indent + ' ' : ''
// original
// const complexIndent = prefix && inlineKeys.length > 0 ? indent + ' ' : ''
// modified for bruno-toml
// we don't want to indent tables
const complexIndent = '';
complexKeys.forEach(key => {
result.push(stringifyComplex(prefix, complexIndent, key, obj[key]))
})
@ -292,7 +297,12 @@ function stringifyComplexTable (prefix, indent, key, value) {
const fullKey = prefix + stringifyKey(key)
let result = ''
if (getInlineKeys(value).length > 0) {
result += indent + '[' + fullKey + ']\n'
// original
// result += indent + '[' + fullKey + ']\n'
// modified for bruno-toml
// we don't want to indent tables
result += '[' + fullKey + ']\n'
}
return result + stringifyObject(fullKey + '.', indent, value)
}

View File

@ -1,5 +1,5 @@
const stringify = require('../lib/stringify');
const { get, each } = require('lodash');
const { get, each, filter } = require('lodash');
const jsonToToml = (json) => {
const formattedJson = {
@ -15,10 +15,17 @@ const jsonToToml = (json) => {
};
if (json.headers && json.headers.length) {
formattedJson.headers = {};
each(json.headers, (header) => {
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;
});
}
return stringify(formattedJson);

View File

@ -18,6 +18,17 @@ 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],

View 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": "Cookie",
"value": "foo=bar",
"enabled": false
}
]
}

View File

@ -0,0 +1,14 @@
[meta]
name = 'Get users'
type = 'http'
seq = 1
[http]
method = 'GET'
url = 'https://reqres.in/api/users'
[headers]
Content-Type = 'application/json'
[headers.disabled]
Cookie = 'foo=bar'

View 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": "Empty-Header",
"value": "",
"enabled": true
}
]
}

View 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'
Empty-Header = ''

View 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": "Spaces In Header",
"value": "",
"enabled": true
}
]
}

View 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'
'Spaces In Header' = ''

View 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": "🐶",
"value": "🚀",
"enabled": true
}
]
}

View 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'
'🐶' = '🚀'

View File

@ -3,7 +3,15 @@ const path = require('path');
const jsonToToml = require('../src/jsonToToml');
const tomlToJson = require('../src/tomlToJson');
const fixtures = ['methods/get', 'methods/delete', 'headers/simple'];
const fixtures = [
'methods/get',
'methods/delete',
'headers/simple',
'headers/empty-header',
'headers/spaces-in-header',
'headers/unicode-in-header',
'headers/disabled-header'
];
describe('bruno toml', () => {
fixtures.forEach((fixture) => {