SOAPUI检查请求然后模拟响应

时间:2016-07-11 11:57:44

标签: c# xml groovy soapui

我已经设置了一个模拟SOAPUI项目,在调用端点时返回模拟响应。我要做的是根据请求的内容返回不同的模拟结果。在我点击端点URL时,它始终返回相同的响应。

是否可以使用groovy脚本查看请求的内容并根据其内容发送模拟响应?请求是SOAP所以我要检查XML

这里我有两个模拟请求。我正在看的是请求详细信息节点及其类型,即TelephoneNumber或PostCode,然后基于此发送响应。是否可以检查节点类型,即TelephoneNumber,PostCode并根据它发送响应?

邮政编码请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lin="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
   <soapenv:Header/>
   <soapenv:Body>
      <lin:GetLineCharacteristics>
         <lin:request>
            <!--Optional:-->
            <lin:UserCredentials>
               <!--Optional:-->
               <lin:Username>Test</lin:Username>
               <!--Optional:-->
               <lin:Password>Test</lin:Password>
               <lin:AgentID>321</lin:AgentID>
            </lin:UserCredentials>
            <lin:RequestDetails xsi:type="lin:PostcodeRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <lin:Postcode>L00 0DD</lin:Postcode>
            </lin:RequestDetails>
            <lin:UserConsent>Yes</lin:UserConsent>
            <lin:ServiceType>SMPF</lin:ServiceType>
         </lin:request>
      </lin:GetLineCharacteristics>
   </soapenv:Body>
</soapenv:Envelope>

邮政编码回复

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetLineCharacteristicsResponse xmlns="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
         <GetLineCharacteristicsResult>
            <Status>
               <HasErrors>false</HasErrors>
               <Errors/>
            </Status>
            <ResponseDetails>
               <CharacteristicsBase xsi:type="LineCharacteristicsForTelephoneNumber">
                  <ExchangeDetails>
                     <CSSExchangeDistrictCode>LV</CSSExchangeDistrictCode>
                     <ExchangeCode>LVSIM</ExchangeCode>
                     <ExchangeName>Simonswood</ExchangeName>
                     <ExchangeState>Live</ExchangeState>
                     <ExchangeCapacity>G</ExchangeCapacity>
                     <ForecastDate>0001-01-01T00:00:00</ForecastDate>
                     <CircuitLineLength>
                        <MeasureType>CircuitEstimated</MeasureType>
                        <Length>3549</Length>
                     </CircuitLineLength>
                  </ExchangeDetails>
                  <TechnologyMessages>
                     <TechnologyMessage>
                        <Code>LLUSMPF</Code>
                        <Message>LLU Installation</Message>
                     </TechnologyMessage>
                  </TechnologyMessages>
                  <EstimatedSpeedInformation>
                     <EstimatedSpeedInformation>
                        <EstimatedSpeedInformationType>Standard</EstimatedSpeedInformationType>
                        <EstimatedAnnexADownstreamSpeed>3647</EstimatedAnnexADownstreamSpeed>
                        <EstimatedAnnexAMaxRange>5519</EstimatedAnnexAMaxRange>
                        <EstimatedAnnexAMinRange>2287</EstimatedAnnexAMinRange>
                        <MinThresholdAnnexA>1657</MinThresholdAnnexA>
                        <EstimatedAnnexMDownstreamSpeed>2647</EstimatedAnnexMDownstreamSpeed>
                        <EstimatedAnnexMUpstreamSpeed>1000</EstimatedAnnexMUpstreamSpeed>
                     </EstimatedSpeedInformation>
                  </EstimatedSpeedInformation>
                  <TelephoneNumber>01515481460</TelephoneNumber>
                  <AccessLineID>LVAAACJ8C2</AccessLineID>
                  <AccessLineStatus>BTLive</AccessLineStatus>
                  <NumberRangeHolder>BT</NumberRangeHolder>
                  <NumberPortingInformation>NotSet</NumberPortingInformation>
                  <WorkingLineDetails>
                     <ProductType>PSTN Single Line</ProductType>
                     <PendingCeaseOrderIndicator>N</PendingCeaseOrderIndicator>
                  </WorkingLineDetails>
               </CharacteristicsBase>
            </ResponseDetails>
         </GetLineCharacteristicsResult>
      </GetLineCharacteristicsResponse>
   </soap:Body>
</soap:Envelope>

电话请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lin="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
   <soapenv:Header/>
   <soapenv:Body>
      <lin:GetLineCharacteristics>
         <lin:request>
            <!--Optional:-->
            <lin:UserCredentials>
               <!--Optional:-->
               <lin:Username>"Test</lin:Username>
               <!--Optional:-->
               <lin:Password>Test</lin:Password>
               <lin:AgentID>111</lin:AgentID>
            </lin:UserCredentials>
            <lin:RequestDetails xsi:type="lin:TelephoneNumberRequest" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <lin:TelephoneNumber>11111111111</lin:TelephoneNumber>
            </lin:RequestDetails>
            <lin:UserConsent>Yes</lin:UserConsent>
            <lin:ServiceType>SMPF</lin:ServiceType>
         </lin:request>
      </lin:GetLineCharacteristics>
   </soapenv:Body>
