How to fetch random values from feeder for using in header in gatling

Viewed 549

I have a CSV of 1000 tokens to be used in the header, any random token in each scenario. And that random token should remain the same for all get and post requests in the scenario. Below is the sample data of the CSV file.

token

Token token1tewq

Token token2cbvj

and so on till 1000 rows

The scenario I want to execute is like following.

  1. Hit a get API using a random token(from CSV) in the header.
  2. pause for 2 sec.
  3. Hit a posting API with a body with the same token fetched in step 1.
  4. Hit the same API with the same token as done in step 1.

I tried doing it with the below code but couldn't get it working.

val tokencsv = csv("test.csv").random
val httpConf = http.baseURL("http://myApiUrl")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // Here are the common headers
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")

val userFeed = feed(tokencsv)
      .exec(http("Get all feeds")
      .get("url1")
      .headers(Map("Content-Type" -> "application/json","Authorization" -> "${token}")))
      .pause(2)
      .exec(http("Post a feed")
      .post("url2")
      .body(StringBody("""{"type":2, "images":["http://imageurl.jpg"],"text":"lorem ipsum", "data_id":12}""")).asJSON
      .headers(Map("Content-Type" -> "application/json","Authorization" ->  "${token}")))
      .pause(1)
      .exec(http("Reloading page after posting the feed").get("url1")
      .headers(Map("Content-Type" -> "application/json","Authorization" -> "${token}")))

val socialFeedGetAll = scenario("social feed").exec(userFeed)
setUp(socialFeedGetAll.inject(constantUsersPerSec(10) during(60 seconds)).protocols(httpConf))
0 Answers
Related