身份驗證Authenticationjavascript
postman有一個helpers能夠幫助咱們簡化一些重複和複雜的任務。當前的一套helpers能夠幫助你解決一些authentication protocols的問題。前端
Basic Authjava
填寫用戶名和密碼,點擊Refresh headersjson
Digest Authapi
要比Basic Auth複雜的多。使用當前填寫的值生成authorization header。因此在生成header以前要確保設置的正確性。若是當前的header已經存在,postman會移除以前的header。數組
OAuth 1.0a安全
postman的OAuth helper讓你簽署支持OAuth 1.0基於身份驗證的請求。OAuth不用獲取access token,你須要去API提供者獲取的。OAuth 1.0能夠在header或者查詢參數中設置value。app
OAuth 2.0less
postman支持得到OAuth 2.0 token並添加到requests中。ide
3、Writting Test
Postman的Tests標籤能夠用來寫測試:
本質上是javascript code,能夠爲tests object設置values。這裏使用描述性文字做爲key,檢驗body中的各類狀況,固然你能夠建立任意多的key,這取決於你須要測試多少點。 tests也會隨着request保存到collection中。api測試保證前端後臺都能正常的於api協做工做,而不用在出錯時猜想是哪裏的問題。 須要在request的test中建立了test後,再進行request,test的結果在body的test中查看。 注意: 1.這裏的key描述必須是惟一的,不然相同描述只會執行第一個。 2.這裏的key能夠使用中文。 例子: tests[「Body contains user_id」] = responseBody.has(「user_id」)
這裏描述性的key爲:Body contains user_id。檢測點爲:responseBody.has(「user_id」),意思是檢測返回的body中是否包含」user_id」這個字段。
查看responses中的Tests結果:記過顯示每一個key,也就是咱們測試點的具體結果,是否經過。
Testing Sandbox
postman的測試是運行在沙箱環境,是與app獨立的。查看什麼在沙箱中是可用的,參見Sandbox documentation.
Snippets
用於快速添加經常使用的測試代碼。能夠自定義snippets。
Viewing results
postman每次執行request的時候,會執行tests。測試結果會在tests的tab上面顯示一個經過的數量。
Testing Sandbox
Testing examples
測試代碼會在發送request而且接收到responses後執行。
1.設置環境變量 postman.setEnvironmentVariable("key", "value");
2.設置全局變量 postman.setGlobalVariable("key", "value");
3.檢查response body中是否包含某個string tests["Body matches string"] = responseBody.has("string_you_want_to_search");
4.檢測JSON中的某個值是否等於預期的值
var data = JSON.parse(responseBody);tests["Your test name"] = data.value === 100;
JSON.parse()方法,把json字符串轉化爲對象。parse()會進行json格式的檢查是一個安全的函數。 如:檢查json中某個數組元素的個數(這裏檢測programs的長度)
var data = JSON.parse(responseBody);tests["program's lenght"] = data.programs.length === 5;
5.轉換XML body爲JSON對象 var jsonObject = xml2Json(responseBody);
6.檢查response body是否與某個string相等 tests["Body is correct"] = responseBody === "response_body_string";
7.測試response Headers中的某個元素是否存在(如:Content-Type)
tests["Content-Type is present"] = postman.getResponseHeader("Content-Type"); //getResponseHeader()方法會返回header的值,若是該值存在
或者:
tests["Content-Type is present"] = responseHeaders.hasOwnProperty("Content-Type");
上面的方法,不區分大小寫。下面的方法,要區分大小寫。
8.驗證Status code的值 tests["Status code is 200"] = responseCode.code === 200;
9.驗證Response time是否小於某個值 tests["Response time is less than 200ms"] = responseTime < 200;
10.name是否包含某個值 tests["Status code name has string"] = responseCode.name.has("Created");
11.POST 請求的狀態響應碼是不是某個值 tests["Successful POST request"] = responseCode.code === 201 || responseCode.code === 202;
12.很小的JSON數據驗證器
var schema = { "items": { "type": "boolean" } };var data1 = [true, false];var data2 = [true, 123];console.log(tv4.error); tests["Valid Data1"] = tv4.validate(data1, schema); tests["Valid Data2"] = tv4.validate(data2, schema);
結果:
4、運行Collections
postman容許你運行collection,你能夠運行任意的次數。 最後會給出一個總體運行的結果。會保存每一次運行的結果,提供給你比較每一次運行解僱的不一樣。
選擇collection,選擇環境。點擊運行按鈕。
json文件的地方記得添加。
運行collection測試會在另外一個窗口運行。若是須要在main窗口修改東西,在新窗口能正常讀取。