JMETER使用BEANSHELL PREPROCESSOR编辑PATH,计算签名
- 在Pre Processor中获取请求的path
//获取path
String path = sampler.getPath();
log.info("===path===:"+path);
- 在Pre Processor中修改path
//获取path
String path = sampler.getPath();
log.info("===path===:"+path);
//设置path的值
sampler.setPath("/api/aaa/bbb");
//获取新的path
String newPath = sampler.getPath();
log.info("===newPath===:"+newPath);
- 在Pre Processor中获取请求参数
import org.apache.jmeter.config.Arguments;
//获取请求参数,key,value参数
Arguments args = sampler.getArguments();
Map params = args.getArgumentsAsMap();
log.info("===params===:"+params);
//获取请求参数,消息体数据获取
String body = arguments.getArgument(0).getValue();
- 在Pre Processor中添加参数
import org.apache.jmeter.config.Arguments;
import org.apache.jemeter.config.Argument;
//添加参数
sampler.addArgument("key1","value1");
sampler.addArgument("key2","value3");
//获取请求参数
Arguments args = sampler.getArguments();
Map params = args.getArgumentsAsMap();
log.info("===params===:"+params);
- 计算签名(网上找来的,具体逻辑根据具体业务)
import org.apache.jmeter.config.Arguments;
import org.apache.jemeter.config.Argument;
import org.apache.commons.codec.digest.DigestUtils;
sampler.addArgument("appKey","ling");
Date date = new Date();
String timestamp = String.valueOf(date.getTime());
sampler.addArgument("timestamp", timestamp); //添加时间戳
String nonce = UUID.randomUUID().toString().replaceAll("-", "");
sampler.addArgument("nonce", nonce); //添加随机字符串
//获取请求参数
Arguments args = sampler.getArguments();
Map params = args.getArgumentsAsMap();
log.info("*************params:"+params);
Map sortMap = new TreeMap(new Comparator() {
public int compare(String obj1, String obj2) {
return obj1.compareTo(obj2);//升序排序
}
});
sortMap.putAll(params);
Iterator sortByKeyEntries = sortMap.entrySet().iterator();
StringBuilder sb = new StringBuilder();
while (sortByKeyEntries.hasNext()) {
Map.Entry entry = sortByKeyEntries.next();
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("_");
}
log.info("=================sb.toString():"+sb.toString());
String url= sb.toString();
String url_encoded = URLEncoder.encode(url,"utf-8");
String sign = DigestUtils.md5Hex(url_encoded);
sampler.addArgument("sign", sign);