soupUI解決md5加密簽名,cookie傳遞
問題詳情:
1、接口調用需要前提狀態:登錄狀態(cookie)
2、接口請求需要簽名,簽名規則為:MD5(TokenKey+apikey+timestamp+nonc)
其中
1、TokenKey、apikey為接口構造方提供(永久不變);
2、nonc為隨機數,自定義
3、timestamp 為 時間戳(百度百科)
對應解決辦法:
1、登錄獲取cookie;
- 登錄接口
- meiad type :application/x-www-form-urlencoded; charset=UTF-8
- 獲取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 cookiesListreturn cookiesList
"""
- 外部引用方法:${(Groovy Script 命名)#result} eg:${cookie_data#result}
2、獲取時間戳timestamp;
- test steps 中添加Groovy Script ,其內容如下:
"""
time = (new Date().time / 1000).intValue()
log.info time
return time"""
-
外部引用方法:${(Groovy Script 命名)#result} eg:${timestamp#result}
3、獲取簽名sign;
- 簽名關鍵點為MD5加密,先構建加密方式;test steps 中添加 Groovy Script ,其內容如下:
"""
import java.security.MessageDigest;
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"""
- 固定值TokenKey、apikey以及自定義值nonc的存放於參與簽名組成;
- test steps 中添加 Properties;添加對應key,value;
- Groovy Script 腳本引用方法:
- 外部引用方法${(Properties命名)#(key)} eg:${Properties#apiKey}
- test steps 中添加 Properties;添加對應key,value;
4、構建目標接口
soupUI解決md5加密簽名,cookie傳遞