[英]SOAPUI check request then mock response
我已經建立了一個模擬SOAPUI項目,該項目在調用端點時會返回模擬響應。 我想做的是根據請求的內容返回不同的模擬結果。 當我點擊端點URL時,它一直在返回相同的響應。
常規腳本是否可以查看請求的內容並根據其內容發送模擬響應? 請求是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)
謝謝
克里斯
是的,可以根據SOAPUI模擬服務中的請求內容使用不同的響應。
首先在您的嘲諷服務中生成可能的響應。 右鍵單擊您的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
對象在模擬操作的腳本上下文中可用。 還要注意,模擬操作中的return in腳本必須返回與配置的響應名稱匹配的字符串 。
希望這可以幫助,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.