SOAPUI 检查请求然后模拟响应

SOAPUI check request then mock response

我有一个模拟 SOAPUI 项目 set-up,当我调用端点时,它 return 模拟响应。我想做的是 return 根据请求的内容不同的模拟结果。在我到达端点 URL 的那一刻,它一直在 returning 相同的响应。

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

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

Post代码请求

<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>

Post代码响应

<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 服务,我无法使用 headers 执行此操作。

这是我正在编写的 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)

谢谢

克里斯

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

首先在您的 mockService 中生成可能的响应。右键单击您的 MockOperation 和 select New MockResponse,为您的两个响应执行操作(在您的情况下 Post代码和电话回复)。

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

并使用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 对象在模拟操作的脚本上下文中可用。另请注意,模拟操作脚本中的 return 必须 return 一个与配置的响应名称匹配的字符串 .

希望这对您有所帮助,