接到開發接口的任務,寫完后不知對錯,就想到找一個接口測試工具測一測。接口測試工具有很多,比如Swagger、SoapUI、Jmeter、Postman等,本文着重講述Postman的安裝與使用。
1.Postman的安裝
Postman的官方下載地址為https://www.getpostman.com/apps,在這里,你可以根據自己的需求直接下載相應的exe文件,達到一鍵安裝的效果。
2.Postman測試WebService接口
(1)設置url
一般就是訪問項目中的asmx文件。
(2)設置請求模式:post
(3)設置Header
為防止出現中文亂碼,數據編碼格式為UTF-8。所以添加Content-Type,值為text/xml;charset=utf-8。
(4)設置Body
勾選raw, 因為WebService通過HTTP協議發送請求和接收結果時,發送的請求內容和結果內容都采用XML格式封裝,並增加了一些特定的HTTP消息頭,以說明 HTTP消息的內容格式,這些特定的HTTP消息頭和XML內容格式就是SOAP協議。這邊需要指定傳輸數據的類型,選擇XML(text/xml)格式。
接口定義的請求格式復制到Body中,並且填上必要的參數,例子如下:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <UniteBudgetManage xmlns="http://tempuri.org/"> <budgetManageCriteria> <AccountCode>123456</AccountCode> <BudgetDeptCode>SMCV-MAN10</BudgetDeptCode> <Year>2018</Year> <BlockAmount>8989</BlockAmount> </budgetManageCriteria> </UniteBudgetManage> </soap:Body> </soap:Envelope>
其中UnitBudgetManage是方法名,budgetManageCriteria為該方法中的參數,它是一個對象,包含BudgetType、CheckorBlock等屬性。
一切設置完成后,點擊 send 按鈕就可以了。
3.Postman測試WCF接口
(1)設置url
一般訪問的是對應的svc文件,加上相應的方法名。
其中UnitBudgetManage是對應的UrlTemplate。
(2)設置請求模式:post
(3)設置Header:添加Content-Type,值為text/xml;charset=utf-8
(4)設置Body
勾選raw,以JSON(application/json)格式進行傳輸。Body中輸入需要傳輸的JSON格式的數據。例子如下,
{ "Service": { "Data": { "Request": { "BudgetType": "Account", "CheckorBlock": "Check", "BmEntity": [ { "AccountCode": "123", "BlockAmount": "1900", "BudgetDeptCode": "SMCV-MAN", "Year": "2018" }, { "AccountCode": "456", "BlockAmount": "4500", "BudgetDeptCode": "SMCV-GR", "Year": "2018" } ] } } } }
一切設置完畢后,點擊 Send 按鈕,這樣就會調用指定的接口,並執行內部的方法,根據返回結果來測試功能的正確與否。
(5)返回結果
返回結果依然是JSON格式。
{ "Service": { "Data": { "Request": { "BmEntity": [ { "AccountCode": "123", "BlockAmount": "1900", "BudgetDeptCode": "SMCV-MAN", "Year": "2018" }, { "AccountCode": "456", "BlockAmount": "4500", "BudgetDeptCode": "SMCV-GR", "Year": "2018" } ], "BudgetType": "Account", "CheckorBlock": "Check" }, "Response": { "Code": "E0000000", "Desc": "科目預算金額不足:123-2018-SMCV-MAN預算金額不足", "Status": "FAIL" } } } } }