一、PTS 支持的配置选项
| HTTP 请求配置选项 | 描述 |
| maxRedirects | 重定向跳转次数 |
| maxIdleConns | 单个 VU 最大活跃连接数 |
| maxIdleConnsPerHost | 单个 VU 单个域名最大活跃连接数 |
| disableKeepAlives | 禁用长连接 |
| headers | 公共请求头 |
| timeout | 请求超时时间,单位毫秒 |
| basicAuth | 基本认证 |
| http2 | 是否开启 http2 |
| discardResponseBody | 是否丢弃回包,当压测业务不关注回报,可开启此开关,提升压测性能 |
注意:说明:
注意:完整选项列表请参见 HTTP global Options。
二、配置 HTTP 请求的超时时间
import http from "pts/http";export const option = { http: { timeout: 3000, }}export default function() { http.get("http://httpbin.org/get"); // Error: Get "http://httpbin.org/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://httpbin.org/basic-auth/user/passwd`); console.log(resp.json().authenticated); // true check('body.authenticated equals true', () => resp.json().authenticated === true);}