【问题标题】:SOAPUI check request then mock responseSOAPUI 检查请求然后模拟响应
【发布时间】:2016-07-13 17:55:25
【问题描述】:

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

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

这里我有两个模拟请求。我正在查看的是 Request details 节点及其类型,即 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)

谢谢

克里斯

【问题讨论】:

  • Here 是 rest mock 的一个示例,即使您正在寻找肥皂请求,它也应该类似。
  • 谢谢。我已经设法用休息做到了这一点。我想知道你会如何使用肥皂?
  • 抱歉,没有收到您的问题。
  • 您建议的示例是使用 Rest。我正在查看soap xml 请求以检查要返回的响应
  • 好的,你想提供相同的请求和预期的输出吗?编辑问题并添加详细信息。

标签: c# xml groovy soapui


【解决方案1】:

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

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

然后在 MockOperation 编辑器中,选择 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 对象在模拟操作的脚本上下文中可用。另请注意,模拟操作的脚本中的返回必须返回与配置的响应名称匹配的字符串

希望这会有所帮助,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多