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