postman接口測試系列: 時間戳和加密


postman接口測試系列: 時間戳和加密

 

在使用postman進行接口測試的時候,對於有些接口字段需要時間戳加密,這個時候我們就遇到2個問題,其一是接口中的時間戳如何得到?其二就是對於現在常用的md5加密操作如何在postman中使用代碼實現呢?
下面我們以一個具體的接口例子來進行說明。
首先來看看我們的接口文檔信息,如圖所示
接口文檔
此接口文檔中,需要三個參數customercode、timestamp和itoken(是customerCode+timestamp+ytoken加密后的結果)。
第一次操作的時候,我們使用postman會這樣操作,如圖
postman
這樣操作流程是:

  1. 選擇提交方式是post,輸入接口的url地址
  2. 選擇接口情況的方式是x-www-form-urlencoded
  3. 設置接口的參數customerCode、timestamp和itoken和值
  4. 設置完成之后點擊send發送,查看接口響應結果

說明

x-www-form-urlencoded即是application/x-www-from-urlencoded,將表單內的數字轉換為鍵對值

postman中 form-data、x-www-form-urlencoded、raw、binary的區別:

http://blog.csdn.net/ye1992/article/details/49998511

時間戳轉換工具:

http://tool.chinaz.com/Tools/unixtime.aspx

md5加密工具:

https://md5jiami.51240.com/

這樣創建會話的接口我們就完成了!但是為了系統的安全性,這里的timestamp是每30分鍾就會過期的,下次我們又需要重新設置timestamp,就是md5加密的結果......這樣操作豈不是太麻煩?

還好postman中Pre-Request Script可以在 Request 之前自定義請求數據,這樣做的好處就是可以以嵌入腳本的方式動態准備測試數據,並根據業務需求設計測試用例。

這里我們仍繼續以上面的用例為例:

在postman中,如何才能獲取當前機器上的timestamp呢?

Math.round(new Date().getTime()) 

可以滿足我們的要求!!!
那代碼如何實現呢?

//設置當前時間戳毫秒 postman.setGlobalVariable("timestamp",Math.round(new Date().getTime()));

這樣就將獲取的時間戳設置為全局變量timestamp
我們知道itoken的值是md5(customerCode+timestamp+ytoken')
那么接下來就可以動態的獲取md5的信息了,代碼如下:

//發起請求之前獲取當前的時間戳放在參數里 //postman.setGlobalVariable("customerCode","***2345677***"); //1.設置環境變量 postman.setEnvironmentVariable("key", "value"); //2.設置全局變量 postman.setGlobalVariable("key", "value"); //environment.customerCode = "***2345677***"; customerCode = postman.getGlobalVariable("customerCode"); //設置當前時間戳毫秒 postman.setGlobalVariable("timestamp",Math.round(new Date().getTime())); //environment.timestamp = Math.round(new Date().getTime()); //postman.setEnvironmentVariable("unixtime_now","timecode"); //var jsonData = JSON.parse(request.data.applyJsonStr); //postman.setGlobalVariable("ytoken","*********b176a4739bfccb*********"); //獲取全局變量 //如postman.getGlobalVariable("key"); customerCode = postman.getGlobalVariable("customerCode"); timestamp = postman.getGlobalVariable('timestamp'); ytoken = postman.getGlobalVariable("ytoken"); var str = customerCode+timestamp+ytoken; //postman.setEnvironmentVariable("str",str); //environment.str = str; postman.setGlobalVariable("str",str); //var md5 = CryptoJS.MD5(str).toString().toLowerCase(); //使用md5加密 //var strmd5 = CryptoJS.MD5(str).toString(); var strmd5 = CryptoJS.MD5(str); //environment.strmd5 = strmd5; postman.setGlobalVariable('md5',strmd5); //environment.md5 = md5; //timecode=System.currentTimeMillis(); console.log(str);

而在接口請求中,就可以使用已經定義好的變量來進行接口操作,代碼如下

customerCode:{{customerCode}} timestamp:{{timestamp}} ltoken:{{md5}}

如圖所示

這樣下次創建接口的時候,直接運行該用例即可,不用再次修改參數值~(≧▽≦)/~
那么我們如何才能知道該接口用例是成功的呢,該怎么斷言呢?
這里列出我該接口斷言的一個示例,代碼如下

/*
// 推薦用全等 ===,確保類型和值都一致
tests['Status code is 200'] = responseCode.code === 200; // 判斷是否存在 'success' 值 tests["Body matches code"] = responseBody.has("0"); var jsonData = JSON.parse(responseBody); postman.setEnvironmentVariable("sessionId",jsonData.result); tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true; tests["have result "]=jsonData.hasOwnProperty("error")!==true; tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000; **/ //狀態代碼是200 if(responseCode.code === 200){ // 判斷是否存在 'success' 值,檢查響應體包含一個字符串 tests["Body matches code"] = responseBody.has("0"); //響應結果中result保存為全局變量sessonId var jsonData = JSON.parse(responseBody); postman.setGlobalVariable("sessionId",jsonData.result); //輸入接口參數信息 tests[`[INFO] Request params: ${JSON.stringify(request.data)}`] = true; // tests["have result "]=jsonData.hasOwnProperty("error")!==true; //判斷接口響應結果有result tests["have result "]=jsonData.hasOwnProperty("result")===true; //判斷接口響應時間小於N秒 tests[`[INFO] Response timeout: ${responseTime}`] = responseTime < 6000; }else{ //接口請求失敗 tests["Waring:Request Failed. Please Fix!"] = false; }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM