setResponseCallback( callback )
设置响应回调,以确定响应是否符合预期/成功。
其结果是,请求将被标记为 expected_response
"true" 或 "false",http_req_failed
将以相反的方式发出。这包括所有重定向,因此如果一个请求只期望 200 状态码,而在此之前发生了 301 重定向,则该重定向将被标记为失败,因为只有状态码 200 被标记为期望的。
例外情况
由于具体的实现细节
- 使用
digest
认证的请求,总是期望先获得 401,然后再获得指定的内容。 - 使用
ntlm
认证的请求,第一次请求允许 401 状态码,以及由expectedStatuses
定义的任何其他内容。
参数 | 类型 | 描述 |
---|---|---|
callback | expectedStatuses | 一个从 expectedStatuses 返回的对象 |
目前仅支持非常特殊的 expectedStatuses 对象,但未来计划也支持 JavaScript 回调。默认情况下,状态码在 200 到 399 之间的请求被认为是“期望的”。
将回调设置为 null
会禁用 expected_response
的标记以及 http_req_failed
的发出。
建议如果将 per request responseCallback 与 Params 一起使用,则应将其定义一次并使用,而不是在每个请求上创建它。
示例
import http from 'k6/http';
http.setResponseCallback(http.expectedStatuses({ min: 200, max: 300 }));
const only300Callback = http.expectedStatuses(300);
export default () => {
// this will use the default response callback and be marked as successful
http.get('https://quickpizza.grafana.com/api/status/200');
// this will be marked as a failed request as it won't get the expected status code of 300
http.get('https://quickpizza.grafana.com/api/status/200', {
responseCallback: only300Callback,
});
http.setResponseCallback(http.expectedStatuses(301));
// from here on for this VU only the 301 status code will be successful so on the next iteration of
// the VU the first request will be marked as failure
};