CookieJar.set(url, name, value, [options])
通过指定 url、name、value 和其他可选设置(如 domain、path 等)在 CookieJar 中设置 Cookie。
参数 | 类型 | 描述 |
---|---|---|
url | string | Cookie URL |
name | string | Cookie 名称 |
value | string | Cookie 值 |
options (可选) | object | 特定 Cookie 设置:domain 、path 、expires 、max_age 、secure 和 http_only 。 |
示例
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const jar = http.cookieJar();
jar.set('http://quickpizza.grafana.com', 'my_cookie', 'hello world', {
domain: 'quickpizza.grafana.com',
path: '/api/cookies',
secure: true,
max_age: 600,
});
const res = http.get('https://quickpizza.grafana.com/api/cookies');
console.log(res.body);
check(res, {
'has status 200': (r) => r.status === 200,
"has cookie 'my_cookie'": (r) => r.json().cookies.my_cookie !== null,
'cookie has correct value': (r) => r.json().cookies.my_cookie == 'hello world',
});
}