artillery icon indicating copy to clipboard operation
artillery copied to clipboard

Preserve cookies from `before` into the sceario

Open MickL opened this issue 2 years ago • 3 comments

Version info: 2.0.0-14

I expect, using before, e.g. to login, the received cookies to be preserved into the scenario.

Instead, the cookies are not present in the scenario.

Usecase: Login once, receive a token (in cookie) and then test the refresh token endpoint.

MickL avatar Apr 21 '22 09:04 MickL

hi @MickL 👋

Cookies (and any other VU-specific data) aren't propagated from a before section into VU scenarios. Only variables created with capture will be available to VUs. This is by design.

hassy avatar Apr 21 '22 10:04 hassy

Thanks for the quick response! How may I do a login before a scenario so i can test my refresh token endpoint? I want to test the token refresh without the login.

My idea is to do something like that, what do you think?

const isFirstRequest = true;

export const beforeRefreshTokenHandler: AsyncRequestInterceptor = async (
  request: ArtilleryRequest,
  context: ScenarioContext,
  ee: events.EventEmitter,
  next: Next
) => {
  if (!isFirstRequest) {
    return;
  }

  // Login
  // Set cookie

  return next();
}

MickL avatar Apr 21 '22 10:04 MickL

I have run into a similar problem. In v1.6 I was using a before section to log my VUs into the service I'm testing. I have a tokens object defined in my processor.js file and after each VU logged in the authtoken was stored there with the username as the key e.g.

var token = { "user1": "someAuthTokenHere", "user2": "anotherAuthTokenHere" }

Then I could make the token available like this in my processor file: context.vars["authToken"] = tokens[context.vars['username']];

and reference it in the yml file

headers: Authorization: "Token {{authToken}}"

In version 2 that doesn't work. If I log the token object during the before loop I can see each authToken is assigned to its user, but when I log the token object in the scenarios section, the tokens have disappeared.

Using the before section to setup your users seems like a necessary use case. Otherwise you must login each user every time, and that's not ideal, since load testing your authentication service is not necessarily the goal. I suppose the other option would be to provide an auth token in the csv file, but auth tokens expire, and it's also a security concern.

mellema avatar May 03 '22 22:05 mellema