Forum Discussion

eric_the_tester's avatar
eric_the_tester
Occasional Contributor
3 years ago

Route request but return mocked response

I have a request that needs to be routed to the actual live api because the api invokes some backend logic needed to continue a workflow.

 

However, I would still like to return a mocked response. Is there a straight forward way to have the request still be routed but return a mocked response?

  • eric_the_tester's avatar
    eric_the_tester
    Occasional Contributor

    I found a solution and sharing here incase anyone has the same issue. I simply send a request to the actual service using groovy, and have the groovy logic for this executed before the Routing logic. You can use event handlers for this code snippet but I've placed it along the Routing script box for better visibility. 

     

    def get = new URL("Your 'Route to' host here" + mockRequest.path)
    conn = get.openConnection();
    conn.requestMethod = 'GET'
    def headers = mockRequest.getRequestHeaders()
    conn.setRequestProperty("session_token", headers.get('session_token')[0])
    conn.getResponseCode();

     

    Thanks all for the help.

  • richie's avatar
    richie
    Community Hero
    Hey eric_the_tester,

    Ive typed and deleted reaponse out 3 times, but im a bit confused!

    So you have an endpoint you want to hit which will give you a perfectly valid response, but instead of the expected, valid response, you want to stub the response to be something different?

    Can you just explain a bit more please? I must be missing something vital, cos im just thinking..."why?"


    Cheers!

    Rich
    • eric_the_tester's avatar
      eric_the_tester
      Occasional Contributor

      So you have an endpoint you want to hit which will give you a perfectly valid response, but instead of the expected, valid response, you want to stub the response to be something different?

       

      Correct!

      There is some backend logic that gets triggered and invoked whenever the endpoint gets called. I need this logic to be triggered in order to proceed in a workflow I am testing. I don't need the actual response that is returned from the actual service though. I'll be using a mocked response (which is different from what the actual service returns, the response itself is a bunch of configuration data the UI consumes that drives certain UI behavior, I'm testing a different set of configuration than what the actual service usually returns).

       

       

      • richie's avatar
        richie
        Community Hero
        Hey eric_the_tester,

        Ok...wow!

        Never actually tried stubbing out a whole response before!

        If we're keeping everything contained within ReadyAPI only i think the way to do this would be to use the Event Handlers (think pre-processors and post-processors in jmeter or postman, etc.).

        There is a replaceAll() method ive used before in events when ive wanted to change the content of the reaponse ive received before my assertions kick in (so post-processor), but ive never used it to replace a whole payload with a different one!

        Start having a look at the events handlers functionality and i'll start thinking about the groovy script youll need. Now im thinking about it, i think i might know what to do with the groovyscript, BUT! the important bit is getting the groovy script right....which ChrisAdams or one of other real coders will be able to do, better, quicker and more efficiently than i. What i knock out may work, but it wont be pretty!

        Cheers,

        Rich