I did some more tests - it works in setup as well
I would guess that the cookie is not actually set for res.url
but for some parent URL, lets say your domain is “example.com”.
What I think happens is that your actual cookie(s) is set for “example.com/something/”
If you ask for cookies for “example.com/something/” or “example.com/something/else/” you will get that cookie as this is how … cookies work . If you ask for cookies for “example.com” you will get nothing, as the cookie is only set for path=“something”. You can read more in section “scope of cookies” on mdn.
I would guess in your case you make a request for “example.com/something/else/” and it will use the cookie for “example.com/something/” and then you set a cookie with the same name but then you reset the cookie for “example.com/something/else/”. But this is a different cookie so you basically made a new cookie and set it to expire in the past.
Code example:
import http from "k6/http";
import { check, group } from "k6";
export let options = {
maxRedirects: 3
};
const printCookies = (jar, domains) =>
domains.forEach((i) => {
const cookies = jar.cookiesForURL("http://" + i);
console.log(i + ":" + JSON.stringify(cookies))
});
export default function() {
let domains = [
"example.com",
"example.com/something/",
"example.com/something/else/",
];
let vuJar = http.cookieJar();
vuJar.set(
"http://example.com/something/",
"name1",
"something",
);
printCookies(vuJar, domains);
vuJar.set(
"http://example.com/something/else/",
"name1",
"else",
);
printCookies(vuJar, domains);
vuJar.set(
"http://example.com/something/else/",
"name1",
"whatever",
{"expires": "Mon, 02 Jan 2006 15:04:05 MST"}
);
printCookies(vuJar, domains);
vuJar.set(
"http://example.com/something/",
"name1",
"whatever",
{"expires": "Mon, 02 Jan 2006 15:04:05 MST"}
);
printCookies(vuJar, domains);
}
Running will print:
INFO[0001] example.com:{}
INFO[0001] example.com/something/:{"name1":["something"]}
INFO[0001] example.com/something/else/:{"name1":["something"]}
INFO[0001] example.com:{}
INFO[0001] example.com/something/:{"name1":["something"]}
INFO[0001] example.com/something/else/:{"name1":["else","something"]}
INFO[0001] example.com:{}
INFO[0001] example.com/something/:{"name1":["something"]}
INFO[0001] example.com/something/else/:{"name1":["something"]}
INFO[0001] example.com:{}
INFO[0001] example.com/something/:{}
INFO[0001] example.com/something/else/:{}
Chances are that you are not using neither “Domain” nor “Path” when setting cookies from the server. But you will need to check that and reset the time for the specific domain-path combination it is set for.
Hope this helps you