面試題-web service接口如何測試?


前言

接口相關的測試,http 協議的接口大家平常基礎的很多,基本上問題不大。webservice 接口如何測試呢?需先了解什么是 webservice 接口,和 http 協議的接口有什么不一樣?

http 協議和soap 協議

我們平常說的接口,其實就是跟服務器進行數據交互,把數據提交到服務端,或者查詢服務端的數據,那么如何數據交互,需遵循接口協議,根據協議的不同這里分http協議和soap協議(還有其它協議就不一一列舉)

http 協議: HTTP協議是Hyper Text Transfer Protocol(超文本傳輸協議)的縮寫,HTTP是一個屬於應用層的面向對象的協議,
是用於從萬維網(WWW:World Wide Web )服務器傳輸超文本到本地瀏覽器的傳送協議。

soap 協議:(Simple Object Access Protocol)簡單對象存取協議。是XML Web Service 的通信協議。
當用戶通過UDDI找到你的WSDL描述文檔后,他通過可以SOAP調用你建立的Web服務中的一個或多個操作。
SOAP是XML文檔形式的調用方法的規范,它可以支持不同的底層接口,像HTTP(S)或者SMTP。

什么是web service?

通俗來講就是 soap 協議開發的接口對應的服務就是web service接口,通過 SOAP 在 Web上 提供的軟件服務,使用 WSDL 文件進行說明,並通過 UDDI 進行注冊。有以下幾個特點

  • 基於Web的服務:服務器端整出一些資源讓客戶端應用訪問(獲取數據)
  • 一個跨語言、跨平台的規范(抽象)
  • 多個跨平台、跨語言的應用間通信整合的方案(實際)

http 協議開發的接口對應的服務,我們叫http service

SOAP 協議是什么?

Webservice是基於 SOAP 協議傳輸數據。 SOAP 又是一種簡單的基於 XML 的協議,它使應用程序通過 HTTP 來交換信息。

什么是 XML ?
SOAP 是基於XML 來描述的,必須先了解什么是xml格式,如下格式,就是xml

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
<?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>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>上海</theCityName>
    </getWeatherbyCityName>
  </soap:Body>
</soap:Envelope>

什么是 WSDL 文件?

WSDL(Web Services Description Language)基於XML語言,用於描述Web Service及其函數、參數和返回值。
它是WebService客戶端和服務器端能理解的標准格式。
因為是基於 XML 的,所以 WSDL 既是機器可閱讀的,又是人可閱的,這將是一個很大的好處。——可以視為接口文檔

WSDL 文件保存在 Web 服務器上,通過一個 url 地址就可以訪問到它。客戶端要調用一個 WebService 服務之前,要知道該服務的 WSDL 文件的地址。

wsdl地址可以訪問這個地址查看示例 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

UDDI 是什么

UDDI (Universal Description, Discovery, and Integration) 是一個主要針對Web服務供應商和使用者的新項目。在用戶能夠調用Web服務之前,必須確定這個服務內包含哪些商務方法,找到被調用的接口定義,還要在服務端來編制軟件,UDDI是一種根據描述文檔來引導系統查找相應服務的機制。UDDI利用SOAP消息機制(標准的XML/HTTP)來發布,編輯,瀏覽以及查找注冊信息。它采用XML格式來封裝各種不同類型的數據,並且發送到注冊中心或者由注冊中心來返回需要的數據。

webservice 接口測試

先根據webservice 提供的接口文檔 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
這是一個 WeatherWebService 的接口文檔,可以查詢天氣

以 getWeatherbyCityName 根據城市或地區名稱查詢獲得未來三天內天氣情況、現在的天氣實況、天氣和生活指數為例

在 theCityName 后面輸入:上海,點調用

就可以查看返回的結果了

使用 postman 測試 webservice 接口

根據上面提供的接口請求報文內容,以下是 SOAP 1.2 請求和響應示例。所顯示的占位符需替換為實際值。

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"

<?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>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>string</theCityName>
    </getWeatherbyCityName>
  </soap:Body>
</soap:Envelope>

響應示例

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <getWeatherbyCityNameResponse xmlns="http://WebXml.com.cn/">
      <getWeatherbyCityNameResult>
        <string>string</string>
        <string>string</string>
      </getWeatherbyCityNameResult>
    </getWeatherbyCityNameResponse>
  </soap:Body>
</soap:Envelope>

於是可以在postman上填寫相關的請求參數
請求url地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
請求頭部參數:Content-Type: text/xml; charset=utf-8

請求body傳raw,類型選 XML(text/xml),body里面輸入

# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
<?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>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>string</theCityName>
    </getWeatherbyCityName>
  </soap:Body>
</soap:Envelope>

點 send 按鈕就可以查詢到結果了


免責聲明!

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



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