feat(#119): bru lang support for basic and bearer auth

This commit is contained in:
Anoop M D 2023-09-29 01:35:22 +05:30
parent a6b19605b5
commit 51ee37cf96
4 changed files with 77 additions and 3 deletions

View File

@ -22,7 +22,8 @@ const { outdentString } = require('../../v1/src/utils');
* *
*/ */
const grammar = ohm.grammar(`Bru { const grammar = ohm.grammar(`Bru {
BruFile = (meta | http | query | headers | bodies | varsandassert | script | tests | docs)* BruFile = (meta | http | query | headers | auths | bodies | varsandassert | script | tests | docs)*
auths = authbasic | authbearer
bodies = bodyjson | bodytext | bodyxml | bodygraphql | bodygraphqlvars | bodyforms | body bodies = bodyjson | bodytext | bodyxml | bodygraphql | bodygraphqlvars | bodyforms | body
bodyforms = bodyformurlencoded | bodymultipart bodyforms = bodyformurlencoded | bodymultipart
@ -75,6 +76,9 @@ const grammar = ohm.grammar(`Bru {
varsres = "vars:post-response" dictionary varsres = "vars:post-response" dictionary
assert = "assert" assertdictionary assert = "assert" assertdictionary
authbasic = "auth:basic" dictionary
authbearer = "auth:bearer" dictionary
body = "body" st* "{" nl* textblock tagend body = "body" st* "{" nl* textblock tagend
bodyjson = "body:json" st* "{" nl* textblock tagend bodyjson = "body:json" st* "{" nl* textblock tagend
bodytext = "body:text" st* "{" nl* textblock tagend bodytext = "body:text" st* "{" nl* textblock tagend
@ -92,13 +96,21 @@ const grammar = ohm.grammar(`Bru {
docs = "docs" st* "{" nl* textblock tagend docs = "docs" st* "{" nl* textblock tagend
}`); }`);
const mapPairListToKeyValPairs = (pairList = []) => { const mapPairListToKeyValPairs = (pairList = [], parseEnabled = true) => {
if (!pairList.length) { if (!pairList.length) {
return []; return [];
} }
return _.map(pairList[0], (pair) => { return _.map(pairList[0], (pair) => {
let name = _.keys(pair)[0]; let name = _.keys(pair)[0];
let value = pair[name]; let value = pair[name];
if (!parseEnabled) {
return {
name,
value
};
}
let enabled = true; let enabled = true;
if (name && name.length && name.charAt(0) === '~') { if (name && name.length && name.charAt(0) === '~') {
name = name.slice(1); name = name.slice(1);
@ -282,6 +294,33 @@ const sem = grammar.createSemantics().addAttribute('ast', {
headers: mapPairListToKeyValPairs(dictionary.ast) headers: mapPairListToKeyValPairs(dictionary.ast)
}; };
}, },
authbasic(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const usernameKey = _.find(auth, { name: 'username' });
const passwordKey = _.find(auth, { name: 'password' });
const username = usernameKey ? usernameKey.value : '';
const password = passwordKey ? passwordKey.value : '';
return {
auth: {
basic: {
username,
password
}
}
};
},
authbearer(_1, dictionary) {
const auth = mapPairListToKeyValPairs(dictionary.ast, false);
const tokenKey = _.find(auth, { name: 'token' });
const token = tokenKey ? tokenKey.value : '';
return {
auth: {
bearer: {
token
}
}
};
},
bodyformurlencoded(_1, dictionary) { bodyformurlencoded(_1, dictionary) {
return { return {
body: { body: {

View File

@ -13,7 +13,7 @@ const stripLastLine = (text) => {
}; };
const jsonToBru = (json) => { const jsonToBru = (json) => {
const { meta, http, query, headers, body, script, tests, vars, assertions, docs } = json; const { meta, http, query, headers, auth, body, script, tests, vars, assertions, docs } = json;
let bru = ''; let bru = '';
@ -82,6 +82,23 @@ const jsonToBru = (json) => {
bru += '\n}\n\n'; bru += '\n}\n\n';
} }
if (auth && auth.basic) {
bru += `auth:basic {
${indentString(`username: ${auth.basic.username}`)}
${indentString(`password: ${auth.basic.password}`)}
}
`;
}
if (auth && auth.bearer) {
bru += `auth:bearer {
${indentString(`token: ${auth.bearer.token}`)}
}
`;
}
if (body && body.json && body.json.length) { if (body && body.json && body.json.length) {
bru += `body:json { bru += `body:json {
${indentString(body.json)} ${indentString(body.json)}

View File

@ -21,6 +21,15 @@ headers {
~transaction-id: {{transactionId}} ~transaction-id: {{transactionId}}
} }
auth:basic {
username: john
password: secret
}
auth:bearer {
token: 123
}
body:json { body:json {
{ {
"hello": "world" "hello": "world"

View File

@ -43,6 +43,15 @@
"enabled": false "enabled": false
} }
], ],
"auth": {
"basic": {
"username": "john",
"password": "secret"
},
"bearer": {
"token": "123"
}
},
"body": { "body": {
"json": "{\n \"hello\": \"world\"\n}", "json": "{\n \"hello\": \"world\"\n}",
"text": "This is a text body", "text": "This is a text body",