JMeter测试Web服务

Web服务简介

什么是Web服务

【Web服务】即Web Service。Web服务是一种服务导向架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作。根据W3C的定义,Web服务(Web service)应当是一个软件系统,用以支持网络间不同机器的互动操作。网络服务通常是许多应用程序接口(API)所组成的,它们透过网络,例如国际互联网(Internet)的远程服务器端,执行客户所提交服务的请求。

Web服务三要素

 

SOAP

* SOAP构建模块

* SOAP消息组成

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

    <soap:Header>
      ...
    </soap:Header>

    <soap:Body>
      ...
      <soap:Fault>
         ...
      </soap:Fault>
      ...
    </soap:Body>

</soap:Envelope>

* SOAP消息例子

股票查询服务,根据股票名称查询股票价格,返回股票价格。
1.SOAP消息请求:
POST /StockQuote HTTP/1.1
Host: example.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn
SOAPAction: "http://example.com/GetLastTradePrice"

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Body>
    <m:TradePriceRequest xmlns:m="http://example.com/stockquote.xsd">
      <tickerSymbol>MSFT</tickerSymbol >
    </m:TradePriceRequest>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

2.SOAP消息响应:
HTTP/1.1 200 OK
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  <SOAP-ENV:Body>
    <m:TradePriceResult xmlns:m=" http://example.com/stockquote.xsd ">
      <price>74.5</price>
    </m:TradePriceResult >
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

WSDL

* WSDL文档结构

WSDL元素结构示意图如下图所示:

 

其中,
1.Types是一个数据类型定义的容器,包含了所有在消息定义中需要的XML元素的类型定义。
2.Message具体定义了在通信中使用的消息的数据结构,Message元素包含了一组Part元素,
  每个Part元素都是最终消息的一个组成部分,每个Part都会引用一个DataType来表示它的结构。
  Part元素不支持嵌套。
3.PortType具体定义了一种服务访问入口的类型,何谓访问入口的类型呢?
  就是传入/传出消息的模式及其格式。一个PortType可以包含若干个Operation,
  而一个Operation则是指访问入口支持的一种类型的调用。
以上三种结构描述了调用Web服务的抽象定义,这三部分与具体Web服务部署细节无关,是可复用的描述(每个层次都可以复用)。
4.Service描述的是一个具体的被部署的Web服务所提供的所有访问入口的部署细节,
  一个Service往往会包含多个服务访问入口,而每个访问入口都会使用一个Port元素来描述。
5.Port描述的是一个服务访问入口的部署细节,包括通过哪个Web地址(URL)来访问,应当使用怎样的消息调用模式来访问等。
  其中消息调用模式则是使用Binding结构来表示。
6.Binding结构定义了某个PortType与某一种具体的网络传输协议或消息传输协议相绑定,
  从这一层次开始,描述的内容就与具体服务的部署相关了。比如可以将PortType与SOAP/HTTP绑定,
  也可以将PortType与MIME/SMTP相绑定等。

* WSDL端口

<portType>元素是最重要的WSDL元素。它可描述一个Web service可被执行的操作以及相关的消息。可以把<portType>元素比作传统编程语言中的一个函数库(或一个模块,或一个类)。端口包含如下类型:

1.一个One-way操作的例子:
<message name="newTermValues">
  <part name="term" type="xs:string"/>
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="setTerm">
    <input name="newTerm" message="newTermValues"/>
  </operation>
</portType >

在这个例子中,端口"glossaryTerms"定义了一个名为"setTerm"的one-way操作。
这个"setTerm"操作可接受新术语表项目消息的输入,这些消息使用一条名为"newTermValues"的消息,
此消息带有输入参数"term"和"value"。不过,没有为这个操作定义任何输出。

2.一个Request-response操作的例子:
<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

在这个例子中,端口“glossaryTerms”定义了一个名为“getTerm”的request-response操作。
“getTerm”操作会请求一个名为“getTermRequest”的输入消息,
此消息带有一个名为“term”的参数,并将返回一个名为 “getTermResponse”的输出消息,
此消息带有一个名为“value”的参数。

* WSDL绑定

一个绑定的例子:
<message name="getTermRequest">
  <part name="term" type="xs:string"/>
