• JMETER使用BEANSHELL PREPROCESSOR编辑PATH,计算签名


    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);  
    
  • 相关阅读:
    你知道Synchronized底层实现原理嘛
    一篇搞定Java集合类原理
    lsp都要会的内存模型
    Sql Server 查询优化
    使用Windows的mstsc远程桌面连接到Ubuntu图形界面(AWS上安装的Ubuntu系统)
    AWS EC2实例Ubuntu系统设置root用户密码并使用root/ubuntu用户登录
    安装mysql.zip文件教程(包含常见问题修复)
    DevExpress GridControl小结
    C#开发必会
    C# 错误集锦
  • 原文地址:https://www.cnblogs.com/tester-star/p/15348711.html
Copyright © 2020-2023  润新知