Basic Auth on requests depending on environment

Hi,

I’m currently rewriting our Jmeter setup into Gatling. First results look promising.
I have the following question. On our 4 environments (local dev, test, acc, prod) we have different needs with respect to basic authentication.

For example on test & acc environment we need basicAuth(“user”,“pass”) on every Http request. Local-dev & prod this is not necessary.
I was trying to setup somehing like

.http(“url”).exec( session =>
if (env = ‘acc’) {
get(“get url”).basicAuth(“user”,“pass”)
} else
{
get(“get url”) }

)

// above is pseudo code
I can’t get it to work, I also used the doIf but the use case for that is a different I guess. Is there a way to accomplish what I want or do I need to define all requests twice (the set with and without basic Auth)?

tia,

regards,

Alex

Hi,

You can do something similar to
https://github.com/excilys/gatling/wiki/Handling-JSF

val env = Option(System.getProperty("env")).getOrElse("dev")

def authenticatedGet(name: EvaluatableString, url: EvaluatableString) = {
  val regularGet = http(name).get(url)
  env match {
    case "dev" => regularGet.basicAuth("user","pass")
    case "prod" => regularGet
  }
}

Cheers,

Stéphane

thanks, I was just working out something similar. I’m lazy and hoped for something generic on the http object :wink:
But this will work.

(will by any change in the future basicAuth be supported on the baseUrl?)

Alex

HttpProtocolConfig currently contains only static configuration, meaning that it doesn’t depend on the user. If we stick to this design, we could provide a way to set up a basic auth there, but credentials would be the same for all the users.

BTW, I assumed that the credentials were user dependent, is that right? If not, you can set up the Authorization header on the HttpProtocolConfig, but you’ll have to encode it yourself.

Stéphane

Hi,

The basicAuth is same for every user (just for test & acc).
So I can try the HttpProtocolConfig.

I had this solution for now:

class Requests(env:Env) {

val authenicate_user = “secret”
val authenticate_pass = “alsosecret”

val authenticateURL = env match {
case TST => true
case ACC => true
case _ => false
}

def getrequest(name:String, url:String):GetHttpRequestBuilder = http(name).get(url)

def postrequest(name:String, url:String) = http(name).post(url)

def authenticatedGetRequest(name:String, url:String) = getrequest(name,url).basicAuth(authenicate_user,authenticate_pass)

def authenticatedPostRequest(name:String, url:String) = postrequest(name,url).basicAuth(authenicate_user,authenticate_pass)

def request(name:String, url:String,verb:Verb) = (authenticateURL,verb) match {
case (false,GET) => getrequest(name,url)
case (false,POST) => postrequest(name,url)
case (true,GET) => authenticatedGetRequest(name,url)
case (true,POST) => authenticatedGetRequest(name,url)
}

val homepage = request(“homepage”,"/",GET)

val bidOnLot =
request(“bid on lot”,"/lots/bid/4/61", GET).queryParam(“prefillNextBidAmount”,“true”).check(status.is(302))

calling in my Scenarios file exec(requests.homepage)

Works fine.

exec(requests.bidOnLot) fails with

overloaded method value exec with alternatives:
(chains: Iterable[com.excilys.ebi.gatling.core.structure.ChainBuilder])com.excilys.ebi.gatling.core.structure.ScenarioBuilder
(chains: Iterator[com.excilys.ebi.gatling.core.structure.ChainBuilder])com.excilys.ebi.gatling.core.structure.ScenarioBuilder
(chains: com.excilys.ebi.gatling.core.structure.ChainBuilder*)com.excilys.ebi.gatling.core.structure.ScenarioBuilder
(actionBuilder: com.excilys.ebi.gatling.core.action.builder.ActionBuilder)com.excilys.ebi.gatling.core.structure.ScenarioBuilder
(sessionFunction: com.excilys.ebi.gatling.core.session.Session => com.excilys.ebi.gatling.core.session.Session)com.excilys.ebi.gatling.core.structure.ScenarioBuilder
cannot be applied to (ScalaObject)
15:14:15.655 [ERROR] c.e.e.g.a.ZincCompiler$ - .exec(requests.bidOnLot)
15:14:15.656 [ERROR] c.e.e.g.a.ZincCompiler$ - ^
15:14:15.701 [ERROR] c.e.e.g.a.ZincCompiler$ - one error found
Exception in thread “main” Compilation failed

I do not quite understand, the only difference is I enriched the httpGetBuilder instance with queryParam and a check.

Alex

You messing with the type system in your request method. It return either GET requests or POST requests, but those are different types as the available methods are not the same.

Got it.

Thanks.

regards,

Alex