No results for

Powered byAlgolia

Cookies Example

Scripting examples on how you can interact with cookies during your load test if required.

Cookies

As HTTP is a stateless protocol, cookies are used by server-side applications to persist data on client machines. This is used more or less everywhere on the web, commonly for user session tracking purposes. In k6 cookies are managed automatically by default, however, there are use cases where access to read and manipulate cookies are required.

From the response headers

access-cookie.js
1import http from 'k6/http';
2import { check, group } from 'k6';
3
4export default function () {
5 // Since this request redirects the `res.cookies` property won't contain the cookies
6 const res = http.get('https://httpbin.test.k6.io/cookies/set?name1=value1&name2=value2');
7 check(res, {
8 'status is 200': (r) => r.status === 200,
9 });
10
11 // Make sure cookies have been added to VU cookie jar
12 const vuJar = http.cookieJar();
13 const cookiesForURL = vuJar.cookiesForURL(res.url);
14 check(null, {
15 "vu jar has cookie 'name1'": () => cookiesForURL.name1.length > 0,
16 "vu jar has cookie 'name2'": () => cookiesForURL.name2.length > 0,
17 });
18}

Log all the cookies in a response

⚠️ Note that this only works when using k6 locally

The console.log() family of APIs are currently only usable when running k6 locally. When running k6 tests with LoadImpact Cloud Execution the logs will be discarded.

log-all-cookies.js
1// Example showing two methods how to log all cookies (with attributes) from a HTTP response.
2
3import http from 'k6/http';
4
5function logCookie(cookie) {
6 // Here we log the name and value of the cookie along with additional attributes.
7 // For full list of attributes see: https://k6.io/docs/using-k6/cookies#properties-of-a-response-cookie-object
8 console.log(
9 `${cookie.name}: ${cookie.value}\n\tdomain: ${cookie.domain}\n\tpath: ${cookie.path}\n\texpires: ${cookie.expires}\n\thttpOnly: ${cookie.http_only}`
10 );
11}
12
13export default function () {
14 const res = http.get('https://www.google.com/');
15
16 // Method 1: Use for-loop and check for non-inherited properties
17 for (const name in res.cookies) {
18 if (res.cookies.hasOwnProperty(name) !== undefined) {
19 logCookie(res.cookies[name][0]);
20 }
21 }
22
23 // Method 2: Use ES6 Map to loop over Object entries
24 new Map(Object.entries(res.cookies)).forEach((v, k) => logCookie(v[0]));
25}

To set a cookie that should be sent with every request matching a particular domain, path, etc. you'd do something like this:

set-cookie-in-jar.js
1import http from 'k6/http';
2import { check } from 'k6';
3
4export default function () {
5 // Get VU cookie jar and add a cookie to it providing the parameters
6 // that a request must match (domain, path, HTTPS or not etc.)
7 // to have the cookie attached to it when sent to the server.
8 const jar = http.cookieJar();
9 jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world', {
10 domain: 'httpbin.test.k6.io',
11 path: '/cookies',
12 secure: true,
13 max_age: 600,
14 });
15
16 // As the following request is matching the above cookie in terms of domain,
17 // path, HTTPS (secure) and will happen within the specified "age" limit, the
18 // cookie will be attached to this request.
19 const res = http.get('https://httpbin.test.k6.io/cookies');
20 check(res, {
21 'has status 200': (r) => r.status === 200,
22 "has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null,
23 'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world',
24 });
25}

To delete a cookie in the jar for a specific URL and name, use the delete method.

set-cookie-in-jar.js
1import http from 'k6/http';
2import { check } from 'k6';
3
4export default function () {
5 const jar = http.cookieJar();
6 jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_1', 'hello world_1');
7 jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie_2', 'hello world_2');
8
9 const res1 = http.get('https://httpbin.test.k6.io/cookies');
10 check(res1, {
11 'res1 has status 200': (r) => r.status === 200,
12 "res1 has cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 !== null,
13 'res1 cookie has correct value_1': (r) => r.json().cookies.my_cookie_1 == 'hello world_1',
14 "res1 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null,
15 'res1 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2',
16 });
17
18 jar.delete('https://httpbin.test.k6.io/cookies', 'my_cookie_1');
19
20 const res2 = http.get('https://httpbin.test.k6.io/cookies');
21 check(res2, {
22 'res2 has status 200': (r) => r.status === 200,
23 "res2 hasn't cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 == null,
24 "res2 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null,
25 'res2 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2',
26 });
27}

To clear all cookies in the jar by specifying url, use the clear method.

set-cookie-in-jar.js
1import http from 'k6/http';
2import { check } from 'k6';
3
4export default function () {
5 const jar = http.cookieJar();
6 jar.set('https://httpbin.test.k6.io/cookies', 'my_cookie', 'hello world');
7 const res1 = http.get('https://httpbin.test.k6.io/cookies');
8 check(res1, {
9 'has status 200': (r) => r.status === 200,
10 "has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null,
11 'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world',
12 });
13
14 jar.clear('https://httpbin.test.k6.io/cookies');
15
16 const res2 = http.get('https://httpbin.test.k6.io/cookies');
17 check(res2, {
18 'has status 200': (r) => r.status === 200,
19 "hasn't cookie 'my_cookie'": (r) => r.json().cookies.my_cookie == null,
20 });
21}

Relevant k6 APIs: