JLLMNCHR
JLLMNCHR

Reputation: 1571

Using SOAPUI as a kind of gateway

I want to ask you if it is possible in SOAPUI to alter a mock response, and link it to the real web service operation in which the MockService is based on.

I need to do that because within SOAPUI I can reach to external webservices; BUT for security/configuration reasons, I can’t access to this external webservices inside my local code in my Eclipse (I have tried several proxy configurations in my Eclipse without success).

What I want to do is to pass the request that reach to the mock service to the original web service and return the response without manipulation.

Upvotes: 1

Views: 618

Answers (2)

JLLMNCHR
JLLMNCHR

Reputation: 1571

I finally did it turning 'Dispatch' to SCRIPT and adding this 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())

Upvotes: 0

albciff
albciff

Reputation: 18517

You can create a mockService in SOAPUI to redirect your request to a 3rd party service as follows:

First creat a mockService in your project: right click on your project > New SOAP MockService

enter image description here

Then creat a mockOperation on it: right click on your MockService > New MockOperation

enter image description here

Inside your mockOperation there is a request created, open it an put for example the follow code as a response: ${myResponse}. This name is bind to a variable which will fill then with a script.

enter image description here

Finally open your mockOperation and use the follow script to hit your 3rd party service redirecting the original request:

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

enter image description here

Hope this helps,

Upvotes: 1

Related Questions