Java - Move the HTTP headers to a separate class file.

113 views
Skip to first unread message

Bharath Marrivada

unread,
Feb 13, 2022, 9:31:37 AM2/13/22
to Gatling User Group
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.

Stéphane LANDELLE

unread,
Feb 13, 2022, 9:34:28 AM2/13/22
to gat...@googlegroups.com
Simply do a constant (static final).

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/517ab2cc-f6fb-4753-926a-2e402287ad23n%40googlegroups.com.

Bharath Marrivada

unread,
Feb 13, 2022, 12:10:49 PM2/13/22
to Gatling User Group
Hello Stephane,

Request you to elaborate with an example.

Regards,
Bharath.

Joseph Pollone

unread,
Feb 13, 2022, 2:40:03 PM2/13/22
to Gatling User Group
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");

Bharath Marrivada

unread,
Feb 14, 2022, 2:06:48 AM2/14/22
to Gatling User Group
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.

Bharath Marrivada

unread,
Feb 14, 2022, 2:06:59 AM2/14/22
to Gatling User Group
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.



On Monday, February 14, 2022 at 1:10:03 AM UTC+5:30 jpol...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages