k6 community forum

Access response cookie set under a different domain

I have a get request with a goto parameter like
https://example.com?goto=test.com

After the request is sent, it sets cookies under both the domains (test.com and example.com)

Problem is in a K6 test, below code prints the cookies set under test.com and not the cookies under domain example.com

var res = http.get(https://example.com?goto=test.com);  
console.log(JSON.stringify(res.cookies));

How do one get the cookies set under the domain example.com ?

Hi @jeevananthank,

You can use http.cookieJar - the first example shows you how to get the cookies for any URL :wink:

1 Like

@mstoykov is there any way to delete a cookie using cookieJar ?

Kind of … you can make it expire by setting the expires in the past as shown below:

import http from "k6/http";
import { check, group } from "k6";

export let options = {
    maxRedirects: 3
};

export default function() {
    // Since this request redirects the `res.cookies` property won't contain the cookies
    let res = http.get("http://httpbin.org/cookies/set?name3=value3&name4=value4");
    check(res, {
        "status is 200": (r) => r.status === 200
    });

    // Make sure cookies have been added to VU cookie jar
    let vuJar = http.cookieJar();
    let cookiesForURL = vuJar.cookiesForURL(res.url);
    check(null, {
        "vu jar has cookie 'name3'": () => cookiesForURL.name3.length > 0,
        "vu jar has cookie 'name4'": () => cookiesForURL.name4.length > 0
    });
    res = http.get("http://httpbin.org/cookies");
    console.log(res.body);
    vuJar.set(res.url, "name4", "whatever", {"expires": "Mon, 02 Jan 2006 15:04:05 MST"});

    res = http.get("http://httpbin.org/cookies");
    console.log(res.body);
}
export function clearCookies(){
var jar = http.cookieJar();
console.log(JSON.stringify(jar.cookiesForURL(res.url)));
jar.set(res.url, "name1", "test", {"expires": "Mon, 02 Jan 2006 15:04:05 MST"});
jar.set(res.url, "name2", "test", {"expires": "Mon, 02 Jan 2006 15:04:05 MST"});
console.log(JSON.stringify(jar.cookiesForURL(res.url)));
};

i am using the above code in a function, where i expect the 2nd console.log() to print {}
but it still prints “name1” and “name2” with its values

and to Note that am calling this function from a setup function and NOT under default function() as you mentioned. @mstoykov

I did some more tests - it works in setup as well :smiley:

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 :smiley: . 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
};

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");
    domains.forEach((i) =>  { console.log(i + ":" + JSON.stringify(vuJar.cookiesForURL("http://" + i))) });

    vuJar.set("http://example.com/something/else/", "name1", "else");
    domains.forEach((i) =>  { console.log(i + ":" + JSON.stringify(vuJar.cookiesForURL("http://" + i))) });

    vuJar.set("http://example.com/something/else/", "name1", "whatever", {"expires": "Mon, 02 Jan 2006 15:04:05 MST"});
    domains.forEach((i) =>  { console.log(i + ":" + JSON.stringify(vuJar.cookiesForURL("http://" + i))) });

    vuJar.set("http://example.com/something/", "name1", "whatever", {"expires": "Mon, 02 Jan 2006 15:04:05 MST"});
    domains.forEach((i) =>  { console.log(i + ":" + JSON.stringify(vuJar.cookiesForURL("http://" + i))) });
}

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 :wink:

1 Like

@mstoykov it helped. Thanks alot. the cookies were set at a parent level and i was trying to access from a child level.

1 Like