mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-21 23:43:15 +01:00
feat(#1296): toml tests: simple header
This commit is contained in:
parent
48ec87ec8c
commit
2aa073c69a
@ -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
packages/bruno-toml/src/tomlToJson.js
Normal file
32
packages/bruno-toml/src/tomlToJson.js
Normal 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;
|
23
packages/bruno-toml/tests/headers/simple/request.json
Normal file
23
packages/bruno-toml/tests/headers/simple/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": "Cookie",
|
||||
"value": "foo=bar",
|
||||
"enabled": true
|
||||
}
|
||||
]
|
||||
}
|
12
packages/bruno-toml/tests/headers/simple/request.toml
Normal file
12
packages/bruno-toml/tests/headers/simple/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'
|
||||
Cookie = 'foo=bar'
|
@ -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));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
@ -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);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user