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://httpbin.org/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://httpbin.org/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 (raw)
// 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', resp);}
HTTP Post (x-www-form-urlencoded)
// Send a form urlencoded 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/x-www-form-urlencoded',},});console.log(resp.json().headers['Content-Type']); // application/x-www-form-urlencodedconsole.log(resp.json().form.user_id); // 12345check('body.form.user_id equals 12345', () => resp.json().form.user_id === '12345', resp);}
HTTP Post (form-data)
// Send a multipart requestimport http from 'pts/http';import {check} from 'pts';const fileData = open("./sample/tmp.js")export default function () {const formData = new http.FormData();formData.append('data', 'some data');formData.append('file', http.file(fileData));const resp = http.post('http://httpbin.org/post', formData.body(), {headers: {'Content-Type': formData.contentType()}});console.log(resp.json().headers['Content-Type']); // multipart/form-data; boundary=c7fced8e5c0ce8939a96691da77d13f635c389cdbcc951e8784114edb41fconsole.log(resp.json().form.data); // some dataconsole.log(resp.json().files.file.length); // 801check('body.form.data equals some data', () => resp.json().form.data === 'some data');};
HTTP 基础鉴权
// Http basic authenticationimport http from 'pts/http';import {check} from 'pts';export default function () {const user = 'user';const passwd = 'passwd';const resp = http.get(`http://${user}:${passwd}@httpbin.org/basic-auth/user/passwd`);console.log(resp.json().authenticated); // truecheck('body.authenticated equals true', () => resp.json().authenticated === true);}
HTTP Cookie
// Send a request with cookieimport http from 'pts/http';import {check} from 'pts'export default function () {const resp = http.get('http://httpbin.org/cookies', {headers: {cookie: 'k=v'}});console.log(resp.json().cookies.k); // vcheck('body.cookies.k equals v', () => resp.json().cookies.k === 'v');};