Forum Discussion

jamescollett's avatar
7 years ago

Using scripting to choose a response based on the request

I have a mock service (SOAP XML) with a number of canned responses defined. The request that comes in includes an ID field. I should like to choose which response to send back to my client based on the value of the ID.


I appreciate that I need to choose "Dispatch: SCRIPT" in the Operation dialog panel but I am stumped after that. I have in the past successfully defined a sequence for sending the responses which was different from the default order in which they are listed, although that was a long time ago and that was as sophisticated as it got.


Can someone please help?



SoapUI (free edition) 5.2.1

2 Replies

  • JHunt's avatar
    Community Hero

    You can just use Xpath for dispatch. The result of the Xpath query is the name of the MockResponse that SoapUI will use. So if the MockResponse is exactly the same as the id you want to test for then, then just hit the declare button and then:





    A dispatch script works the same way, just have it return the name of the MockResponse you want to give:



    import groovy.util.XmlSlurper
    import groovy.util.slurpersupport.GPathResult
    GPathResult Envelope = new XmlSlurper().parseText(mockRequest.requestContent)
    return Envelope.Body.Id



    Which you can write as a one-liner if you want:



    new groovy.util.XmlSlurper().parseText(mockRequest.requestContent).Body.Id


    • jamescollett's avatar

      Thank you, JHunt, for your contribution, but I am afraid I do not understand a word of it. I do not know where to start.


      You are clearly a much more advanced user, starting from a level I have yet to reach.