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

feat: clear and delete cookies [#2476] #2509

Merged
merged 9 commits into from
May 18, 2022
Merged
29 changes: 29 additions & 0 deletions js/modules/k6/http/cookiejar.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,32 @@ func (j CookieJar) Set(url, name, value string, opts goja.Value) (bool, error) {
j.Jar.SetCookies(u, []*http.Cookie{&c})
return true, nil
}

// Clear all cookies for a particular URL
func (j CookieJar) Clear(url string, opts goja.Value) (bool, error) {
Maksimall89 marked this conversation as resolved.
Show resolved Hide resolved
u, err := neturl.Parse(url)
if err != nil {
return false, err
}

cookies := j.Jar.Cookies(u)
for _, c := range cookies {
c.MaxAge = -1
}
j.Jar.SetCookies(u, cookies)

return true, nil
}

// Delete cookies for a particular URL
func (j CookieJar) Delete(url, name string, opts goja.Value) (bool, error) {
Maksimall89 marked this conversation as resolved.
Show resolved Hide resolved
u, err := neturl.Parse(url)
if err != nil {
return false, err
}

c := http.Cookie{Name: name, MaxAge: -1}
j.Jar.SetCookies(u, []*http.Cookie{&c})

return true, nil
}