面试题-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交流群:730246532
# 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 按钮就可以查询到结果了


QQ交流群:730246532

posted @ 2021-02-20 10:54  上海-悠悠  阅读(3361)  评论(2编辑  收藏  举报