yapi测试接口通过脚本动态设置请求头参数

2021-10-29  本文已影响0人  爱的旋转体

yapi地址:https://hellosean1025.github.io/yapi/documents/index.html

image.png
var appId = '112345555'
var secret = '24536457647467'

console.log('appId', appId);
console.log('secret', secret);


var timestamp = new Date().getTime()
console.log('timestamp', timestamp);
var randomNum = parseInt((Math.random()*9+1)*100000)
console.log('randomNum', randomNum);
var method = context.method
console.log('method', method);

var content = ''
var param = {}
var keyArray = []

param['api_time']=timestamp
param['api_random']=randomNum
keyArray.push('api_time')
keyArray.push('api_random')

if("GET" == method){
    var queryParam = context.query
    console.log('queryParam', queryParam);
    for(let key in queryParam){
        console.log(key, queryParam[key]);
        param[key]=queryParam[key]
        keyArray.push(key)
    }
}else if("POST" == method){
    var requestBody = context.requestBody
    console.log('requestBody', requestBody);
    for(let key in requestBody){
        console.log(key, requestBody[key]);
        param[key]=requestBody[key]
        keyArray.push(key)
    }
}

console.log('before sort keyArray', keyArray);
keyArray.sort();
console.log('after sort keyArray', keyArray);
keyArray.forEach(x => {
    content += x;
    content += '=';
    content += param[x];
    content += '&';
    
})
content += 'secret=';
content += secret;
console.log('content', content);

var md5 = context.utils.md5(content)
console.log('md5', md5);

var Signature = appId + '_' + md5 + '_' + timestamp + '_' + randomNum
console.log('Signature', Signature);

context.requestHeader.Signature = Signature;
上一篇下一篇

猜你喜欢

热点阅读