文章目錄
- 1背景
- 1.1接口測試工具SoapUi產生背景
- 1.2常見接口類型
- 1.3接口包含內容
- 1.4請求格式
- 2軟件使用
- 3http、webservice、webapi如何測試
- 3.1REST(WebAPi、JSON/HTTP、POST)
- 3.2SOAP(Webserver、XML/HTTP、POST)
1背景
1.1接口測試工具SoapUi產生背景
當服務器被開發出來,但是客戶端(前端還未開發),此時需要使用接口測試工具進行接口測試。通過模擬客戶端發送請求,同時接收服務器回饋結果,完成接口測試。
1.2常見接口類型
1.3接口包含內容
1.4請求格式
- json
{"method": "POST","url": "https://api.example.com/login","headers": {"Content-Type": "application/json"},"body": {"username": "test_user","password": "p@ssw0rd123","remember_me": true}
}
2.XML
<?xml version="1.0" encoding="UTF-8"?>
<request><param name="id">1001</param>
</request>
2軟件使用
該軟件包含兩部分
通俗來說SOAP采用XML格式可以傳輸消息長,REST采用二進制編碼傳輸快。
3http、webservice、webapi如何測試
3.1REST(WebAPi、JSON/HTTP、POST)
1. 生成服務器
這邊有一個很好的博客,教如何創建REST的服務器,輸入和輸出均采用json格式。
2. 服務器限制報文格式
雙擊選中的Response1,點擊底部的Scrip按鈕,下拉會出現一個腳本文件。插入一下腳本內容。
// 獲取請求內容
def requestContent = mockRequest.getRequestContent()// 定義響應模板
def buildResponse(success, message) {return ["success": success,"errMsg": message,"Data": ["PalletNo": "123456","ProductCode": "HY-DH144N8"]]
}try {def requestJson = new groovy.json.JsonSlurper().parseText(requestContent)// 驗證邏輯...def errors = []if(!requestJson.EqpCode) errors << "缺少EqpCode"if(!(requestJson.FullQty in Integer)) errors << "FullQty必須為整數"if(!(requestJson.LotNumbers in List)) errors << "LotNumbers必須為數組"if(errors) {def response = buildResponse(false, "驗證失敗: " + errors.join(", "))mockResponse.setResponseContent(new groovy.json.JsonBuilder(response).toString())mockResponse.setResponseHttpStatus(400)} else {def response = buildResponse(true, "")mockResponse.setResponseContent(new groovy.json.JsonBuilder(response).toString())}} catch(e) {def response = buildResponse(false, "無效的JSON格式: " + e.message)mockResponse.setResponseContent(new groovy.json.JsonBuilder(response).toString())mockResponse.setResponseHttpStatus(400)
}
這樣就可以限制報文輸入類型為:
{
"EqpCode":"L1_BZ1",
"FullQty":36,
"LotNumbers":["H3501241015172300345","H3501241015172300346","H3501241015172300347"]
}
這個界面需要注意紅色框中為空白,一切用腳本來寫入即可。
3.3創建客戶端
參考這邊博文,很清楚說明了REST如何請求服務器
雙擊REST
寫入http://localhost:8080/phone/blackphone
即可得到測試界面
3.2SOAP(Webserver、XML/HTTP、POST)
可以參考這一篇文章。