Forum Discussion

AhamedShaik's avatar
AhamedShaik
Occasional Contributor
7 years ago

How to change resource path for different environments.

Following is my scenario: Using Ready API.

 

I'm working with  different environments ( dev, test & staging) the requirement here is utilizing the same test scenario for different environments.

 

According to my project, the resource path is differ based on the environment. 

For example for dev environment:  http://dev.company.com /rs/hotel/V1/shopping  the resource path is different for test environment example: http://test.company.com /APINH0I/V1/shopping

 

I can able to change the endpoint http://test.company.com to http://dev.company.com when I chose the environment option from the ready api tool. Changing the resource path going in the project is effecting all the resources, with this I can't able to execute the same scenario for two different environments. ( don't want' to duplicate the test suite, it will be huge for maintenance).

 

Could you please recommend or suggest any workaround to handle this scenario.

 

 

  • First of all, I presume you are using the top set of lines. I see that the magic number 24 is used. That is longer than the length of your API (in bytes). That will make it not work.

     

     

    there are 22 characters in "http://dev.company.com" and 23 characters in the "http://test.company.com" in your original post. In my response to you there is a link to a solution I made to someone else told that person to figure out the byte length.

     

    My suggestion is change 24 to 22 and just test for now in the dev environment.

     

    Then change 22 to 23 and use the test environment.

     

    The line below is something to ignore "//" is a comment. It's java-like, C++-like

    //String resourcepath = new String(therequestUri.substring(beginindex, endindex))

    As for the output I see

    /rail/rs/V1/tsearches  and
    /rail/sohail/V1/tsearches

     

    It looks as though somehow it sees two resource paths. Possibly to do with using the wrong number on the API size.

     

    Take small steps. Test with one environment and one resource path.

  • Yeah I have a similar environment setup. One I call a dev, one for customer1 and one for customer2.

     

    In dev, go to your custom properties tab and define the name of the resource, such as CPresourceName. Then give it the value 

    /rs/hotel/V1/shopping

     

    In the test environment you will have a CPresourceName but its value field is empty. Give it the value 

    /APINH0I/V1/shopping

     

    Now at the top in your ReadyAPI environment, click on Events. Add the requestFilter.FilterRequest event and you can use java-like string manipulation to append the resource path to the API. I have answered a similar question awhile back.

     

    Someone wanted to just fetch the resource. It involved knowing first hand the length of the string of the API. In your case you have  

    http://dev.company.com  and 

    http://test.company.com  

     

    You can parse for "dev" or "test" to know which is the current API and from there you have 22 bytes and 23 bytes. Then append the resource path.

     

    See 

    https://community.smartbear.com/t5/SoapUI-Pro/Is-there-any-way-to-get-rest-resource-of-a-rest-step-through/m-p/147648#M33627

     

    I had something like the embedded code here to fetch the resource path. You need to add a resource path using property expansion

     

    def resourcepath = context.expand('${#Project#CPresourceName}').toString()

     

    log.info "\n\n\n\n"
    log.info "                Begin of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script"
    log.info "\n\n\n\n"
    
    
    
    String therequestUri = new String(context.getProperty("requestUri").toString())
    log.info "therequestUri is " + therequestUri
    log.info "length is "+ therequestUri.length()
    
    def endindex = therequestUri.length() - 1
    def beginindex = 24
    
    String resourcepath = new String(therequestUri.substring(beginindex, endindex))
    
    log.info "context.getProperty(\"requestUri\"): " + context.getProperty("requestUri").toString()
    
    log.info "resourcepath is " + resourcepath
    
    
    log.info "\n\n\n\n"
    log.info "                End of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script"
    log.info "\n\n\n\n"

     

    • AhamedShaik's avatar
      AhamedShaik
      Occasional Contributor

      Hi Bill,

       

      I try the above solution, unfortunately I didn't work . May be I'm doing something wrong in the script. Please have a look at the  below script.

       

      log.info "\n\n\n\n"
      log.info "                Begin of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script"
      log.info "\n\n\n\n"
      
      
      
      String therequestUri = new String(context.getProperty("requestUri").toString())
      log.info "therequestUri is " + therequestUri
      log.info "length is "+ therequestUri.length()
      
      def endindex = therequestUri.length() - 1
      def beginindex = 24
      
      def resourcepath = context.expand('${#Project#CPresourceName}').toString()
      
      //String resourcepath = new String(therequestUri.substring(beginindex, endindex))
      
      log.info "context.getProperty(\"requestUri\"): " + context.getProperty("resourcepath").toString()
      log.info "resourcepath is " + resourcepath
      
      
      log.info "\n\n\n\n"
      log.info "                End of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script"
      log.info "\n\n\n\n"

      Also, I didn't get the below code , I comment it out in my script.

      //String resourcepath = new String(therequestUri.substring(beginindex, endindex))

       

      From the test runner logs: I can see the following:

       

      17:45:06,412 INFO  [log]                 Begin of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script
      17:45:06,412 INFO  [log] 
      
      
      
      
      17:45:06,413 INFO  [log] therequestUri is http://xxxxxx.xxx.xxxxx:xx /rail/rs/V1/tsearches
      17:45:06,413 INFO  [log] length is 66
      17:45:06,413 INFO  [log] context.getProperty("requestUri"): null
      17:45:06,414 INFO  [log] resourcepath is /rail/sohail/V1/tsearches
      17:45:06,414 INFO  [log] 
      
      
      
      
      17:45:06,414 INFO  [log]                 End of the demonstration of resourcepath in RequestFilter.filterRequest Groovy Script
      17:45:06,414 INFO  [log] 

      Could you please let me know, what's wrong in the above script.

       

      Thanks a lot.

       

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        First of all, I presume you are using the top set of lines. I see that the magic number 24 is used. That is longer than the length of your API (in bytes). That will make it not work.

         

         

        there are 22 characters in "http://dev.company.com" and 23 characters in the "http://test.company.com" in your original post. In my response to you there is a link to a solution I made to someone else told that person to figure out the byte length.

         

        My suggestion is change 24 to 22 and just test for now in the dev environment.

         

        Then change 22 to 23 and use the test environment.

         

        The line below is something to ignore "//" is a comment. It's java-like, C++-like

        //String resourcepath = new String(therequestUri.substring(beginindex, endindex))

        As for the output I see

        /rail/rs/V1/tsearches  and
        /rail/sohail/V1/tsearches

         

        It looks as though somehow it sees two resource paths. Possibly to do with using the wrong number on the API size.

         

        Take small steps. Test with one environment and one resource path.