mirror of https://github.com/Seich/Beau.git
Merge pull request #15 from Seich/next-dynamic-values
Allow dynamic values to replace values internally.
This commit is contained in:
commit
91d35459aa
File diff suppressed because it is too large
Load Diff
|
|
@ -16,6 +16,7 @@
|
|||
"commander": "^2.15.1",
|
||||
"deepmerge": "^2.1.0",
|
||||
"dotenv": "^5.0.1",
|
||||
"is-plain-object": "^2.0.4",
|
||||
"js-yaml": "^3.11.0",
|
||||
"jsome": "^2.5.0",
|
||||
"request": "^2.85.0",
|
||||
|
|
|
|||
|
|
@ -192,6 +192,7 @@ Beau {
|
|||
"DEPENDENCIES": Set {},
|
||||
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
||||
"FORM": undefined,
|
||||
"FORMDATA": undefined,
|
||||
"HEADERS": Object {
|
||||
"authentication": "hello",
|
||||
},
|
||||
|
|
@ -224,6 +225,7 @@ Beau {
|
|||
"DEPENDENCIES": Set {},
|
||||
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
||||
"FORM": undefined,
|
||||
"FORMDATA": undefined,
|
||||
"HEADERS": Object {
|
||||
"authentication": "hello",
|
||||
"hello": "world",
|
||||
|
|
|
|||
|
|
@ -1,24 +1,5 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`Beau's plugin system Dynamic Values should look for dynamic values executing and replacing them 1`] = `
|
||||
Object {
|
||||
"body": "Hello World",
|
||||
"request": Object {
|
||||
"body": undefined,
|
||||
"endpoint": "http://example.com/hello/3",
|
||||
"headers": Object {
|
||||
"count": "3",
|
||||
"preRequestModifier": true,
|
||||
},
|
||||
},
|
||||
"response": Object {
|
||||
"body": "Hello World",
|
||||
"headers": Array [],
|
||||
"status": 200,
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`Beau's plugin system Request Modifiers should modify the request and response using modifiers. 1`] = `
|
||||
Object {
|
||||
"body": "Hello World",
|
||||
|
|
|
|||
|
|
@ -65,21 +65,31 @@ describe(`Beau's plugin system`, () => {
|
|||
endpoint: 'http://example.com',
|
||||
alias: 'say-hello',
|
||||
headers: {
|
||||
count: '$[add(1, $value2)]'
|
||||
}
|
||||
count: '$[add(1, $value2)]',
|
||||
empty: ''
|
||||
},
|
||||
payload: 'counted $[add(1, $value2)] so far.'
|
||||
},
|
||||
plugins
|
||||
);
|
||||
});
|
||||
|
||||
it(`should look for dynamic values executing and replacing them`, async () => {
|
||||
let cache = new RequestCache();
|
||||
cache.add('value2', '2');
|
||||
|
||||
it(`should look for dynamic values executing and replacing them`, async () => {
|
||||
let req = await request.exec(cache);
|
||||
expect(req).toHaveProperty('request.body', 'counted 3 so far.');
|
||||
});
|
||||
|
||||
expect(req).toHaveProperty('request.headers.count', '3');
|
||||
expect(req).toMatchSnapshot();
|
||||
it(`should change the internal datatype if the only thing in the value is the dynamic value`, async () => {
|
||||
let req = await request.exec(cache);
|
||||
expect(req).toHaveProperty('request.headers.count', 3);
|
||||
});
|
||||
|
||||
it(`should return empty values as empty`, async () => {
|
||||
let req = await request.exec(cache);
|
||||
expect(req).toHaveProperty('request.headers.empty', '');
|
||||
});
|
||||
|
||||
it(`should throw when calling an undefined dynamic value`, async () => {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ const vm = require('vm');
|
|||
const requireg = require('requireg');
|
||||
const deepmerge = require('deepmerge');
|
||||
const { toKebabCase, dynamicValueRegex, replaceInObject } = require('./shared');
|
||||
const isPlainObject = require('is-plain-object');
|
||||
|
||||
class Plugins {
|
||||
constructor(plugins = []) {
|
||||
|
|
@ -36,7 +37,7 @@ class Plugins {
|
|||
}
|
||||
|
||||
executeModifier(modifier, obj, orig) {
|
||||
let result = deepmerge({}, obj);
|
||||
let result = deepmerge({}, obj, { isMergeableObject: isPlainObject });
|
||||
|
||||
this.registry[modifier].forEach(
|
||||
modifier => (result = modifier(result, orig))
|
||||
|
|
@ -47,7 +48,25 @@ class Plugins {
|
|||
|
||||
replaceDynamicValues(obj) {
|
||||
return replaceInObject(obj, val => {
|
||||
let valIsEmpty = val.trim().length === 0;
|
||||
|
||||
if (valIsEmpty) {
|
||||
return val;
|
||||
}
|
||||
|
||||
try {
|
||||
let onlyHasDynamic =
|
||||
val.replace(dynamicValueRegex, '').trim() === '';
|
||||
|
||||
if (onlyHasDynamic) {
|
||||
let call;
|
||||
val.replace(dynamicValueRegex, (match, c) => {
|
||||
call = c;
|
||||
});
|
||||
|
||||
return vm.runInContext(call, this.context);
|
||||
}
|
||||
|
||||
return val.replace(dynamicValueRegex, (match, call) => {
|
||||
return vm.runInContext(call, this.context);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -25,7 +25,8 @@ class Request {
|
|||
'PARAMS',
|
||||
'FORM',
|
||||
'ALIAS',
|
||||
'COOKIEJAR'
|
||||
'COOKIEJAR',
|
||||
'FORMDATA'
|
||||
],
|
||||
req
|
||||
);
|
||||
|
|
@ -93,6 +94,7 @@ class Request {
|
|||
qs: this.PARAMS,
|
||||
body: this.PAYLOAD,
|
||||
form: this.FORM,
|
||||
formData: this.FORMDATA,
|
||||
|
||||
json: true,
|
||||
simple: false,
|
||||
|
|
@ -103,7 +105,8 @@ class Request {
|
|||
'headers',
|
||||
'qs',
|
||||
'body',
|
||||
'form'
|
||||
'form',
|
||||
'formData'
|
||||
]);
|
||||
|
||||
settings = this.plugins.replaceDynamicValues(settings);
|
||||
|
|
|
|||
Loading…
Reference in New Issue