Popular Posts

Monday, November 25, 2013

Simulating RESTful services with soapUI

I have discussed about the mock services which simulate SOAP back-ends in chapter 6 of Web Services Testing with soapUI book. For the past few years, service orientation has been shifting towards RESTful web services from SOAP which demands the necessity of mocking various types of RESTful services. Being the one-stop tool kit of SOA developers/testers, soapUI provides users with different options to simulate RESTful services. In this post, I will discuss one of such a simple mechanisms to create RESTful mock services in soapUI.

Pre-requisites:

soapUI-4.5.2 or later

Simulating POX (Plain-Old-XML) with soapUI

Suppose you have a web service which returns XML response. Usually, the HTTP Content-Type of such a message is application/xml or text/xml.
  1. Create a new MOCK Service in soapUI. You can either create a Mock service from a new WSDL (ouch! we are dealing with RESTful services. Why do we need WSDL?? Does not matter. We just want to have a mock service regardless of where it originated from. We will tweak the response to be RESTy. Trust me!) or add a new mock service by right-clicking on an interface of an existing SOAP based service.
  2. Either way, you will have a mock service similar to the following.


    Note that, our mock service will be exposed at http://localhost:8088/restMockService
  3. We are going to simulate a response with text/xml content. Create an XML file in your file system. i.e:-

    <root>
    <child>value</child>
    <root>
    

    Save the file as xmlresponse.xml.
  4. Once a HTTP request (regardless of the content of incoming message) hits the mock service, it will return XML response. We can execute a groovy script at this time and respond back with XML output. This can be achieved by having a OnRequest Script in soapUI mock service interface. Click on OnRequest Script tab at the bottom pane of mock service editor.
  5. In the OnRequest Script editor, add the following script.

    //To output text/xml
    mockRunner.returnFile(mockRequest.httpResponse, new File("/home/charitha/Desktop/xmlresponse.xml"))
    return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
    
  6. Start the mock service by clicking on green arrow icon at the top left corner of mock service editor. 
  7. Send any HTTP request to http://localhost:8088/restMockService. You will observe the following output.

    HTTP/1.1 200 OK
    
    Content-Length: 35
    
    Content-Type: text/xml
    
    Connection: close
    
    Server: Jetty(6.1.26)
    
    
    
    <root>
    <child>value</child>
    <root>
    
    

    Simulating JSON output with soapUI

    This is quite similar to the above procedure but a few tweaks are required to format the JSON response to be a valid JSON with application/json content-type.

    1. In the same mock service which we created above (or a different one), we will add a new OnRequest script. First, create a file in your local file system with the following content.

      {
        "getQuote": {
          "request": { "symbol": "WSO2" }
        }
      }
      

      Save the file as jsonresponse.json.
    2. Now, add the following script as OnRequest Script
      import javax.servlet.http.HttpServletResponse
      import com.eviware.soapui.support.Tools
      
      
      def response = mockRequest.httpResponse
      File file = new File("/home/charitha/Desktop/jsonresponse.json")
      FileInputStream fin = new FileInputStream(file)
      response.setStatus( HttpServletResponse.SC_OK )
      long length = file.length();
      response.setContentLength( ( int )length );
      response.setContentType("application/json;charset=utf-8" );
      Tools.readAndWrite( fin, length, response.getOutputStream() );
      fin.close();
      
      return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
      

      You will question why we cannot just return the output of jsonresponse.json straight-away as we did with text/xml content. As explained by Ole Lensmar (The creator of soapUI) in http://forum.loadui.org/viewtopic.php?f=2&t=15020&p=36012 , this is a hack to respond back with application/json content-type. If you just return the file, the content-type will be delivered as application/javascript.
    3. Now, restart the mock service and send a HTTP request to above mock service endpoint. You will get a response similar to the following.


      HTTP/1.1 200 OK
      
      Content-Length: 60
      
      Content-Type: application/json;charset=utf-8
      
      Connection: close
      
      Server: Jetty(6.1.26)
      
      
      
      {
        "getQuote": {
          "request": { "symbol": "WSO2" }
        }
      }
      
      

      Similarly, you can simulate what ever RESTful back-end you want with soapUI. For example, if you want to return text/html, create a simple HTML in your file system and add the following script.

      //To output text/html
      mockRunner.returnFile(mockRequest.httpResponse, new File("/home/charitha/Desktop/htmlresponse.html"))
      return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
      

      To return text/plain response, create a text file in file system and add the following script.


      //to output text/plain
      mockRunner.returnFile(mockRequest.httpResponse, new File("/home/charitha/Desktop/plaintextresponse.txt"))
      return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)