将Postman Pre-Request脚本转换为Jmeter beanshell预处理器脚本

时间:2019-06-11 04:14:14

标签: jmeter postman

大家好,我需要一些帮助将这个邮递员的预请求脚本转换为beanheell:

这是邮递员的我的请求前脚本

function encodeURL(str){
    return str.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '');
}

function decodeUrl(str){
    str = (str + '===').slice(0, str.length + (str.length % 4));
    return str.replace(/-/g, '+').replace(/_/g, '/');
}

//var key = "a30e84b7ccfa267570213cd3e1873eb23e3839811260102f5e159c35f47ff5f7";
var obj = JSON.parse(request.data);
var key = obj.transactionId;
var time = Math.round(new Date().getTime());
// var time = 1525518371264
var appId = request.headers["app-id"];
var authorization = request.headers["authorization"];
var method = request.method.toUpperCase();
var url = request.url;

var urlParam = method + " /wallet/purchase/qr";
var base64url = encodeURL(btoa(request.data));
var seed = appId+authorization+time+urlParam+base64url;
var hashedHmac = CryptoJS.enc.Hex.stringify(CryptoJS.HmacSHA256(seed, key));

console.log(request.data);
console.log(request);
console.log(time)
console.log(hashedHmac)
console.log("base64url=>" + base64url)
console.log("seed=>" + seed)
postman.setEnvironmentVariable("timeqr", time);
postman.setEnvironmentVariable("appid", appId);
postman.setEnvironmentVariable("hmacqr", hashedHmac);

我想将其转换为beanshell脚本,我已经做到了,但是似乎不起作用。有人可以帮助我吗?

这就是我所做的:

import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.codec.digest.DigestUtils;

String appId = "ovo-apps";
String time = String.valueOf(System.currentTimeMillis());

vars.put("time", time);
vars.put("app-id", appId);

String authorization = "${__property(user_token)}";
vars.put("authorization-qr", authorization );

String urlParam = sampler.getMethod() +  " "+ sampler.getUrl().getPath();
String bodyString = sampler.getArguments().getArgument(0).getValue();
String bodyBase64 = Base64.encodeBase64URLSafeString(bodyString.getBytes());
String baseMessage = appId + authorization + time + urlParam + bodyBase64;

String secret = "${trxId}";
String secretKey = DigestUtils.sha256Hex(secret);
String signature = HmacUtils.hmacSha256Hex(secretKey, baseMessage);

vars.put("signature", signature);

为此请帮我

0 个答案:

没有答案