Java - Move the HTTP headers to a separate class file

Hello All,

There are many header map entries in the class file, which is making my script very long and unreadable. Decided to move all the header map entries to a separate class file and “Extends” it to the main class.

There is already a “simulation” class extended to the main class and Java doesn’t allow to extend more than one class.

Any suggestions on how to move the HTTP headers to a separate class file?

Regards,
Bharath.

Simply do a constant (static final).

Hello Stephane,

Request you to elaborate with an example.

Regards,
Bharath.

Probably like this:

public class Helpers {

public static HttpProtocolBuilder httpProtocol(String productBaseUrl) {
return http
.baseUrl(productBaseUrl)
.inferHtmlResources(AllowList(), DenyList(".\.js", ".\.css", “.\.gif", ".\.jpeg”, “.\.jpg", ".\.ico”, “.\.woff", ".\.woff2”, “.\.(t|o)tf", ".\.png”, “.detectportal\.firefox\.com.”))
.acceptHeader("/")
.acceptEncodingHeader(“gzip, deflate”)
.acceptLanguageHeader(“en-US,en;q=0.5”)
.doNotTrackHeader(“1”)
.userAgentHeader(“Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0”);
}

}

And in your runner file:

HttpProtocolBuilder httpProtocol = Helpers.httpProtocol(“www.blahblah.com”);

I am looking to move all the HTTP headers as shown in the below example to a separate class file so that the main class file is readable.
Stephane suggested using “constant (static final)”, I am not sure how to implement it.

private Map<CharSequence, String> headers_89 = Map.ofEntries(
Map.entry(“Accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8”),

);

Regards,
Bharath.

I found the solution, following is the implementation

Created a separate class file with all the HTTP header maps shown below example

package webapps;
import io.gatling.javaapi.http.HttpProtocolBuilder;
import java.util.Map;
import static io.gatling.javaapi.http.HttpDsl.http;

public final class headers {

public static final Map<CharSequence, String> headers_90 = Map.ofEntries(
Map.entry(“Accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8”),
Map.entry(“Upgrade-Insecure-Requests”, “1”)

public static final Map<CharSequence, String> headers_91 = Map.ofEntries(
Map.entry(“Accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8”),
Map.entry(“Upgrade-Insecure-Requests”, “1”)

);

In the main class, I have imported the above-mentioned class as shown below.

import static webapps.headers.*;

Now the main class file is readable.

Regards,
Bharath.