1
votes

I tried all the suggestions I found from other examples on this site and I still cannot get my Gatling test to pass the authentication token from a POST to the following GETs. The token is taken from the return body of the POST and passed in as a header to the GET

Initially the login was in BaseSimulationTest but I put it in GetDealsTests for troubleshooting

Steps I have tried:

  1. I added this: //println(" Token value" + session("accessToken").as[String]) And I was able to see that I got a string back in the terminal that seemed to indicate accessToken was a null value
  2. I tried declaring var accessValue and var accessToken both in the method and globally. No change.
  3. I tried checking the header value on the GET after passing the token in but .check(header.is(expected = ${accessToken})) seems to just error out
  4. I have put the login POST and GET in the same method, different methods etc
  5. I have tried passing in the username and password from .formParam instead of in the body of the request statement

Do I need to do the headers as a map? Is this a scope thing? Do I need to declare variables differently? The tests are running with an "expected 200 but received 401" type result. I think the POST doesn't even see the token being passed to it.

class GetDealsTests extends BaseSimulationTest {

  def logIn2() = {
    exec(http("Log in")
      .post(getUrl() + "login")
      .header("Content-Type", "application/json")
      .body(ElFileBody("bodies/Login.json")).asJson
      .check(status.is(200))
      .check(jsonPath("$.access_token").saveAs("accessToken")))
        exec(
        session=>{
          accessValue = session("accessToken").as[String]
          session
        })
  }

  def getAllDeals() = {

    exec(_.set("accessToken", accessValue))
      .exec(
        http("Get all deals")
          .get("deals/all")
            .header("Authorization", "Bearer" + "${accessToken}")
            .check(status.is(200))
          
      )
  }


  val scnGetAllDeals = scenario("Get All Deals endpoint tests")
    .forever() {
      exec(logIn2())
      exec(getAllDeals())
    }

  setUp(
    scnGetAllDeals.inject(
      nothingFor(5 seconds),
      atOnceUsers(users))

  ).protocols(httpConf.inferHtmlResources())
  .maxDuration(FiniteDuration(duration.toLong, MINUTES))
}

I looked at the following: Gatling won't save access token, Gatling Scala:Unable to send auth token to the method using session variable, Gatling - Setting Authorization header as part of request and don't see what I'm doing wrong

1

1 Answers

0
votes

Lots of things:

  1. Both logIn2 and getAllDeals are missing dots to properly chain the different method calls. As an example, here's what your logIn2 is actually doing (properly formatting helps):
def logIn2() = {
  val discardedResult = exec(http("Log in")
    .post(getUrl() + "login")
    .header("Content-Type", "application/json")
    .body(ElFileBody("bodies/Login.json")).asJson
    .check(status.is(200))
    .check(jsonPath("$.access_token").saveAs("accessToken")))
  return exec(
    session => {
      accessValue = session("accessToken").as[String]
      session
    })
}
  1. You probably shouldn't be using a global var. You don't need it and such construct is not threadsafe, so under load, virtual users will be updating and reading from this reference concurrently and result will be a mess.

  2. You're missing a space in your Authorization header, between Bearer and the actual value.

  3. asJson is merely a shortcut for .header("Content-Type", "application/json"), so you setting this header twice.

class GetDealsTests extends BaseSimulationTest {

  val logIn2 =
    exec(http("Log in")
      .post(getUrl() + "login")
      .body(ElFileBody("bodies/Login.json")).asJson
      .check(status.is(200))
      .check(jsonPath("$.access_token").saveAs("accessToken")))

  val getAllDeals =
     exec(
        http("Get all deals")
          .get("deals/all")
            .header("Authorization", "Bearer ${accessToken}")
            .check(status.is(200))
          
      )


  val scnGetAllDeals = scenario("Get All Deals endpoint tests")
    .forever {
      exec(logIn2)
      .exec(getAllDeals)
    }

  setUp(
    scnGetAllDeals.inject(
      nothingFor(5 seconds),
      atOnceUsers(users))

  ).protocols(httpConf.inferHtmlResources())
  .maxDuration(FiniteDuration(duration.toLong, MINUTES))

}