feat(#1296): toml tests: simple header

This commit is contained in:
Anoop M D 2023-12-30 19:09:17 +05:30
parent 48ec87ec8c
commit 2aa073c69a
7 changed files with 103 additions and 67 deletions

View File

@ -1,7 +1,27 @@
const stringify = require('../lib/stringify'); const stringify = require('../lib/stringify');
const { get, each } = require('lodash');
const jsonToToml = (json) => { 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; module.exports = jsonToToml;

View File

@ -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;

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": 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'
Cookie = 'foo=bar'

View File

@ -1,18 +1,31 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const jsonToToml = require('../src/jsonToToml'); 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', () => { describe('bruno toml', () => {
fixtures.forEach((fixture) => { fixtures.forEach((fixture) => {
describe(fixture, () => { describe(fixture, () => {
const json = require(`./${fixture}/request.json`); const json = require(`./${fixture}/request.json`);
const toml = fs.readFileSync(path.join(__dirname, fixture, 'request.toml'), 'utf8'); 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`, () => { it(`should convert json to toml`, () => {
expect(toml).toEqual(jsonToToml(json)); expect(toml).toEqual(jsonToToml(json));
}); });
it(`should convert toml to json`, () => {
expect(json).toEqual(tomlToJson(toml));
});
}); });
}); });
}); });

View File

@ -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);
});
});

View File

@ -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);
});
});