PTS 支持 HTTP 协议的 GET、POST、PUT、PATCH、OPTIONS、DELETE 和 HEAD 请求。
脚本编写
HTTP GET 请求
// Send a http get requestimport http from 'pts/http';import { check, sleep } from 'pts';export default function () {// simple get requestconst resp1 = http.get('http://mockhttpbin.pts.svc.cluster.local/get');console.log(resp1.body);// if resp1.body is a json string, resp1.json() transfer json format body to a json objectconsole.log(resp1.json());check('status is 200', () => resp1.statusCode === 200);// sleep 1 secondsleep(1);// get request with headers and parametersconst resp2 = http.get('http://mockhttpbin.pts.svc.cluster.local/get', {headers: {'Connection': 'keep-alive','User-Agent': 'pts-engine'},query: {'name1': 'value1','name2': 'value2',}});console.log(resp2.json().args.name1); // 'value1'check('body.args.name1 equals value1', () => resp2.json().args.name1 === 'value1');};
HTTP POST 请求
// Send a post requestimport http from 'pts/http';import { check } from 'pts';export default function () {const resp = http.post('http://mockhttpbin.pts.svc.cluster.local/post',{user_id: '12345',},{headers: {'Content-Type': 'application/json',},});console.log(resp.json().json.user_id); // 12345check('body.json.user_id equals 12345', () => resp.json().json.user_id === '12345');}
文件依赖
在压测场景里,您可上传以下几种类型的文件,提供压测执行时的状态数据:
参数文件:以 csv 文件的形式,动态提供测试数据。场景被每个并发用户(VU)执行时,会获取参数文件里的每行数据,作为测试数据的值,供脚本里的变量引用。具体使用方法请参见 使用参数文件。
请求文件:构建您的请求所需的文件,如需要上传的文件。具体使用方法请参见 使用请求文件。
协议文件:请求序列化所需要用到的文件。具体使用方法请参见 使用协议文件。