文章目录
问题描述
工作中,使用postman,发现返回结果不是自己想要的,但是不确定参数是否传递准确,但是又无法像chrome浏览器F12的开发者工具这样的神器,于是在网上搜了相关信息,做个笔记,方便以后学习
地址栏传递参数
查看 request请求的面板
请求前函数 Pre-request Script
var appKey ="11111111111";//需要根据现场情况修改var appSecret ="22222222222222";//需要根据现场情况修改var md5 =calcMd5();var date =newDate().toString();var nonce =createUuid();var timestamp =newDate().getTime();var textToSign ="";
textToSign += request.method +"\n";
textToSign += request.headers["accept"]+"\n";
textToSign += request.headers["content-type"]+"\n";var headers =headersToSign();var signatureHeaders;var sortedKeys = Array.from(headers.keys()).sort();for(var headerName of sortedKeys){
textToSign += headerName +":"+ headers.get(headerName)+"\n";
signatureHeaders = signatureHeaders ? signatureHeaders +","+ headerName : headerName;}
textToSign +=urlToSign();
console.log("textToSign\n"+ textToSign.replace(/\n/g,"#"));var hash = CryptoJS.HmacSHA256(textToSign, appSecret);
console.log("hash:"+ hash);var signature = hash.toString(CryptoJS.enc.Base64);
console.log("signature:"+ signature)
pm.globals.set('AppKey', appKey);
pm.globals.set('Md5', md5);
pm.globals.set("Date", date);
pm.globals.set("Signature", signature);
pm.globals.set("SignatureHeaders", signatureHeaders);
pm.globals.set("Nonce", nonce);
pm.globals.set("Timestamp", timestamp);functionheadersToSign(){var headers =newMap();for(var name in request.headers){
name = name.toLowerCase();if(!name.startsWith('x-ca-')){continue;}if(name ==="x-ca-signature"|| name ==="x-ca-signature-headers"|| name =="x-ca-key"|| name ==='x-ca-nonce'){continue;}var value = request.headers[name];
headers.set(name, value);}
headers.set('x-ca-key', appKey);
headers.set('x-ca-timestamp', timestamp);
console.log("headers: "+ headers.size);for(var key in headers){
console.log("key:"+ key +"value:"+ headers[key]);}return headers;}functionurlToSign(){var params =newMap();var contentType = request.headers["content-type"];if(contentType && contentType.startsWith('application/x-www-form-urlencoded')){const formParams = request.data.split("&");
formParams.forEach((p)=>{const ss = p.split('=');
params.set(ss[0], ss[1]);})}const ss = request.url.split('?');if(ss.length >1&& ss[1]){const queryParams = ss[1].split('&');
queryParams.forEach((p)=>{const ss = p.split('=');
params.set(ss[0], ss[1]);})}var sortedKeys = Array.from(params.keys())
sortedKeys.sort();var l1 = ss[0].indexOf('/',10);var url = ss[0].substring(l1);var first =true;var qs
for(var k of sortedKeys){var s = k +"="+ params.get(k);
qs = qs ? qs +"&"+ s : s;
console.log("key="+ k +" value="+ params.get(k));}return qs ? url +"?"+ qs : url;}functioncalcMd5(){var contentType = request.headers["content-type"];if(request.data &&!contentType.startsWith('application/x-www-form-urlencoded')){var data = request.data;var md5 = CryptoJS.MD5(data);var md5String = md5.toString(CryptoJS.enc.Base64);
console.log("data:"+ data +"\nmd5:"+ md5String);return md5String;}else{return"";}}functioncreateUuid(){return'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(c){var r = Math.random()*16|0, v = c =='x'? r :(r&0x3|0x8);return v.toString(16);});}
Content-Type:application/json
X-Ca-Key:{{AppKey}}
X-Ca-Signature:{{Signature}}
X-Ca-Signature-Headers:{{SignatureHeaders}}
X-Requested-With:XMLHttpRequest
X-Ca-Timestamp:{{Timestamp}}
X-Ca-Nonce:{{Nonce}}
Accept:*/*
axios 等价实现 AK / SK 代码
- axios实例对象添加请求拦截器
// 请求拦截器
http.interceptors.request.use(function(config){getTokenInfo(config)// 在请求对象中添加 header信息returntrimOnlySpace(config)},function(error){// 对请求错误做些什么return Promise.reject(error)})
- 利用 AxiosRequestConfig 实例对象添加头信息
import CryptoJS from'crypto-js'exportfunctiongetTokenInfo(configObj){var appKey ='22918213';//需要根据现场情况修改var appSecret ='wEEt313l0KotykQaN17f';//需要根据现场情况修改functionresetHeadersInfo(key, value){
configObj.headers[key]= value
}functionheadersToSign(){var headers =newMap();for(var name in configObj.headers){
name = name.toLowerCase();if(!name.startsWith('x-ca-')){continue;}if(name ==='x-ca-signature'|| name ==='x-ca-signature-headers'|| name =='x-ca-key'|| name ==='x-ca-nonce'){continue;}var value = configObj.headers[name];
headers.set(name, value);}
headers.set('x-ca-key', appKey);
headers.set('x-ca-timestamp', timestamp);
console.log('headers: '+ headers.size);for(var key in headers){
console.log('key:'+ key +'value:'+ headers[key]);}return headers;}functionurlToSign(){var params =newMap();var contentType = configObj.headers['Content-Type']|| configObj.headers['content-type'];// var contentType = 'application/json';if(contentType && contentType.startsWith('application/x-www-form-urlencoded')){const formParams = configObj.data.split('&');
formParams.forEach((p)=>{const ss = p.split('=');
params.set(ss[0], ss[1]);})}const ss = configObj.url.split('?');if(ss.length >1&& ss[1]){const queryParams = ss[1].split('&');
queryParams.forEach((p)=>{const ss = p.split('=');
params.set(ss[0], ss[1]);})}var sortedKeys = Array.from(params.keys())
sortedKeys.sort();// var l1 = ss[0].indexOf('/', 10);// var url = ss[0].substring(l1);var url = ss[0]var first =true;var qs
for(var k of sortedKeys){var s = k +'='+ params.get(k);
qs = qs ? qs +'&'+ s : s;
console.log('key='+ k +' value='+ params.get(k));}return qs ? url +'?'+ qs : url;}functioncalcMd5(){var contentType = configObj.headers['Content-Type']|| configObj.headers['content-type'];
console.log(contentType)// var contentType = 'application/json'if(configObj.data &&!contentType.startsWith('application/x-www-form-urlencoded')){var data = configObj.data;var md5 = CryptoJS.MD5(data);var md5String = md5.toString(CryptoJS.enc.Base64);
console.log('data:'+ data +'\nmd5:'+ md5String);return md5String;}else{return'';}}functioncreateUuid(){return'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,function(c){var r = Math.random()*16|0, v = c =='x'? r :(r &0x3|0x8);return v.toString(16);});}var md5 =calcMd5();var date =newDate().toString();var nonce =createUuid();var timestamp =newDate().getTime();var textToSign ='';
textToSign += configObj.method.toUpperCase()+'\n';// textToSign += 'POST\n';
textToSign +=(configObj.headers['Accept']|| configObj.headers['accept'])+'\n';// textToSign += '*/*' + '\n';
textToSign +=(configObj.headers['Content-Type']|| configObj.headers['content-type'])+'\n';// textToSign += 'application/json' + '\n';var headers =headersToSign();var signatureHeaders;var sortedKeys = Array.from(headers.keys()).sort();for(var headerName of sortedKeys){
textToSign += headerName +':'+ headers.get(headerName)+'\n';
signatureHeaders = signatureHeaders ? signatureHeaders +','+ headerName : headerName;}
textToSign +=urlToSign();// textToSign += '/artemis/api/v1/resource/searchResourcesByAuth';
console.log('textToSign\n'+ textToSign);
console.log('textToSign\n'+ textToSign.replace(/\n/g,'#'));var hash = CryptoJS.HmacSHA256(textToSign, appSecret);
console.log('hash:'+ hash);var signature = hash.toString(CryptoJS.enc.Base64);resetHeadersInfo('domainId',0);// 多线路id ,解决内外网 IP 地址不一致的问题resetHeadersInfo('X-Ca-Key', appKey);resetHeadersInfo('X-Ca-Signature', signature);resetHeadersInfo('X-Ca-Signature-Headers', signatureHeaders);resetHeadersInfo('X-Ca-Nonce', nonce);resetHeadersInfo('X-Ca-Timestamp', timestamp);return configObj
}
响应后的函数 Test
// 1、Status code is 200 检查响应状态码是否为200
pm.test("Status code is 200",function(){
pm.response.to.have.status(200);});// 2、Body matches string 检查响应中是否包含指定字符串// 例①
pm.test("Body matches string",function(){
pm.expect(pm.response.text()).to.include(pm.variables.get("time"));// 是否包含变量time});// 例②
pm.test("Body matches string",function(){
pm.expect(pm.response.text()).to.include("tags");// 是否包含tags});// 3、json value check 检查响应中json 值
pm.test("Your test name",function(){var jsonData = pm.response.json();
pm.expect(jsonData.errmsg).to.eql("ok");// 检查json中errmsg是否等于"ok"});// 4、Is equal to a string 检查响应等于一个字符串
pm.test("Body is correct",function(){
pm.response.to.have.body('{"errcode":0,"errmsg":"ok"}');// 是否等于{"err......}});// 5、Response heads Content- Type 检查是否包含 Content- Type 这种响应头
pm.test("Content-Type is present",function(){
pm.response.to.have.header("Content-Type");});// 6、Response time is less than 200ms 检查请求耗时小于200ms
pm.test("Response time is less than 300ms",function(){
pm.expect(pm.response.responseTime).to.be.below(300);// 耗时是否小于300ms});
本文转载自: https://blog.csdn.net/hbiao68/article/details/140705192
版权归原作者 胖鹅68 所有, 如有侵权,请联系我们删除。
版权归原作者 胖鹅68 所有, 如有侵权,请联系我们删除。