UNPKG

1.95 kBJavaScriptView Raw
1const A = require('async');
2const _ = require('lodash');
3
4const helpers = require('artillery/core/lib/engine_util');
5
6function doNothing() {
7 const cb = arguments[arguments.length - 1];
8 return cb(null);
9};
10
11function processBeforeRequestFunctions(script, functionNames, params, context, ee, done) {
12 A.eachSeries(
13 functionNames,
14 function iteratee(functionName, next) {
15 let fn = helpers.template(functionName, context);
16 let processFunc = script.config.processor[fn];
17 if (!processFunc) {
18 processFunc = doNothing;
19 console.log(`WARNING: custom function ${fn} could not be found`);
20 }
21 processFunc(params, context, ee, function(err) {
22 if (err) {
23 return next(err);
24 }
25 return next(null);
26 });
27 },
28 done,
29 );
30}
31
32function processAfterResponseFunctions(script, functionNames, params, response, context, ee, done) {
33 A.eachSeries(
34 functionNames,
35 function iteratee(functionName, next) {
36 let fn = helpers.template(functionName, context);
37 let processFunc = script.config.processor[fn];
38 if (!processFunc) {
39 processFunc = doNothing;
40 console.log(`WARNING: custom function ${fn} could not be found`);
41 }
42 processFunc(params, response, context, ee, function(err) {
43 if (err) {
44 return next(err);
45 }
46 return next(null);
47 });
48 },
49 done,
50 );
51}
52
53/**
54 * try to parse a string and infer its content type.
55 * Return the input if failure.
56 *
57 * @param {string} data
58 */
59function tryToParse(data) {
60 const result = {};
61 try {
62 result.body = JSON.parse(data);
63 result.contentType = 'application/json';
64 } catch (e) {
65 result.body = data;
66 result.contentType = '';
67 }
68 return result;
69}
70
71module.exports = {
72 processBeforeRequestFunctions: processBeforeRequestFunctions,
73 processAfterResponseFunctions: processAfterResponseFunctions,
74 tryToParse: tryToParse
75};
\No newline at end of file