Skip to content

Commit

Permalink
feat(#1303): toml parser for scripts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helloanoop committed Dec 30, 2023
1 parent 5ba2c98 commit abb24c9
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/bruno-toml/lib/stringify
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function stringifyMultilineString (str) {
return escapeString(str).replace(/'(?='')/g, "\\'")
}).join('\n')
if (escaped.slice(-1) === "'") escaped += '\\\n'
return "'''\n" + escaped + "\n'''"
return "'''\n" + escaped + "'''"
}

function stringifyAnyInline (value, multilineOk) {
Expand Down
36 changes: 34 additions & 2 deletions packages/bruno-toml/src/jsonToToml.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ const keyValPairHasReservedKeys = (keyValPair) => {
return false;
}

const reservedKeys = ['disabled', 'description', 'enum'];
const reservedKeys = ['disabled', 'description', 'enum', 'bru'];
const names = keyValPair.map((pair) => pair.name);

return names.some((name) => reservedKeys.includes(name));
};

/**
* Json to Toml
*
* Note: Bruno always append a new line at the end of text blocks
* This is to aid readability when viewing the toml representation of the request
* The newline is removed when converting back to json
*
* @param {object} json
* @returns string
*/
const jsonToToml = (json) => {
const formattedJson = {
meta: {
Expand Down Expand Up @@ -55,11 +65,33 @@ const jsonToToml = (json) => {
});
} else {
formattedJson.headers = {
bru: JSON.stringify(json.headers, null, 2)
bru: JSON.stringify(json.headers, null, 2) + '\n'
};
}
}

if (json.script) {
let preRequestScript = get(json, 'script.req', '');
if (preRequestScript.trim().length > 0) {
formattedJson.script = formattedJson.script || {};
formattedJson.script['pre-request'] = preRequestScript + '\n';
}

let postResponseScript = get(json, 'script.res', '');
if (postResponseScript.trim().length > 0) {
formattedJson.script = formattedJson.script || {};
formattedJson.script['post-response'] = postResponseScript + '\n';
}
}

if (json.tests) {
let testsScript = get(json, 'tests', '');
if (testsScript.trim().length > 0) {
formattedJson.script = formattedJson.script || {};
formattedJson.script['tests'] = testsScript + '\n';
}
}

return stringify(formattedJson);
};

Expand Down
32 changes: 28 additions & 4 deletions packages/bruno-toml/src/tomlToJson.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
const Toml = require('@iarna/toml');
const { has, each } = require('lodash');
const { has, each, get } = require('lodash');

const stripNewlineAtEnd = (str) => {
if (!str || typeof str !== 'string') {
return '';
}

return str.replace(/\n$/, '');
};

const tomlToJson = (toml) => {
const json = Toml.parse(toml);

const formattedJson = {
meta: {
name: json.meta.name,
type: json.meta.type,
seq: json.meta.seq
name: get(json, 'meta.name', ''),
type: get(json, 'meta.type', ''),
seq: get(json, 'meta.seq', 0)
},
http: {
method: json.http.method,
Expand Down Expand Up @@ -53,6 +61,22 @@ const tomlToJson = (toml) => {
}
}

if (json.script) {
if (json.script['pre-request']) {
formattedJson.script = formattedJson.script || {};
formattedJson.script.req = stripNewlineAtEnd(json.script['pre-request']);
}

if (json.script['post-response']) {
formattedJson.script = formattedJson.script || {};
formattedJson.script.res = stripNewlineAtEnd(json.script['post-response']);
}

if (json.script['tests']) {
formattedJson.tests = stripNewlineAtEnd(json.script['tests']);
}
}

return formattedJson;
};

Expand Down
7 changes: 5 additions & 2 deletions packages/bruno-toml/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const fixtures = [
'headers/disabled-header',
'headers/dotted-header',
'headers/duplicate-header',
'headers/reserved-header'
'headers/reserved-header',
'scripts/pre-request',
'scripts/post-response',
'scripts/tests'
];

describe('bruno toml', () => {
Expand All @@ -35,7 +38,7 @@ describe('bruno toml', () => {
});

it(`should convert toml to json`, () => {
// expect(json).toEqual(tomlToJson(toml));
expect(json).toEqual(tomlToJson(toml));
});
});
});
Expand Down
14 changes: 14 additions & 0 deletions packages/bruno-toml/tests/scripts/post-response/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"meta": {
"name": "Get users",
"type": "http",
"seq": 1
},
"http": {
"method": "GET",
"url": "https://reqres.in/api/users"
},
"script": {
"res": "bru.setVar('token', res.body.token);\nconsole.log('token: ' + res.body.token);"
}
}
14 changes: 14 additions & 0 deletions packages/bruno-toml/tests/scripts/post-response/request.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[meta]
name = 'Get users'
type = 'http'
seq = 1

[http]
method = 'GET'
url = 'https://reqres.in/api/users'

[script]
post-response = '''
bru.setVar('token', res.body.token);
console.log('token: ' + res.body.token);
'''
14 changes: 14 additions & 0 deletions packages/bruno-toml/tests/scripts/pre-request/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"meta": {
"name": "Get users",
"type": "http",
"seq": 1
},
"http": {
"method": "GET",
"url": "https://reqres.in/api/users"
},
"script": {
"req": "req.body.id = uuid();"
}
}
13 changes: 13 additions & 0 deletions packages/bruno-toml/tests/scripts/pre-request/request.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[meta]
name = 'Get users'
type = 'http'
seq = 1

[http]
method = 'GET'
url = 'https://reqres.in/api/users'

[script]
pre-request = '''
req.body.id = uuid();
'''
12 changes: 12 additions & 0 deletions packages/bruno-toml/tests/scripts/tests/request.json
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"
},
"tests": "test('Status code is 200', function () {\n expect(res.statusCode).to.eql(200);\n});"
}
15 changes: 15 additions & 0 deletions packages/bruno-toml/tests/scripts/tests/request.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[meta]
name = 'Get users'
type = 'http'
seq = 1

[http]
method = 'GET'
url = 'https://reqres.in/api/users'

[script]
tests = '''
test('Status code is 200', function () {
expect(res.statusCode).to.eql(200);
});
'''

0 comments on commit abb24c9

Please sign in to comment.