Skip to content

Commit

Permalink
feat(#1296): toml tests: simple header
Browse files Browse the repository at this point in the history
  • Loading branch information
helloanoop committed Dec 30, 2023
1 parent 48ec87e commit 2aa073c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 67 deletions.
22 changes: 21 additions & 1 deletion packages/bruno-toml/src/jsonToToml.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 32 additions & 0 deletions packages/bruno-toml/src/tomlToJson.js
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 23 additions & 0 deletions packages/bruno-toml/tests/headers/simple/request.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
12 changes: 12 additions & 0 deletions packages/bruno-toml/tests/headers/simple/request.toml
Original file line number Diff line number Diff line change
@@ -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'
17 changes: 15 additions & 2 deletions packages/bruno-toml/tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -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));
});
});
});
});

This file was deleted.

29 changes: 0 additions & 29 deletions packages/bruno-toml/tests/jsonToToml/simple-get.spec.js

This file was deleted.

0 comments on commit 2aa073c

Please sign in to comment.