</message>

<message name="getTermResponse">
  <part name="value" type="xs:string"/>
</message>

<portType name="glossaryTerms">
  <operation name="getTerm">
    <input message="getTermRequest"/>
    <output message="getTermResponse"/>
  </operation>
</portType>

<binding type="glossaryTerms" name="b1">
   <soap:binding style="document"
   transport="http://schemas.xmlsoap.org/soap/http" />
   <operation>
     <soap:operation soapAction="http://example.com/getTerm"/>
     <input><soap:body use="literal"/></input>
     <output><soap:body use="literal"/></output>
  </operation>
</binding>

binding元素有“name”和“type”两个属性。“name”属性定义binding的名称,
而“type”属性指向binding的端口,在这个例子中是“glossaryTerms”端口。
soap:binding元素有“style”和“transport”两个属性。
“style”属性可取值为“rpc”或“document”。在这个例子中我们使用“document”。
“transport”属性定义SOAP使用的协议,在这个例子中使用HTTP。
operation元素定义了每个端口提供的操作符。对于每个操作,相应的SOAP行为都需要被定义。
同时必须知道如何对输入和输出进行编码。在这个例子中使用了“literal”。

应用案例

案例说明

这里以天气预报服务为例。

400个国内外主要城市天气预报Web服务访问地址:
Endpoint:http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx
Disco:http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx?disco
WSDL:http://ws.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

操作:getSupportCity
功能:查询本天气预报Web Services支持的国内外城市或地区信息
请求方式:HTTP/POST
接口地址:http://ws.webxml.com.cn/WebServices/WebServices/WeatherWebService.asmx
输入参数:byProvinceName = 指定的洲或国内的省份,若为ALL或空则表示返回全部城市
返回数据:一个一维字符串数组String(),结构为:城市名称(城市代码)

操作步骤
1.添加线程组;
2.添加HTTP请求取样器并配置;
3.在取样器节点下添加“HTTP Header Manager”并配置;
4.在取样器节点下添加查看结果树;
5.执行看结果。

* 使用SOAP1.1时配置测试

HTTP请求配置:
1.Server Name or IP:ws.webxml.com.cn
2.Method:POST
3.Path:/WebServices/WebServices/WeatherWebService.asmx
4.Content encoding:utf-8
5.Body Data:
<?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>
    <getSupportCity xmlns="http://WebXml.com.cn/">
      <byProvinceName>湖南</byProvinceName>
    </getSupportCity>
  </soap:Body>
</soap:Envelope>
其中"湖南"为传入的参数值(byProvinceName为参数名)

 

POST请求传递数据为SOAP消息,格式为XML。需要将SOAP消息放入Body Data中发送给服务器,
并且需要告诉服务器对应的Content-Type。
故需要添加一个“HTTP Header Manager”配置元件,在其中添加两个首部“Content-Type”与“SOAPAction”,
其中“SOAPAction”用来标识SOAP HTTP请求的目的地,其值是个URI地址。
在SOAP1.1中这个首部若其值为空串(""),表示SOAP消息的目的地由HTTP请求的URI标识;
无值则表示没有指定这条消息的目的地。

1.Content-Type: text/xml; charset=utf-8
2.SOAPAction: "http://WebXml.com.cn/getSupportCity"
配置如下图所示:

* 使用SOAP1.2时配置测试

HTTP请求配置只需要将Body Data修改,其他配置项保持不变:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getSupportCity xmlns="http://WebXml.com.cn/">
      <byProvinceName>string</byProvinceName>
    </getSupportCity>
  </soap12:Body>
</soap12:Envelope>

需要修改"HTTP Header Manager"配置元件,在SOAP1.2规范中,
SOAPAction首部被Content-Type的“action”属性所取代,但其作用和工作原理都没有变化。

Content-Type: application/soap+xml;charset=UTF-8;action="http://WebXml。com。cn/getSupportCity"
配置如下图所示:

最后:下面是配套学习资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!【100%无套路免费领取】

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

  全套资料获取方式:点击下方小卡片自行领取即可

猜你喜欢

转载自blog.csdn.net/weixin_57794111/article/details/133309780