使用 SOAPUI 作为一种网关
Using SOAPUI as a kind of gateway
我想问你是否可以在 SOAPUI 中更改模拟响应,并link将其更改为 MockService 所基于的真实 Web 服务操作。
我需要这样做,因为在 SOAPUI 中我可以访问外部网络服务;但是由于 security/configuration 原因,我无法在我的 Eclipse 中的本地代码中访问此外部 Web 服务(我在我的 Eclipse 中尝试了几种代理配置但没有成功)。
我想要做的是将到达模拟服务的请求传递给原始 Web 服务,并且 return 不进行任何操作的响应。
您可以在 SOAPUI 中创建一个 mockService 以将您的请求重定向到第三方服务,如下所示:
首先在您的项目中创建一个 mockService:右键单击您的项目 > New SOAP MockService
然后在上面创建一个 mockOperation:右键单击你的 MockService > New MockOperation
在您的 mockOperation 中创建了一个 request,打开它并放置例如以下代码作为响应:${myResponse}
.此名称绑定到一个变量,该变量随后将用脚本填充。
最后打开您的 mockOperation 并使用以下脚本访问您的第 3 方服务并重定向原始请求:
final HttpURLConnection connection = 'http://yourService:8080'.toURL().openConnection()
connection.setDoOutput(true)
// copy the headers
mockRequest.getRequestHeaders().each { name, value ->
connection.setRequestProperty(name,value.toString())
}
// write the request
connection.outputStream.withWriter { Writer writer ->
writer << mockRequest.requestContent
}
// get the response
String response = connection.inputStream.withReader { Reader reader -> reader.text }
// set the response in your variable
requestContext.myResponse = response
希望这对您有所帮助,
我终于做到了,将 'Dispatch' 转为 SCRIPT 并添加此脚本:
// import all the namespaces to trim the lines of codes
import com.eviware.soapui.impl.wsdl.WsdlProject
import com.eviware.soapui.impl.wsdl.WsdlInterface
import com.eviware.soapui.impl.wsdl.WsdlRequest
import com.eviware.soapui.impl.wsdl.WsdlSubmitContext
import com.eviware.soapui.impl.wsdl.WsdlSubmit
import com.eviware.soapui.model.iface.Response
import com.eviware.soapui.model.mock.MockResponse
// get reference to project
WsdlProject project = (WsdlProject)mockOperation.mockService.project
// get reference to request
WsdlRequest request = (WsdlRequest)project.interfaces["TheRealWebService"].operations["TheRealOperation"].getRequestByName("TheRealRequest")
// set request content from incoming mockRequest
request.setRequestContent(mockRequest.getRequestContent())
// submit request asynchronously
WsdlSubmit submit=request.submit( new WsdlSubmitContext( request ), false )
// wait for the response
Response response = submit.getResponse();
// get reference to MockResponse
MockResponse mockResponse=mockOperation.getMockResponseByName("Response1")
// set the mock response content from response received by the request.
mockResponse.setResponseContent(response.getContentAsString())
我想问你是否可以在 SOAPUI 中更改模拟响应,并link将其更改为 MockService 所基于的真实 Web 服务操作。
我需要这样做,因为在 SOAPUI 中我可以访问外部网络服务;但是由于 security/configuration 原因,我无法在我的 Eclipse 中的本地代码中访问此外部 Web 服务(我在我的 Eclipse 中尝试了几种代理配置但没有成功)。
我想要做的是将到达模拟服务的请求传递给原始 Web 服务,并且 return 不进行任何操作的响应。
您可以在 SOAPUI 中创建一个 mockService 以将您的请求重定向到第三方服务,如下所示:
首先在您的项目中创建一个 mockService:右键单击您的项目 > New SOAP MockService
然后在上面创建一个 mockOperation:右键单击你的 MockService > New MockOperation
在您的 mockOperation 中创建了一个 request,打开它并放置例如以下代码作为响应:${myResponse}
.此名称绑定到一个变量,该变量随后将用脚本填充。
最后打开您的 mockOperation 并使用以下脚本访问您的第 3 方服务并重定向原始请求:
final HttpURLConnection connection = 'http://yourService:8080'.toURL().openConnection()
connection.setDoOutput(true)
// copy the headers
mockRequest.getRequestHeaders().each { name, value ->
connection.setRequestProperty(name,value.toString())
}
// write the request
connection.outputStream.withWriter { Writer writer ->
writer << mockRequest.requestContent
}
// get the response
String response = connection.inputStream.withReader { Reader reader -> reader.text }
// set the response in your variable
requestContext.myResponse = response
希望这对您有所帮助,
我终于做到了,将 'Dispatch' 转为 SCRIPT 并添加此脚本:
// import all the namespaces to trim the lines of codes
import com.eviware.soapui.impl.wsdl.WsdlProject
import com.eviware.soapui.impl.wsdl.WsdlInterface
import com.eviware.soapui.impl.wsdl.WsdlRequest
import com.eviware.soapui.impl.wsdl.WsdlSubmitContext
import com.eviware.soapui.impl.wsdl.WsdlSubmit
import com.eviware.soapui.model.iface.Response
import com.eviware.soapui.model.mock.MockResponse
// get reference to project
WsdlProject project = (WsdlProject)mockOperation.mockService.project
// get reference to request
WsdlRequest request = (WsdlRequest)project.interfaces["TheRealWebService"].operations["TheRealOperation"].getRequestByName("TheRealRequest")
// set request content from incoming mockRequest
request.setRequestContent(mockRequest.getRequestContent())
// submit request asynchronously
WsdlSubmit submit=request.submit( new WsdlSubmitContext( request ), false )
// wait for the response
Response response = submit.getResponse();
// get reference to MockResponse
MockResponse mockResponse=mockOperation.getMockResponseByName("Response1")
// set the mock response content from response received by the request.
mockResponse.setResponseContent(response.getContentAsString())