Gatling proxy configuration from command line

Hi, I need to configure HTTP proxy conditionally based on environment where Gatling runs . I tried to use
gatling.http.proxy.host and gatling.http.proxy.port arguments but it seems they are deprecated.
I use the latest version of Gatling 3.8.3
Can anyone help how to enable/disable proxy conditionally?
Thanks fro help

Let me know if this helps. Please pretty please, give us something to work with, like for example, what have your written so far, what language are you using.

import io.gatling.core.Predef._
import io.gatling.core.scenario.Simulation
import io.gatling.http.Predef._

class Community extends Simulation {

  val httpProtocol = {
    val hp = http.baseUrl("foo")
    sys.env.get("enableProxy") match {
      case Some(_) => hp.proxy(Proxy("host", 1234))
      case None => hp
    }
  }

  //rest of your code

}
1 Like

In JAVA and Maven below.
I’m wondering if it’s possible to do it better :slight_smile:

package pl.gemiusz;

import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.http.HttpProtocolBuilder;

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.Proxy;
import static io.gatling.javaapi.http.HttpDsl.http;

/**
 * HOW TO RUN:
 * mvnw gatling:test -Dgatling.simulationClass=pl.gemiusz.Case0011ProxyCommandLineParametersSimulation -Dfoo=10 -Dbar=GeMi -DuseProxy=true -DproxyHost=127.0.0.1 -DproxyPort=8080
 */
public class Case0011ProxyCommandLineParametersSimulation extends Simulation {

    int foo = Integer.getInteger("foo", 1);
    String bar = System.getProperty("bar");


    static boolean useProxy = Boolean.getBoolean("useProxy");
    static String proxyHost = System.getProperty("proxyHost");
    static int proxyPort = Integer.getInteger("proxyPort", 8080);

    static HttpProtocolBuilder httpProtocol = httpProtocolCustom();

    public static HttpProtocolBuilder httpProtocolCustom() {
        HttpProtocolBuilder httpProtocolB =
                http
                        .baseUrl("https://postman-echo.com");

        if (useProxy) {
            if (proxyHost.length() == 0) {
                proxyHost = "127.0.0.1";
            }
            httpProtocolB = httpProtocolB.proxy(Proxy(proxyHost, proxyPort));
        }
        return httpProtocolB;
    }

    ScenarioBuilder scn =
            scenario("GeMi_ProxyCommandLineParametersSimulation")
                    .exec(
                            http("GeMi_ProxyCommandLineParametersSimulation_get")
                                    .get("/get?foo=" + foo + "&bar=" + bar)
                                    .check(jmesPath("args.foo").is(String.valueOf(foo)))
                                    .check(jmesPath("args.bar").is(String.valueOf(bar)))
                    );

    {
        setUp(scn.injectOpen(atOnceUsers(1)).protocols(httpProtocol));
    }
}

The verbosity of Java hurts my eyes :slight_smile: . If there is a for yield mechanism in java then that might simplify things.

This is my code

  val httpProtocol: HttpProtocolBuilder = {

    val hp = http
      .baseUrl(baseUrl)
      .acceptHeader("application/json")
      .doNotTrackHeader("1")
      .acceptLanguageHeader("en-GB")
      .acceptEncodingHeader("gzip, deflate")
      .header("Apikey", apiKey);

    if (Properties.envOrNone("http_proxy_host").nonEmpty) {
      hp.proxy(
        Proxy(Properties.envOrNone("http_proxy_host").get, Properties.envOrNone("http_proxy_port").get.toInt)
      );
    }

    hp

  }

and it won’t work.

First, Gatling DSL components are immutable, see Gatling - Concepts

so your hp.proxy call doesn’t mutate hp, it returns a new instance that you discard.

Then, you’re obviously not familiar with Scala programming idioms (no offense meant). Shouldn’t you go with the Java DSL instead of the Scala one?

One of the correct Scala ways would be:


val httpProtocol: HttpProtocolBuilder = {

    val hp = http
      .baseUrl(baseUrl)
      .acceptHeader("application/json")
      .doNotTrackHeader("1")
      .acceptLanguageHeader("en-GB")
      .acceptEncodingHeader("gzip, deflate")
      .header("Apikey", apiKey);

    (
        for {
          host <- Properties.envOrNone("http_proxy_host")
          port <- Properties.envOrNone("http_proxy_port")
        } yield hp.proxy(Proxy(host, port.toInt)
      ).getOrElse(hp)
  }
1 Like

Now I see it. After refactoring to this

  val httpProtocol = {

    val hp = http
      .baseUrl(baseUrl)
      .acceptHeader("application/json")
      .doNotTrackHeader("1")
      .acceptLanguageHeader("en-GB")
      .acceptEncodingHeader("gzip, deflate")
      .header("Apikey", apiKey);

    if (Properties.envOrNone("http_proxy_host").nonEmpty)
      hp.proxy(
        Proxy(Properties.envOrNone("http_proxy_host").get, Properties.envOrNone("http_proxy_port").get.toInt)
      )
    else
      hp

  }

it then works. Thanks everyone for help

@pavelz use what @slandelle has provided, with the for yield construct. You’ll have an issue with cases where port is not set, in your implementation. A for yield will take care of any unset env properties, (google it and read up on amazing it is)