HTTP Helpers
Helpers for handling cookies and caches
Dealing with cookies
Gatling supports cookies out-of-the-box and transparently, just like a browser would.
However, some use cases require a more fine grain control.
Adding a cookie
One might want to manually add or compute a cookie:
exec(addCookie(Cookie("name", "value")));exec(addCookie(Cookie("name", "value")));exec(addCookie(Cookie("name", "value")))exec(addCookie(Cookie("name", "value")))Cookie can also take more optional parameters:
// with static values
Cookie("name", "value")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true);
// with Gatling EL strings
Cookie("#{name}", "#{value}")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true);
// with functions
Cookie(
session -> session.getString("cookieName"),
session -> session.getString("cookieValue")
)
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true);// with static values
Cookie("name", "value")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true);
// with Gatling EL strings
Cookie("#{name}", "#{value}")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true);
// with functions
Cookie(
(session) => session.get("cookieName"),
(session) => session.get("cookieValue")
)
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true);// with static values
Cookie("name", "value")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true)
// with Gatling EL strings
Cookie("#{name}", "#{value}")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true)
// with functions
Cookie(
{ session -> session.getString("cookieName") },
{ session -> session.getString("cookieValue") }
)
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true)// with static values
Cookie("name", "value")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true)
// with Gatling EL strings
Cookie("#{name}", "#{value}")
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true)
// with functions
Cookie(
session => session("cookieName").as[String],
session => session("cookieValue").as[String]
)
.withDomain("domain")
.withPath("path")
.withMaxAge(10)
.withSecure(true)domainis optional, defaulting to base url domainpathis optional, defaulting to “/”maxAgeis and optional number of seconds, defaulting toLong.MinValuesecureis optional, defaulting to false, meaning it’s valid for both http and https urls
Getting a cookie Value
Get the cookie value and put it in the session
exec(getCookieValue(CookieKey("name")));exec(getCookieValue(CookieKey("name")));exec(getCookieValue(CookieKey("name")))exec(getCookieValue(CookieKey("name")))CookieKey can also take more optional parameters:
// with static values
CookieKey("name")
.withDomain("domain")
.withPath("path")
.withSecure(true)
.saveAs("key");
// with Gatling EL strings
CookieKey("#{name}")
.withDomain("#{domain}")
.withPath("path")
.withSecure(true)
.saveAs("key");
// with functions
CookieKey(session -> session.getString("cookieName"))
.withDomain(session -> session.getString("cookieDomain"))
.withPath("path")
.withSecure(true)
.saveAs("key");// with static values
CookieKey("name")
.withDomain("domain")
.withPath("path")
.withSecure(true)
.saveAs("key");
// with Gatling EL strings
CookieKey("#{name}")
.withDomain("#{domain}")
.withPath("path")
.withSecure(true)
.saveAs("key");
// with functions
CookieKey((session) => session.get("cookieName"))
.withDomain((session) => session.get("cookieDomain"))
.withPath("path")
.withSecure(true)
.saveAs("key");// with static values
CookieKey("name")
.withDomain("domain")
.withPath("path")
.withSecure(true)
.saveAs("key")
// with Gatling EL strings
CookieKey("#{name}")
.withDomain("#{domain}")
.withPath("path")
.withSecure(true)
.saveAs("key")
// with functions
CookieKey { session -> session.getString("cookieName") }
.withDomain { session -> session.getString("cookieDomain") }
.withPath("path")
.withSecure(true)
.saveAs("key")// with static values
CookieKey("name")
.withDomain("domain")
.withPath("path")
.withSecure(true)
.saveAs("key")
// with Gatling EL strings
CookieKey("#{name}")
.withDomain("#{domain}")
.withPath("path")
.withSecure(true)
.saveAs("key")
// with functions
CookieKey(session => session("cookieName").as[String])
.withDomain(session => session("cookieDomain").as[String])
.withPath("path")
.withSecure(true)
.saveAs("key")domainis optional. If undefined, defaults to the domain of thebaseUrldefined in theHttpProtocol. In this case, fail if thebaseUrlis undefined. Matching is based on RFC6265’s domain matching algorithm.pathis optional. If defined, match based on RFC6265’s path matching algorithm. Otherwise, always match.secureis optional. If defined, match based on the cookie’s secure attribute. Otherwise, always match.saveAsis optional, defaults tonameparam
Flushing session cookies
Simulate closing a browser, so session cookies are dropped but not permanent cookies.
exec(flushSessionCookies());exec(flushSessionCookies());exec(flushSessionCookies())exec(flushSessionCookies)Flushing all cookies
Flush the whole CookieJar.
exec(flushCookieJar());exec(flushCookieJar());exec(flushCookieJar())exec(flushCookieJar)Flushing the HTTP cache
Flush the virtual user’s whole HTTP cache: known redirects, known expires and ETag.
exec(flushHttpCache());exec(flushHttpCache());exec(flushHttpCache())exec(flushHttpCache)