How to do an IF statement and choosing protocoll based on input from command line

I want to be able to specify to use a proxy or not for my Simulation class.

I have this:

class KubtestSimulation extends Simulation {

val myProperty = sys.props.get(“gatling.Miljo”).mkString

val proxy = sys.props.get(“gatling.Proxy”).mkString

println("Miljo: " + myProperty)
println("Proxy: " + proxy)

val httpProtocolProxy = http.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.noProxyFor(“kubtest.kj.nhn.no”)
.acceptHeader(“application/json, text/javascript, /; q=0.01”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1”)
.doNotTrackHeader(“1”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0”)
.baseUrl(myProperty)

val httpProtocol = http
.baseUrl(myProperty)
.inferHtmlResources(BlackList("""..js""", “”"..css""", “”"..gif""", “”"..jpeg""", “”"..jpg""", “”"..ico""", “”"..woff""", “”"..woff2""", “”"..(t|o)tf""", “”"..png""", “”".detectportal.firefox.com."""), WhiteList())
.acceptHeader(“application/json, text/javascript, /; q=0.01”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1”)
.doNotTrackHeader(“1”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0”)
.silentUri(“https://myCDN/.*”)

How do I say:

//if ${proxy} true

//setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocolProxy)
//Else
//setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)

var proxy = Try(sys.props.get("gatling.Proxy").mkString.toBoolean).getOrElse(false)
//Defalut false incase of conversion errors

Thanks, But doing what you suggest gives men this error: “Cannot resolve overloaded method ´Try´”
Must I include something?

Yes, it requires "import scala.util.Try "


Thanks, that did work. However I just got another problem. I get this error when running:

mvn gatling:test -Dgatling.simulationClass=kjernejournal.KubtestSimulation -Dgatling.Miljo=https://utv2.kj.nhn.no -Dgatling.Proxy=false

Error:

22:44:39.118 [ERROR] i.g.a.Gatling$ - Run crashed

java.net.UnknownHostException: proxy.drift.nhn.no: nodename nor servname provided, or not known

at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)

My (proxy-protocol) definition is:

val httpProtocolProxy = http.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.baseUrl(myProperty)
.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.acceptHeader(“application/json, text/javascript, /; q=0.01”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1”)
.doNotTrackHeader(“1”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0”)

Observations:

  1. passing -Dgatling.proxy=false. Should it be using the httpProtocol with proxy or no proxy? Check it out2) Based on the error, sounds like httpproxy server is not accessible from the machine the Gatling tests are running from. Check if it’s accessible on the machine and also see if the port is accessible as well.
  2. The usage proxy is a bit confusing ( proxy is defined twice ). You do not need to define it twice. Check the usage from here: https://gatling.io/docs/current/http/http_protocol/#proxy-parameters
  3. Check if the credentials are needed for the proxy server

val httpProtocolProxy = http.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.baseUrl(myProperty)
.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.acceptHeader(“application/json, text/javascript, /; q=0.01”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1”)
.doNotTrackHeader(“1”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0”)

Change the definition as follows:

val httpProtocolProxy = http.
.baseUrl(myProperty)
.proxy(Proxy(“proxy.drift.nhn.no”, 8080))
.acceptHeader(“application/json, text/javascript, /; q=0.01”)
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1”)
.doNotTrackHeader(“1”)
.userAgentHeader(“Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0”)