• 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);  
    
  • 相关阅读:
    Word如何去水印
    计算机二级公共基础知识 #02
    计算机二级公共基础知识 #01
    Linux常用快捷键
    计算机二级C语言概述 #00
    信管专业的同学都进来看一看叭~~~~~
    Python--Hanoi塔问题
    MATLAB——实验一:查看图像的RGB值
    Python课 #06号作业
    Python课 #05号作业
  • 原文地址:https://www.cnblogs.com/tester-star/p/15348711.html
Copyright © 2020-2023  润新知