Forum Discussion

breaux's avatar
breaux
Contributor
6 years ago
Solved

Mock (REST) service, as a (mostly) passthrough

I'd like to create a Mock service that mostly acts as a pass-through to a real service that we use, but be able to quickly make one particular request fail in a particular way, so that I can address a particular case we've seen.

 

Anybody have advice or example script on the simplest way to simply pass an incoming request & response through a mock operation?

 

(Similar desire for a different use case, from this StackOverflow question, BTW: https://stackoverflow.com/questions/36041434/soapui-create-mock-service-with-pass-through-behavior-for-selected-methods )

  • This is possible with a fair bit of code involved, and I have done it before, but first I have to ask if it's really necessary to call the real service in realtime?

     

    Can't you just recreate your issue by calling the real service with manual requests in SoapUI, saving the responses, and adding them to a MockService?

6 Replies

  • JHunt's avatar
    JHunt
    Community Hero

    This is possible with a fair bit of code involved, and I have done it before, but first I have to ask if it's really necessary to call the real service in realtime?

     

    Can't you just recreate your issue by calling the real service with manual requests in SoapUI, saving the responses, and adding them to a MockService?

    • breaux's avatar
      breaux
      Contributor
      Yes, I probably could recreate the issue that way. I was just hoping there was some trivial way to pass requests through instead. That would address the other StackOverflow request as well.
  • gutterball's avatar
    gutterball
    Occasional Contributor

    Here is the code I have for calling a SOAP service within the mock script.  

    the host can be override in the header as in 

    <header>

       <host>142.234.345.67</host>
    </header>

    ```

    /**************************************/
    /* get the override host value */
    /**************************************/
    def String host = holder.getNodeValue("//host");

    //def host = context.expand('${#Project#host}');
    //log.info("HOST => " + host);

    /**************************************/
    /* if no host override, use localhost */
    /**************************************/
    if (host == null || host.length() == 0)
    host = "localhost";

    /**************************************/
    /* build the URL */
    /**************************************/
    service_url = "http://" + host + ":7001/my_downstream_service";


    /**************************************/
    /* setup the soap connection */
    /**************************************/
    def soapUrl = new URL(service_url);
    def connection = soapUrl.openConnection();
    def soapRequest = mockRequest.requestContent;

    connection.setRequestMethod("POST");
    connection.setRequestProperty("Accept-Encoding" ,"gzip,deflate");
    connection.setRequestProperty("Content-Type" ,"text/xml;charset=UTF-8");
    connection.setRequestProperty("SOAPAction", mockRequest.getSoapAction()); // mockRequest.getSoapAction());
    connection.doOutput = true;

    /**************************************/
    /* remove host override from reqeust */
    /**************************************/
    def String requestStr = mockRequest.requestContent
    def int pos1 = requestStr.indexOf("<host>");
    if (pos1 > 0){
    def String newReqeustStr
    def int pos2 = requestStr.indexOf("</host>");
    newReqeustStr = requestStr.substring(0, pos1) +
    requestStr.substring(pos2 + 8, requestStr.length());
    soapRequest = newReqeustStr;
    }

    /**************************************/
    /* call service */
    /**************************************/
    Writer writer = new OutputStreamWriter(connection.outputStream);
    writer.write(soapRequest);
    writer.flush();
    writer.close();
    connection.connect();
    def soapResponse = connection.content.text;
    requestContext.responseMessage = soapResponse;

    return "Response 1"

    ```

  • JHunt's avatar
    JHunt
    Community Hero

    Yeah, it's not trivial to do.

     

    Did you want to go down the route of writing a bunch of code? I could give you some starting points.

     

    Otherwise does that answer your question?

     

     

    • breaux's avatar
      breaux
      Contributor
      No thanks. For my particular case, writing a bunch of code isn't worthwhile. Thanks.