</soapenv:Envelope>

电话回复

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetLineCharacteristicsResponse xmlns="http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS">
         <GetLineCharacteristicsResult>
            <Status>
               <HasErrors>false</HasErrors>
               <Errors/>
            </Status>
            <ResponseDetails>
               <CharacteristicsBase xsi:type="LineCharacteristicsForTelephoneNumber">
                  <ExchangeDetails>
                     <CSSExchangeDistrictCode>LV</CSSExchangeDistrictCode>
                     <ExchangeCode>LVSIM</ExchangeCode>
                     <ExchangeName>Simonswood</ExchangeName>
                     <ExchangeState>Live</ExchangeState>
                     <ExchangeCapacity>G</ExchangeCapacity>
                     <ForecastDate>0001-01-01T00:00:00</ForecastDate>
                     <CircuitLineLength>
                        <MeasureType>CircuitEstimated</MeasureType>
                        <Length>3549</Length>
                     </CircuitLineLength>
                  </ExchangeDetails>
                  <TechnologyMessages>
                     <TechnologyMessage>
                        <Code>LLUSMPF</Code>
                        <Message>LLU Installation</Message>
                     </TechnologyMessage>
                  </TechnologyMessages>
                  <EstimatedSpeedInformation>
                     <EstimatedSpeedInformation>
                        <EstimatedSpeedInformationType>Standard</EstimatedSpeedInformationType>
                        <EstimatedAnnexADownstreamSpeed>3647</EstimatedAnnexADownstreamSpeed>
                        <EstimatedAnnexAMaxRange>5519</EstimatedAnnexAMaxRange>
                        <EstimatedAnnexAMinRange>2287</EstimatedAnnexAMinRange>
                        <MinThresholdAnnexA>1657</MinThresholdAnnexA>
                        <EstimatedAnnexMDownstreamSpeed>2647</EstimatedAnnexMDownstreamSpeed>
                        <EstimatedAnnexMUpstreamSpeed>1000</EstimatedAnnexMUpstreamSpeed>
                     </EstimatedSpeedInformation>
                  </EstimatedSpeedInformation>
                  <TelephoneNumber>11111111111</TelephoneNumber>
                  <AccessLineID>LVAAACJ8C2</AccessLineID>
                  <AccessLineStatus>BTLive</AccessLineStatus>
                  <NumberRangeHolder>BT</NumberRangeHolder>
                  <NumberPortingInformation>NotSet</NumberPortingInformation>
                  <WorkingLineDetails>
                     <ProductType>PSTN Single Line</ProductType>
                     <PendingCeaseOrderIndicator>N</PendingCeaseOrderIndicator>
                  </WorkingLineDetails>
               </CharacteristicsBase>
            </ResponseDetails>
         </GetLineCharacteristicsResult>
      </GetLineCharacteristicsResponse>
   </soap:Body>
</soap:Envelope>

由于我正在尝试模拟的Web服务,我无法使用标头。

这是我正在编写的Groovy脚本的一个版本

import com.eviware.soapui.support.GroovyUtils
import groovy.xml.XmlUtil

def groovyUtils = new GroovyUtils(context)
def xmlParser = new XmlParser()
def responseContent

def requestXmlHolder = groovyUtils.getXmlHolder(mockRequest.getRequestContent())
requestXmlHolder.declareNamespace("lin", "http://llu.webservices.opalonline.co.uk/LineCharacteristicsWS")

log.info(requestXmlHolder)

def postCode = requestXmlHolder.getNodeValue("//PostCode")

log.info(postCode)

感谢

克里斯

1 个答案:

答案 0 :(得分:1)

是的,可以根据 SOAPUI 模拟服务中的请求内容使用不同的响应。

首先在您的 mockService 中生成可能的响应。右键单击 MockOperation 并选择 New MockResponse ,为您的两个响应做出操作(在您的情况下为邮政编码和电话回复)。

enter image description here

然后在 MockOperation 编辑器中,选择 Dispatch SCRIPT。

enter image description here

使用 Groovy 脚本来评估服务将根据请求内容发送的响应,为此您可以使用以下 Groovy 脚本:

// parse the content of the SOAP request
def slurper = new XmlSlurper().parseText(mockRequest.getRequestContent())
// find for example if <TelephoneNumber> node exists
def phone = slurper.'**'.find { it.name() == 'TelephoneNumber' }
if(phone?.size() > 0){
    // if there is at least one <TelephoneNumber> return telephoneResponse
    return 'TelephoneResponse'
}

// if no <TelephoneNumber> is found, return postCode
return 'PostCodeResponse'

请注意,在模拟操作的脚本上下文中可以使用mockRequest对象。另请注意,模拟操作脚本中的返回必须返回与已配置响应的名称匹配的字符串

希望这有帮助,