failure friendly dsl constructs

Hi,

Given the following builder

val postBlaBla =
http(“post bla bla”)

.get(“/bla”)
.header(Authorization, “Bearer ${bearerToken}”)

.body(StringBody( “”"

{
“blabla” : ${bla},
}

“”".stripMargin))
.check(status is 201)

I want to DRY my code so that it handles both the case when I have some expressions in session (using oauth, in this case) and the case when I don’t.

Specifically, I wanted something like a headerIfPresent(name: String, value: Expression[String]) method available that does nothing when the evaluation of the expression fails.

I wonder what the idiomatic / elegant way of doing this might be.

Thanks.

Vasco

Wrap in a doIf block such as doIf(_.countains(“bearerToken”)) {}

I also added a feature request that would be very easy to implement, so you could write doIf("${bearerToken.exist()}")) {}
https://github.com/gatling/gatling/issues/2165

Humm…

That forces me to separate my logic into chains where I would need different ScenarioBuilder for both cases differing in only one instruction (not DRY), doesn’t it?

I wanted something more like the following (from the BasicSimulation in the docs)

val scn = scenario(“BasicSimulation”) // 7
.exec(http(“request_1”) // 8
.get(“/”)
.doIf(_.contains(“bearerToken”)) {
_.header(Authorization, “${bearerToken}”)
}
) // 9
.pause(5) // 10

where the result of the doIf would be this (the builder where it is called on) in case of failure or the result of the closure in case of success.

I was trying to hack a way to implicitly “Pimp My Builder” with something along the lines of the following:

class PimpedHttpRequestBuilder[B <: RequestBuilder[B]](builder: B) {

def doIfSuccess(expression: Expression[_])(conditionalStep: (B) => B): B = {

if(verifyExpression(expression)) builder else conditionalStep(builder)
}

def verifyExpression(exp: Expression[_]) = true // need the session
}

object PimpMyGatling {
implicit def httpRequestBuilder2PimpedHttpRequestBuilder[B <: RequestBuilder[B]](builder: B): PimpedHttpRequestBuilder[B] = new PimpedHttpRequestBuilder(builder)
}

which would allow me to write

import PimpMyGatling._
val scn = scenario(“BasicSimulation”) // 7
.exec(http(“request_1”) // 8
.get(“/”)
.doIfSuccess(“${bearerToken}”) {_.header(Authorization, “${bearerToken}”)}
) // 9
.pause(5) // 10

…if only I figure out a way to use the session to verify the expression.

Thoughts?

Yours,

Vasco

Ah, ok, I didn’t get it right.

Like this?

object Pimps {

import io.gatling.core.session._
import io.gatling.http.request.builder._

implicit class PimpedHttpRequestBuilder(val request: HttpRequestBuilder) extends AnyVal {

def pimp(condition: Expression[Boolean], onTrue: HttpRequestBuilder => HttpRequestBuilder) =
doIfOrElse(condition) {
exec(onTrue(request))
} {
exec(request)
}
}
}

import Pimps._
exec(req.pimp(_.contains(“bearerToken”), _.header(Authorization, “${bearerToken}”)))