Cookies 示例
关于如何在必要时在负载测试期间与 cookies 交互的脚本示例。
Cookies
由于 HTTP 是一个无状态协议,服务器端应用程序使用 cookies 在客户端机器上持久化数据。这在网络上随处可见,通常用于用户会话跟踪。在 k6 中,默认情况下会自动管理 cookies,但在某些用例中需要读取和操作 cookies。
从响应头
import http from 'k6/http';
import { check, group } from 'k6';
export default function () {
// Since this request redirects the `res.cookies` property won't contain the cookies
const res = http.post('https://quickpizza.grafana.com/api/cookies?name1=value1&name2=value2');
check(res, {
'status is 200': (r) => r.status === 200,
});
// Make sure cookies have been added to VU cookie jar
const vuJar = http.cookieJar();
const cookiesForURL = vuJar.cookiesForURL(res.url);
check(null, {
"vu jar has cookie 'name1'": () => cookiesForURL.name1.length > 0,
"vu jar has cookie 'name2'": () => cookiesForURL.name2.length > 0,
});
}
记录响应中的所有 cookies
⚠️ 注意:这仅在本地运行 k6 时有效
console.log()
系列 API 目前仅在本地运行 k6 时可用。使用 LoadImpact Cloud Execution 运行 k6 测试时,日志将被丢弃。
// Example showing two methods how to log all cookies (with attributes) from a HTTP response.
import http from 'k6/http';
function logCookie(cookie) {
// Here we log the name and value of the cookie along with additional attributes.
// For full list of attributes see: https:///grafana.org.cn/docs/k6/using-k6/cookies#properties-of-a-response-cookie-object
console.log(
`${cookie.name}: ${cookie.value}\n\tdomain: ${cookie.domain}\n\tpath: ${cookie.path}\n\texpires: ${cookie.expires}\n\thttpOnly: ${cookie.http_only}`
);
}
export default function () {
const res = http.get('https://www.google.com/');
// Method 1: Use for-loop and check for non-inherited properties
for (const name in res.cookies) {
if (res.cookies.hasOwnProperty(name) !== undefined) {
logCookie(res.cookies[name][0]);
}
}
// Method 2: Use ES6 Map to loop over Object entries
new Map(Object.entries(res.cookies)).forEach((v, k) => logCookie(v[0]));
}
在 VU cookie jar 中设置 cookie
要设置一个应随匹配特定域、路径等的每个请求发送的 cookie,您可以这样做:
import http from 'k6/http';
import { check } from 'k6';
export default function () {
// Get VU cookie jar and add a cookie to it providing the parameters
// that a request must match (domain, path, HTTPS or not etc.)
// to have the cookie attached to it when sent to the server.
const jar = http.cookieJar();
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie', 'hello world', {
domain: 'quickpizza.grafana.com',
path: '/api/cookies',
secure: true,
max_age: 600,
});
// As the following request is matching the above cookie in terms of domain,
// path, HTTPS (secure) and will happen within the specified "age" limit, the
// cookie will be attached to this request.
const res = http.get('https://quickpizza.grafana.com/api/cookies');
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',
});
}
在 VU cookie jar 中删除 cookie
要删除特定 URL 和名称的 jar 中的 cookie,请使用 delete
方法。
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const jar = http.cookieJar();
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie_1', 'hello world_1');
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie_2', 'hello world_2');
const res1 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res1, {
'res1 has status 200': (r) => r.status === 200,
"res1 has cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 !== null,
'res1 cookie has correct value_1': (r) => r.json().cookies.my_cookie_1 == 'hello world_1',
"res1 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null,
'res1 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2',
});
jar.delete('https://quickpizza.grafana.com/api/cookies', 'my_cookie_1');
const res2 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res2, {
'res2 has status 200': (r) => r.status === 200,
"res2 hasn't cookie 'my_cookie_1'": (r) => r.json().cookies.my_cookie_1 == null,
"res2 has cookie 'my_cookie_2'": (r) => r.json().cookies.my_cookie_2 !== null,
'res2 cookie has correct value_2': (r) => r.json().cookies.my_cookie_2 == 'hello world_2',
});
}
清除 VU cookie jar 中的所有 cookies
要通过指定 URL 清除 jar 中的所有 cookies,请使用 clear
方法。
import http from 'k6/http';
import { check } from 'k6';
export default function () {
const jar = http.cookieJar();
jar.set('https://quickpizza.grafana.com/api/cookies', 'my_cookie', 'hello world');
const res1 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res1, {
'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',
});
jar.clear('https://quickpizza.grafana.com/api/cookies');
const res2 = http.get('https://quickpizza.grafana.com/api/cookies');
check(res2, {
'has status 200': (r) => r.status === 200,
"hasn't cookie 'my_cookie'": (r) => r.json().cookies.my_cookie == null,
});
}
相关 k6 API: