Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to delete cookies from CookieJar #2476

Closed
imiric opened this issue Apr 4, 2022 · 1 comment
Closed

Add a way to delete cookies from CookieJar #2476

imiric opened this issue Apr 4, 2022 · 1 comment
Labels
Milestone

Comments

@imiric
Copy link
Contributor

imiric commented Apr 4, 2022

Feature Description

Currently (v0.37.0) there's no way to delete a cookie from the CookieJar. Users can override a cookie by passing a local jar (http.CookieJar) or a cookies parameter per request, but there's no easy way of actually deleting a cookie from the Cookie header.

See this forum post for the use case.


Setting the cookie on the VU jar to null actually sends it as an empty string:

import http from 'k6/http';

export default function () {
  http.get('https://httpbin.test.k6.io/cookies/set?one=1&two=2');
  const jar = http.cookieJar();  // get the VU specific jar
  jar.set('https://httpbin.test.k6.io/cookies', 'one', null);
  const res = http.get('https://httpbin.test.k6.io/cookies');
  console.log(JSON.stringify(res.json().cookies));
}
Output
INFO[0001] Request:
GET /cookies HTTP/1.1
Host: httpbin.test.k6.io
User-Agent: k6/0.37.0 (https://k6.io/)
Cookie: one=; two=2
Accept-Encoding: gzip

INFO[0001] Response:
HTTP/1.1 200 OK
Content-Length: 54
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Type: application/json
Date: Mon, 04 Apr 2022 13:55:47 GMT

{
  "cookies": {
    "one": "",
    "two": "2"
  }
}

INFO[0001] {"one":"","two":"2"}                          source=console

Similarly setting both the name and value to null (jar.set('https://httpbin.test.k6.io/cookies', null, null)) sends a new cookie with both empty values:

Output
INFO[0001] Request:
GET /cookies HTTP/1.1
Host: httpbin.test.k6.io
User-Agent: k6/0.37.0 (https://k6.io/)
Cookie: one=1; two=2; =
Accept-Encoding: gzip

INFO[0001] Response:
HTTP/1.1 200 OK
Content-Length: 68
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Type: application/json
Date: Mon, 04 Apr 2022 14:02:00 GMT

{
  "cookies": {
    "": "",
    "one": "1",
    "two": "2"
  }
}

INFO[0001] {"":"","one":"1","two":"2"}                   source=console

Suggested Solution (optional)

Setting a cookie to null with jar.set(url, 'cookie', null) could be a way to delete a specific cookie. If the user wanted to send an empty string they could just set the value to '' instead.

Similarly jar.set(url, null, null) could be used to delete all cookies for that URL, or jar.set(null, null, null) to delete all cookies in the jar.

A more intuitive approach suggested by @mstoykov would be to add another method, e.g. CookieJar.delete, to make this more explicit.

Already existing or connected issues / PRs (optional)

No response

@imiric imiric added the feature label Apr 4, 2022
@stevenebutler
Copy link

stevenebutler commented Apr 25, 2022

While looking at this issue, I would sometimes find it useful to be able to clear the cookie jar completely, but was unable to find a way to do that.

My use case would be where I want each iteration to use the same vuser, but on error with the site, fail() out of the iteration, clear all cookies when the iteration fails and login again, possibly with a new user id.

Possible API:

    http.cookieJar().clear()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants