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)
domain
is optional, defaulting to base url domainpath
is optional, defaulting to “/”maxAge
is and optional number of seconds, defaulting toLong.MinValue
secure
is 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")
domain
is optional. If undefined, defaults to the domain of thebaseUrl
defined in theHttpProtocol
. In this case, fail if thebaseUrl
is undefined. Matching is based on RFC6265’s domain matching algorithm.path
is optional. If defined, match based on RFC6265’s path matching algorithm. Otherwise, always match.secure
is optional. If defined, match based on the cookie’s secure attribute. Otherwise, always match.saveAs
is optional, defaults toname
param
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)
Dealing with Caching
Flushing the Cache
Flush the virtual user’s whole HTTP cache.
exec(flushHttpCache());
exec(flushHttpCache());
exec(flushHttpCache())
exec(flushHttpCache)