Merge pull request #15 from Seich/next-dynamic-values

Allow dynamic values to replace values internally.
This commit is contained in:
Sergio Díaz 2018-04-30 14:42:51 -06:00 committed by GitHub
commit 91d35459aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1356 additions and 1342 deletions

2624
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -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",

View File

@ -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",

View File

@ -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",

View File

@ -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
); );
}); });
it(`should look for dynamic values executing and replacing them`, async () => {
let cache = new RequestCache(); let cache = new RequestCache();
cache.add('value2', '2'); cache.add('value2', '2');
it(`should look for dynamic values executing and replacing them`, async () => {
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 () => {

View File

@ -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);
}); });

View File

@ -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);