1 | const A = require('async');
|
2 | const _ = require('lodash');
|
3 |
|
4 | const helpers = require('artillery/core/lib/engine_util');
|
5 |
|
6 | function doNothing() {
|
7 | const cb = arguments[arguments.length - 1];
|
8 | return cb(null);
|
9 | };
|
10 |
|
11 | function 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 |
|
32 | function 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 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function 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 |
|
71 | module.exports = {
|
72 | processBeforeRequestFunctions: processBeforeRequestFunctions,
|
73 | processAfterResponseFunctions: processAfterResponseFunctions,
|
74 | tryToParse: tryToParse
|
75 | }; |
\ | No newline at end of file |