本文介绍了SoapUI MockService:作为代理工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 SoapUI 中写 MockService



$如果节点存在于接收到的 SOAP 请求中(由 XPath )我想通过 groovy 脚本生成响应。在其他情况下,我想重定向收到的 SOAP - 请求。我的意思是,我希望将收到的 SOAP -Request发送给其他 SOAP -Service(我希望通过<$ c发送

有没有办法做到这一点?

解决方案

如果你想使用 soapUI MockService 作为代理,请执行以下操作: / p>



  def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

//载入JKS(需要https)
def keyStorePath =keystore.jks
def keyStorePassword =keystore_pswd
def trustStorePath =truststore.jks
def trustStorePassword =truststore_pswd

def keyStoreFactory = javax.net.ssl.KeyManagerFactory。进入(SUNX509)
def trustStoreFactory = javax.net.ssl.TrustManagerFactory.getInstance(SUNX509)
def keyStore = java.security.KeyStore.getInstance(JKS)
def trustStore = java.security.KeyStore.getInstance(JKS)
$ b $ def keyInput = new FileInputStream(keyStorePath)
keyStore.load(keyInput,keyStorePassword.toCharArray())
keyInput.close()
$ b def def trustInput = new FileInputStream(trustStorePath)
trustStore.load(trustInput,trustStorePassword.toCharArray())
trustInput.close()

keyStoreFactory.init(keyStore,keyStorePassword.toCharArray())
trustStoreFactory.init(trustStore)
$ b $ def sslContext = javax.net.ssl.SSLContext.getInstance(TLS )
sslContext.init(keyStoreFactory.getKeyManagers(),trustStoreFactory.getTrustManagers(),new java.security.SecureRandom())
$ b def sslFactory = sslContext.getSocketFactory()

//将收到的请求发送到https://example.org/soap/service
def soapRequest = mockRe quest.requestContent
def soapUrl = new URL(https://example.org/soap/service)
def connection = soapUrl.openConnection()

connection.setRequestMethod (POST)
connection.setRequestProperty(Content-Type,text / html)
connection.setRequestProperty(SOAPAction,)
connection.setSSLSocketFactory(sslFactory)
connection.doOutput = true

Writer writer = new OutputStreamWriter(connection.outputStream);
writer.write(soapRequest)
writer.flush()
writer.close()
connection.connect()
$ b $ def soapResponse =连接。 content.text
def responseXPathHelper = groovyUtils.getXmlHolder(soapResponse)

responseXPathHelper.declareNamespace(envelop,http://www.w3.org/2003/05/soap-envelope )
responseXPathHelper.declareNamespace(msg,https://example.org/soap/service)

//在这里你可以修改条件(你想欺骗的地方)
if(ExampleResponse.equals(responseXPathHelper [/ envelop:Envelope / envelop:Body / * / local-name()]))//欺骗
{
def requestXPathHelper = groovyUtils.getXmlHolder(mockRequest.requestContent)
requestXPathHelper.declareNamespace(envelop,http://www.w3.org/2003/05/soap-envelope)

def messageValue =spoofed response:+ requestXPathHelper [/ envelope:Envelope / envelope:Body / msg:ExampleResponse / text()]

def responseTemplate =< S:Envelope xmlns:S = HTTP: //www.w3.org/2003/05/soap-envelope\">
< S:Body>
< ExampleResponse xmlns =https://example.org/soap/service>
$ {messageValue}
< / ExampleResponse>
< / S:Body>
< / S:Envelope>

requestContext.responseMessage = responseTemplate
}
//作为代理工作,即只返回从示例中收到的响应。 org / soap / service
else requestContext.responseMessage = soapResponse


I'm writing MockService in SoapUI.

In the case if node exist in a received SOAP Request (by XPath) I want generate response by groovy script. In other cases I want redirect a received SOAP-Request. I mean, I want send a received SOAP-Request to other SOAP-Service (I want send by URL).

Is there way to do this?

解决方案

If you want use your soapUI MockService as proxy, do this :

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

// load JKS (need for https)
def keyStorePath = "keystore.jks"
def keyStorePassword = "keystore_pswd"
def trustStorePath = "truststore.jks"
def trustStorePassword = "truststore_pswd"

def keyStoreFactory = javax.net.ssl.KeyManagerFactory.getInstance("SUNX509")
def trustStoreFactory = javax.net.ssl.TrustManagerFactory.getInstance("SUNX509")
def keyStore = java.security.KeyStore.getInstance("JKS")
def trustStore = java.security.KeyStore.getInstance("JKS")

def keyInput = new FileInputStream(keyStorePath)
keyStore.load(keyInput, keyStorePassword.toCharArray())
keyInput.close()

def trustInput = new FileInputStream(trustStorePath)
trustStore.load(trustInput, trustStorePassword.toCharArray())
trustInput.close()

keyStoreFactory.init(keyStore, keyStorePassword.toCharArray())
trustStoreFactory.init(trustStore)

def sslContext = javax.net.ssl.SSLContext.getInstance("TLS")
sslContext.init(keyStoreFactory.getKeyManagers(), trustStoreFactory.getTrustManagers(), new java.security.SecureRandom())

def sslFactory = sslContext.getSocketFactory()

// Send received request to https://example.org/soap/service
def soapRequest = mockRequest.requestContent
def soapUrl = new URL("https://example.org/soap/service")
def connection = soapUrl.openConnection()

connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type" ,"text/html")
connection.setRequestProperty("SOAPAction", "")
connection.setSSLSocketFactory(sslFactory)
connection.doOutput = true

Writer writer = new OutputStreamWriter(connection.outputStream);
writer.write(soapRequest)
writer.flush()
writer.close()
connection.connect()

def soapResponse = connection.content.text
def responseXPathHelper = groovyUtils.getXmlHolder(soapResponse)

responseXPathHelper.declareNamespace("envelop", "http://www.w3.org/2003/05/soap-envelope")
responseXPathHelper.declareNamespace("msg", "https://example.org/soap/service")

// Here you can modify condition (Where you want to spoofing)
if ("ExampleResponse".equals(responseXPathHelper["/envelop:Envelope/envelop:Body/*/local-name()"]))    // spoofing
{
    def requestXPathHelper = groovyUtils.getXmlHolder(mockRequest.requestContent)
    requestXPathHelper.declareNamespace("envelop", "http://www.w3.org/2003/05/soap-envelope")

    def messageValue = "spoofed response : " + requestXPathHelper[/envelop:Envelope/envelop:Body/msg:ExampleResponse/text()]

    def responseTemplate = """<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
        <S:Body>
            <ExampleResponse xmlns="https://example.org/soap/service">
                ${messageValue}
            </ExampleResponse>
        </S:Body>
    </S:Envelope>"""

    requestContext.responseMessage = responseTemplate
}
// work as proxy, i.e. just return response recieved from example.org/soap/service
else     requestContext.responseMessage = soapResponse

这篇关于SoapUI MockService:作为代理工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:50