菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/httpbreadcrumb arrow CookieJarbreadcrumb arrow CookieJar.set(url, name, value, [options])
开源

CookieJar.set(url, name, value, [options])

通过指定 url、name、value 和其他可选设置(如 domain、path 等)在 CookieJar 中设置 Cookie。

参数类型描述
urlstringCookie URL
namestringCookie 名称
valuestringCookie 值
options (可选)object特定 Cookie 设置:domainpathexpiresmax_agesecurehttp_only

示例

JavaScript
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',
  });
}