Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
10 years ago
Solved

How to get soap mock response from external file!

I have soap request with ordernumber tag. I will be saving mock response with file name same as ordernumber value in request. How to get mock response of specific ordernumber request? There will be multiple users placing response file in same path with name of ordernumber value from request. They need to get their corresponding response only after hiting same mock URL. I am using free version of soapui .Thanks in advance.

18 Replies

    • Apoorva6's avatar
      Apoorva6
      Frequent Contributor
      Rao, Thank you. However I am poor in groovy. Could you please help me with the script? If we get ordernumber as 1234 then we save the mock response in C:\users\order folder as 1234.txt
      • rupert_anderson's avatar
        rupert_anderson
        Valued Contributor

        Hi,

         

        Sorry to hop in, but assuming you're happy to extract the file name from the mockRequest (using Groovy), then does the following give you what you need:

         

        1. In your mock action, create a new Mock MockResponse with dispatch type 'SEQUENCE' e.g. called 'default'

         

        2. In the MockResponse (default) add a script e.g.

         

        def fileName = "1234.txt" //Extract this value from the request (ask if you need help)
        
        def responseFileContent = new StringBuilder()
        File testDataFile = new File("/Users/test/$fileName") //Load file containing test data response
        
        testDataFile.eachLine {content, lineNumber ->
                  responseFileContent.append(content) //Add each line of test data file to a String
        }
        
        mockResponse.responseContent=responseFileContent //Populate response body
        mockResponse.responseHttpStatus=200 //set status code (can also be done in UI)
        
        def headers = mockResponse.responseHeaders
        headers["Content-Type"]=["text/xml"]
        mockResponse.responseHeaders=headers //Headers can also be set in the UI

        Here is a screen shot:

         

        Screen Shot 2015-12-18 at 17.07.25.png

         

        This should load and despatch the contents of the file /Users/test/1234.txt as the body of the mock response. All that should then be needed is for you to add some script before my script to set fileName to the value you want to extract from the mockRequest object (let me know if you need any help with this). Setting the responseHttpStatus and responseHeaders is optional, as it can be set in the Mock Response UI - I added it as it can be useful in the case where you want to conditionally simulate error / 404 / 500 statuses or respond with different content types!  

         

        Hope this helps,

        Rupert