0
votes

I'm completely new to Scala and Gatling, please forgive the basic question!

I want to create an http protocol with baseUrl specified by the result of an initial http request. Or in other words:

  1. Get remote config as JSON, let's say from https://example.com/config.json
  2. Parse JSON, retrieve a specified property endpoint
  3. Pass that value to http.baseUrl()

I can do this on every single scenario manually but this quickly becomes tedious (and is unnecessarily repetitive). I'd like to find a solution where I can perform this setup once at the beginning of the test run.

My instinct is to go for something like this:

object Environment {
  val config = "https://example.com/config.json"
}

val httpProtocol = http("config")
    .get(Environment.config)
    .check(
      jsonPath("$.endpoint").saveAs("endpoint")
    )
    .baseUrl("${endpoint}")

// ... and then later on

setUp(
    // scenario.inject()…
  ).protocols(httpProtocol)

... but that doesn't compile.

Thanks very much for any help.

1

1 Answers

0
votes

What you're proposing won't work.

.protocols takes a HttpProtocolBuilder (documented on the gatling site), whereas you're trying to pass a HttpRequestBuilder

Furthermore, the baseUrl parameter of HttpProtocolBuilder only takes a string, so you won't be able to pass a gatling session value into it.

The only way I can think to do this would be to make the request that returns the base url in the 'before' block, but you won't be able to use gatling dsl constructs to make that request: you'll have to do it with raw scala