Forum Discussion

elysialock's avatar
elysialock
Occasional Contributor
11 years ago

Best approach for handling URLs without hard coding?

Hi everyone,

While I'm a seasoned SoapUI user, I'm new to the Pro version, and thus new to the concept of how environments work within SoapUI Pro. I'm working on a RESTFUL API that uses HATEOAS principles. HATEOAS testing is also new to me.

I'm trying to avoid hard-coding URLs into each of my test steps in order to make it easy to run in various dev, staging, and QA environments.

Originally my approach was to grab the URL from a response and transfer it to the endpoint of subsequent test steps. It's great when the response looks like this:
{
"link" : [ {
"rel" : "self",
"href" : "http://dev.foo.com/web-rest/webapi/authenticated"
}

but when I have a response that looks like this, with the parameters included:
{
"rel" : "clients",
"href" : "http://dev.foo.com/web-rest/webapi/clients{?page,size,sort,name,status}"
}

It doesn't work because {?page,size,sort,name,status} is included in the endpoint for subsequent tests. I need to pass just http://dev.foo.com/web-rest/webapi/clients so the request can complete without a 400, 401, or 404.

So, how should I handle this? Is this response format common for REST APIs using HATEOAS? Is there a way for me to use the links provided in the responses in subsequent test steps so that I only have to specify the domain/starting endpoint at the beginning of my test runs? Should I ask the developer to change the way the response is laid out so that the possible parameters are not included in the links? Can environments take care of this for me? Halp!!1!

Thanks!

-E
  • Although I am not seasoned myself - I have used property to store my URL and used property expension to call the property from the URL field.
    So when I change environments I do it within the property only. One change and all is well with the world.

    steps:
    1. create a custom property at the project level
    2. go to a request and remove the URL
    3. Use this link and my example bellow to construct the 'new' url (which calls the property) http://www.soapui.org/Scripting-Propert ... nsion.html
    Example:
    ${#Project#end_point}
    my property is named 'end_point'
    dont forget the dollar sign and the rest of the brackets
    4. Add it to the URL list (use the drop down in the URL to choose the add option)
    5. go to all your requests and update the URL from the drop down

    -- Note for soap: Because in SOAP the URL shows in soap as it's full URL (end point, and part of the request), you wont be able to use the drop down for the saved URL. Instead copy paste the endpoint part into the endpoint of the URL leaving the rest of the URL intact.

    2 mistakes that I made when doing it the first time:
    1. Since my API is under development and the versions increase - I added the version to the URL. That was a big fail. Specially when the automation was used within a building tool (Jenkins in my case), also - this worked for rest and not soap so I ended up having 2 endpoint properties.
    2. avoid having more then 1 endpoint property if possible, trying to add bits of the rest of the URL into the endpoint is going to make your life more complicated (specifically issue in soap requests)
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    Create a testCase level property "url" or any other named variable,
    Add a groovy script teststep,

    def response = context.expand( '${Xml - Request 1#Response#$.href}' )
    log.info response

    response = response.replaceAll( "\\{\\?page,size,sort,name,status\\}", "" )

    log.info response
    testRunner.testCase.setPropertyValue( "url", response )


    Transfer the endpoint from the property to request. You can also make it dynamic with substring or regex.

    Thanks,
    Jeshtha
  • elysialock's avatar
    elysialock
    Occasional Contributor
    Thanks everyone!

    I'll give the Groovy script a try. Thanks Jeshtha!

    I have used property to store my URL and used property expension to call the property from the URL field.
    So when I change environments I do it within the property only.

    @smadji: this won't work, as some of the URLs in the response have {parameters} appended to the end of the URL. I described this in my original post but probably not well. Basically the "junk" (i.e. parameters in curly brackets) is also appended to the end of the URL, which makes the resulting property-transferred-endpoint improperly formatted & invalid, which means I get a 400 or 404 error. I was looking for a way to strip out the extra junk at the end so that I had a properly-formatted URL that will work with my test cases.