soupUI解决md5加密签名,cookie传递

问题详情:

1、接口调用需要前提状态:登录状态(cookie)

2、接口请求需要签名,签名规则为:MD5(TokenKey+apikey+timestamp+nonc)

其中

1、TokenKey、apikey为接口构造方提供(永久不变);

2、nonc为随机数,自定义

3、timestamp 为 时间戳(百度百科)

对应解决办法:

1、登录获取cookie;

  1. 登录接口
  2. meiad type :application/x-www-form-urlencoded; charset=UTF-8
  3. 获取response data (cookie);test steps 中添加 Groovy Script ,其内容如下:

    """

    import com.eviware.soapui.support.types.StringToStringMap

    def cookiesList = testRunner.testCase.getTestStepByName("Login - Request 1").testRequest.response.responseHeaders["Set-Cookie"][0]

    cookiesList = cookiesList[0..-29] +"IsClosePwdWeak=0"
    log.info cookiesList

    return cookiesList

    """

  4. 外部引用方法:${(Groovy Script 命名)#result}  eg:${cookie_data#result}

2、获取时间戳timestamp;

  1. test steps 中添加Groovy Script ,其内容如下:

    """

    time = (new Date().time / 1000).intValue()
    log.info time
    return time

    """

  2. 外部引用方法:${(Groovy Script 命名)#result}  eg:${timestamp#result}

3、获取签名sign;

  1. 签名关键点为MD5加密,先构建加密方式;test steps 中添加 Groovy Script ,其内容如下:

    """


    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException

    public static String md5Password(String password) {

    try {

    MessageDigest digest = MessageDigest.getInstance("md5");
    byte[] result = digest.digest(password.getBytes());
    StringBuffer buffer = new StringBuffer();

    for (byte b : result) {

    int number = b & 0xff;
    String str = Integer.toHexString(number);
    if (str.length() == 1) {
    buffer.append("0");
    }
    buffer.append(str);
    }


    return buffer.toString();
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    return "";
    }
    }

    def apiKey = context.expand( '${Properties#apiKey}' ) 
    def tokenKey = context.expand( '${Properties#TokenKey}' )
    def nonc = context.expand( '${Properties#nonc}' )
    def timestamp = context.expand( '${time#result}' )

    md5_data= md5Password(tokenKey+apiKey+timestamp+nonc) //按照开发人员提供的签名组成规则,组成签名
    log.info md5_data
    return md5_data

    """

  2. 固定值TokenKey、apikey以及自定义值nonc的存放于参与签名组成;
    1. test steps 中添加 Properties;添加对应key,value;

    2.  Groovy Script 脚本引用方法:
    3. 外部引用方法${(Properties命名)#(key)} eg:${Properties#apiKey}

4、构建目标接口

猜你喜欢

转载自www.cnblogs.com/testwjr/p/10156461.html
今日推荐