Service Virtualization

Expand all | Collapse all

How to get different Response from same REST URL

  • 1.  How to get different Response from same REST URL

    Posted Sep 13, 2017 09:42 AM

    Assuming, I have a banking app, where there are 3 brands - AAA, BBB & ZZZ. Below REST URL - retrieves list of branches for three brands.

    OPERATION: GET

     

    URL1: https://api.aaa.com/v1/branches

    URL2: https://api.bbb.com/v1/branches

    URL3: https://api.zzz.com/v1/branches

     

    Where api.aaa.com -> will definitely be some hostname & port number.

     

    Issue: I want to have single SV project deployed in Single port -> where if user consumes above 3 URL gets different response for respective brands.I have 3 response specific to incoming request. But since there is no change in header request and no additional Request parameters being sent, Is there a way - we can retrieve different response for three different URL's hitting same port. Is there a way LISA can identify it?



  • 2.  Re: How to get different Response from same REST URL

    Broadcom Employee
    Posted Sep 14, 2017 01:40 AM

    Hi,

        If you create a single project, single port and deployed on same server, there are no differences in incoming request. So, can you tell us on what criteria you would like to send different responses?.

     

    Thanks & Regards

    Srikanth Gajawada



  • 3.  Re: How to get different Response from same REST URL

    Posted Sep 14, 2017 09:47 AM

    Hi Pranay,

     

    If I understood your question correctly,

    Your Base path is  /v1/branches  which is same for all the 3 servers and you want to maintain a single responder for all three servers but the response sent is different.

     

    1) After the Listen step , add a do nothing step with a filter to read property from a file.

    2) create a normal text file containing its corresponding server name and place it across same location on all three servers

    3) Maintain 3 image files with respective rr pairs configured 

    4) Now assert on the property value and go to respective image response selection step

    5) Use a common respond step

     

    Let me know if you have questions on it. Hope it helps!

     

    Thanks,

    Venkatesh Satturi

     



  • 4.  Re: How to get different Response from same REST URL

    Posted Sep 14, 2017 02:23 PM

    Hi VenkateshSatturi I would try to follow steps mentioned by you and will get back to you.



  • 5.  Re: How to get different Response from same REST URL

    Broadcom Employee
    Posted Sep 14, 2017 10:12 AM

    If there is something in the incoming request which can identify your different requests (base path or header info or something else), then you can do assert on that and use the approach of multiple VSI's as Venkatesh recommended.

     

    Thanks,

    Prema



  • 6.  Re: How to get different Response from same REST URL

    Posted Sep 14, 2017 02:27 PM

    The problem is, there is no difference in incoming request apart from host name and port number. And once I have VSI created, the problem will be to throw back different response while testing the SV. Because there will be no way I can send different incoming request, since it will be deployed in same port. I guess creating different VSI will not solve problem, because every hit will give different response for same request. A single VSM will point to single VSI.



  • 7.  Re: How to get different Response from same REST URL
    Best Answer

    Posted Sep 14, 2017 01:32 PM

    I cannot say with 100% certainty this will work, but in the Request metaData, DevTest used to carry the Client IP address and worker thread port of the consumer sending the request.  

    Perhaps, you could use this to identify the IP address of the consumer. Using a Scriptable DPH as the last Filter on the LISTEN Step.  Please excuse potential typos.

     

    %beanshell%
    import com.itko.util.ParameterList;

    // only do this logic for the GET /v1/branches operation

    // NOTE: not sure if there is a "/" after /v1/branches on your request or not VERIFY AGAINST the Operation name in the VSI

    if ( ! "GET /v1/branches".equals( lisa_vse_request.getOperation() ) ) {

        _logger.info( "<<< Incoming REST request is NOT a /v1/branches request so existing the logic" );

       return;
    }

     

    // Get the meta data from the incoming request and check to see if it has a client ID

    ParameterList metadata = lisa_vse_request.getMetaData();

    String serverName;
     
    if ( metadata == null ) {  // saftey check
        _logger.info( "<<< lisa_vse_request getMetaData() returned NULL defaulting the SERVER NAME" );
        serverName = "UNKNOWN";

     

    } else if ( metadata.containsKey( "lisa.vse.request.client.id" ) ) {
       _logger.info( "<<< Client ID and worker thread is: {}", metadata.get( "lisa.vse.request.client.id" ) );
       int i = metadata.get( "lisa.vse.request.client.id" ).indexOf( ":" );
       serverName = metadata.get( "lisa.vse.request.client.id" ).substring( 0, i );
       _logger.info( "<<< The sending client id is: {}", serverName );

     

    } else {
       _logger.info( "<<< NO Client ID FOUND in META DATA defaulting to Unknown" );
        serverName = "UNKNOWN";
    }

    // now add a key/value pair into the ArgumentList so the VSI can use the SERVER_IP during the lookup 

    ParameterList args = lisa_vse_request.getArguments();

    args.addParameters("SERVER_IP=" + serverName);

    lisa_vse_request.setArguments( args );

     

    Now, in the GET /v1/branches operation in your VSI, 

    - CLICK the META Response

    - Add a key named  SERVER_IP and place "UNKNOWN" in the value

    - CLICK OK and this will cause the specific transactions to be populated with the key

    - CLICK on a specific transaction, CLICK on SERVER_IP and add the IP address for aaa.com

        - Replace the response body with the content for AAA

    - Copy the specific transaction, CLICK on SERVER_IP and add the IP address for bbb.com

        - Replace the response body with the content for BBB

    - Copy the specific transaction, CLICK on SERVER_IP and add the IP address for zzz.com

       - Replace the response body with the content for ZZZ

     

    If an IP address comes in or is not available, the value of SERVER_IP will be UNKNOWN so it will not match a specific transaction and select the META response.  

    If the IP Addresses for AAA, BBB, or ZZZ change or there can be multiple IP addresses, then add specific transactions as required.

     

    Run the service VSM in ITR mode and have one of the applications send a request. Or, send a request from your localhost using SoapUI, PostMan, etc to the service running in ITR mode.

    Single Step through the VSM LISTEN STEP in ITR. Stop and review the Properties and Events tabs to verify the code and make sure I did not have a typo in the above logic.

    Step through the VSI Selection and send the response to the client checking to ensure that the proper response was sent.

    If you are happy with the results, comment _logger.info statements, change them to _logger.debug or remove them so they don't appear in your vse.log when you deploy the service.



  • 8.  Re: How to get different Response from same REST URL

    Posted Sep 14, 2017 02:29 PM

    Thanks so much J_NeSmith for detailed guidance. I would try to implement the approach given by you and will let you the outcome ASAP. Many thanks.



  • 9.  Re: How to get different Response from same REST URL

    Broadcom Employee
    Posted Sep 14, 2017 03:20 PM

    If the only difference in the requests is the URL (hostname), then the best logical way to support this in DevTest is to have multiple VSEs, running on different hosts, each one containing a virtual service to replicate the response from one of the URLs.



  • 10.  Re: How to get different Response from same REST URL

    Posted Sep 15, 2017 12:58 AM

    Pranay,

     

    I'm curious what the benefit or business value is for this kind of deployment of a virtual service. In the real world, the consumer has to send its request to three different endpoints in order to consume the three different services. So to mimic this we typically deploy each virtual service to a different port. So can you explain why you have to deploy all the transactions to a single endpoint? Is it to avoid having to change the endpoint URL in the client application?

    Keep in mind that the idea of a virtual service is to get a predictable response for a given request. With this kind of setup it's not clear to the consumer which service is responding to its request.



  • 11.  Re: How to get different Response from same REST URL

    Posted Nov 07, 2017 09:02 AM

    Yes Willaim, you are correct. Ideally it shoul be that way only. But due to limitations of port number the development team want service specific request to be in same port. Since i was not sure of any other way to do this, I came up here to know if its feasible to do that way or not.