Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeRequest hook doesn't work #184

Closed
andrevas opened this issue Sep 27, 2016 · 6 comments
Closed

beforeRequest hook doesn't work #184

andrevas opened this issue Sep 27, 2016 · 6 comments
Labels

Comments

@andrevas
Copy link

Hi guys!
I noticed the next issue:
When trying to change my json before sending, defined beforeRequest function is not called. Adding afterResponse function working as expected. My code looks like that:

 {
  "config": {
    "target": "...my target...",
    "phases": [
      {"duration": 10, "arrivalRate": 5}
    ],
    "processor" : "helloJS.js",
    "defaults": {
      "headers": {
        "content-type": "application/json"
      }
    }
  },
  "scenarios": [
    {
      "flow": [
        {
          "post": {
            "url": "...my url...",
            "json": {
                 "my json"
            },
            "beforeRequest" : "foo"
          }
        }
      ]
    }
  ]
}
module.exports = {
    foo: foo
};

function foo (requestParams, context, ee, next) {
    console.log("TEST");
    return next();
}
@hassy
Copy link
Member

hassy commented Sep 27, 2016

Thanks @andrevas. Your code looks right and should be working, will need to investigate.

@danielo515
Copy link

danielo515 commented Sep 29, 2016

Something similar is happening for me

In my case it only happens if I do the following

// file a.js
module.exports.funA = myfunction
// file b.js
module.exports.funB = myfunction
// file artillery.js
Object.assign( module.exports , require('./a.js'), require('./b.js'));

Then I configure those for the scenario:

config:
    target: 'https://localhost:3000'
    processor: "./artillery.js"
    tls:
        rejectUnauthorized: false
    phases:
        - duration: 10
          arrivalRate: 20
scenarios:
    - beforeRequest: ["funA","funB"]
      flow:
        - get:
            url: "/test"
            json: "{}"

Seems that the problem is that beforeRequest can not be an array. If I only include one function it works

@danielo515
Copy link

My issue is probably a problem on the YAML parser. Very early on the running phase the beforeRequest array is empty.
In function runScenario(script, intermediate, runState) the script specification already has an empty array for beforeRequest.

Regards

@danielo515
Copy link

Hello @hassy , finally I found the bug. It's pretty straighforward
The problem seems to be at line 82 of runner.js

 if (scenarioSpec.beforeRequest && !_.isArray(scenarioSpec.beforeRequest)) {
      scenarioSpec.beforeRequest = [scenarioSpec.beforeRequest];
    } else {
      scenarioSpec.beforeRequest = [];
    }

Why do you empty the array if it is an array? it makes no sense.

@hassy
Copy link
Member

hassy commented Sep 30, 2016

It needs to be set to an empty array if it's not defined (and left alone if it's an array). Easy fix. Thanks for investigating @danielo515!

@danielo515
Copy link

You're welcome @hassy

As a suggestion this worked fine for me:

if ( !_.isArray(scenarioSpec.beforeRequest)) {
      scenarioSpec.beforeRequest = scenarioSpec.beforeRequest ? [scenarioSpec.beforeRequest] : [];
    }

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants