PTS 支持的配置选项
HTTP 的请求配置,可在全局变量 option 中定义。PTS 支持的配置选项如下表所示:
HTTP 请求配置选项 | 描述 |
maxRedirects | 重定向跳转次数 |
maxIdleConns | 单个 VU 最大活跃连接数 |
maxIdleConnsPerHost | 单个 VU 单个域名最大活跃连接数 |
disableKeepAlives | 禁用长连接 |
headers | 公共请求头 |
timeout | 请求超时时间,单位毫秒 |
basicAuth | 基本认证 |
http2 | 是否开启 http2 |
discardResponseBody | 是否丢弃回包,当压测业务不关注回报,可开启此开关,提升压测性能 |
说明
配置 HTTP 请求的超时时间
import http from "pts/http";export const option = {http: {timeout: 3000,}}export default function() {http.get("http://mockhttpbin.pts.svc.cluster.local/get"); // Error: Get "http://mockhttpbin.pts.svc.cluster.local/get": net/http: request canceled while waiting for connection}
配置 HTTP 请求的基本认证
// Http basic authenticationimport http from 'pts/http';import { check } from 'pts';export const option = {http: {basicAuth: {username: 'user',password: 'passwd',}}}export default function () {const resp = http.get("http://mockhttpbin.pts.svc.cluster.local/basic-auth/user/passwd");console.log(resp.json().authenticated); // truecheck('body.authenticated equals true', () => resp.json().authenticated === true);}