Search code examples
javaload-testinggatling

Getting a 403 Forbidden Error in Gatling despite Adding Correct Cookie - Troubleshooting Help Needed


I am working on a load testing script using Gatling for a website, and I've encountered a 403 Forbidden error in the response, even after adding the necessary cookie for authentication. I've double-checked the cookie, headers, and the URL, but I'm still unable to resolve this issue.

package assignment7;

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

import io.gatling.javaapi.core.*;
import io.gatling.javaapi.http.*;

public class AssignmentSimulation extends Simulation {
    
    // http config
    private HttpProtocolBuilder httpProtocol = http
        .baseUrl("https://staging.com")
        .acceptHeader("application/json, text/plain, */*")
        .acceptEncodingHeader("gzip, deflate, br")
        .acceptLanguageHeader("en-US,en;q=0.9")
        .contentTypeHeader("application/json")
        .userAgentHeader("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.47");
        
    // Scenario definition
    private ScenarioBuilder scn = scenario("Load Testing")
        .exec(http("Accessing Website")
            .get("/")
            .check(status().saveAs("status")))

        .exec(addCookie(Cookie("stgoauth", "ZW1haWw6ZGFybGFAa2FsaWJyci5jb20gdXNlcjo=")
            .withDomain(".staging.com")
            .withPath("/")
            .withSecure(true)))
        
        .exec(session -> {
            int responseStatus = session.getInt("status"); // Retrieve the saved response status
            System.out.println("Response Code: " + responseStatus);
            return session;
        });

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

I would greatly appreciate any guidance or insights on why I'm still receiving a 403 error despite adding the cookie correctly. Is there anything else I might be missing or any common pitfalls in Gatling that I should be aware of? Thank you in advance for your help!


Solution

  • I changed the order of the scenario. Setting cookies first before accessing the website.

        private ScenarioBuilder scn = scenario("Load Testing")
    
            .exec(addCookie(Cookie("stgoauth", "ZW1haWw6ZGFybGFAa2FsaWJyci5jb20gdXNlcjo=")
                .withDomain(".staging.com")
                .withPath("/")
                .withSecure(true)))
    
            .exec(http("Accessing Website")
                .get("/")
                .check(status().saveAs("status")))
            
            .exec(session -> {
                int responseStatus = session.getInt("status"); // Retrieve the saved response status
                System.out.println("Response Code: " + responseStatus);
                return session;
            });