使用postman 測試restful接口


   前提:

  在chrome網上應用商店安裝postman 和 Postman Interceptor.

   如下圖:

   

       

      使用postman的時候,最后開啟postman Interceptor,如下圖:

      

      然后啟動,postman工具,開始使用。

 

其中{{url}}的使用,是因為在設置了環境變量,如下圖:

 

 使用{{address}}是因為設置了全局變量,如下圖:

 

Testing 實例

來看一些Postman用於test的例子。這些例子中的大多數在Postman中是有效的,他們像一行JavaScript語句一樣簡答。在你的request中你可以有很多的test。

注意:test腳本在從服務器收到response后執行

1.設置環境變量:

postman.setEnvironmentVariable("key", "value");

2.設置全局變量:

 postman.setGlobalVariable("key", "value");

3.檢查response的body中是否包含字符串:

tests["Body matches string"] = responseBody.has("string_you_want_to_search");

4.把XML的body轉換成JSON對象:

var jsonObject = xml2Json(responseBody);

5.檢查response的body是都為一個字符串:

tests["Body is correct"] = responseBody === "response_body_string";

6.檢查JSON的值:

var data = JSON.parse(responseBody); tests["Your test name"] = data.value === 100;

7.內容類型存在(檢查不區分大小寫)

tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //Note: the getResponseHeader() method returns the header value, if it exists.

8.內容類型存在(區分大小寫):

tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");

9.response的響應時間小於200ms:

tests["Response time is less than 200ms"] = responseTime < 200;

10.狀態碼為200:

tests["Status code is 200"] = responseCode.code === 200;

11.Code name contains a string:

tests["Status code name has string"] = responseCode.name.has("Created");

12.成功的POST request狀態碼:

tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;

 

 

 


免責聲明!

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



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