Forum Discussion

mmoser18's avatar
mmoser18
Frequent Contributor
8 years ago
Solved

Strange bug where <string>.replace(...) does not work.

In one of my scripted assertion I need to replace a substring with another value. For some strange reason that doesn't work:

 

My expression reads:

${=context.endpoint.replace("http","https")}

 

... but for some odd reason the resulting link still starts with "http://" instead of "https://" ?!?

 

To debug this I tried the following:

endpoint: ${=context.endpoint}
result1: ${=context.endpoint.replace("http","https")}
result2: ${=context.endpoint.replace('http','https')}

 

but the result still reads:

endpoint: http://chbbatscsoa01:9895/references_v2.0.0

result1: http://chbbatscsoa01:9895/references_v2.0.0

result2: http://chbbatscsoa01:9895/references_v2.0.0

 

According to the docs, both arguments of the replace()-method are simple strings (i.e. no reg. exps. or such), so why does this simple replace(...) not work? What am I missing here???

  • When I tried again today for some reason the original

    ${=context.endpoint.replace('http','https')}

     

    suddenly worked!

    Beats me, why this was not properly handled last time! Really weird!

6 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    Just use:

    def newEndpoint = context.endpoint.replace('http', 'https')
    log.info newEndpoint

     

    EDIT: KarelHusa thank you for point missing one. Updated.

    • mmoser18's avatar
      mmoser18
      Frequent Contributor

      nmrao:

      Sorry - doesn't work for me!

       

      If I enter:

      ${=context.endpoint('http','https')}

       

      this yields:

      No signature of method: com\.eviware\.soapui\.impl\.wsdl\.testcase\.WsdlTestRunContext\.endpoint() is applicable for argument types: (java\.lang\.String, java\.lang\.String) values: [http, https]

       

      This is running inside a "Script Assertion".

      • KarelHusa's avatar
        KarelHusa
        Champion Level 3

        mmoser18, in SoapUI we need to distinguish when we are writing a Groovy script and when we are writing a statement inside a Groovy string (inside the curly braces ${} ).

         

        When you write a Groovy script, as is the case of Script Assertion, Groovy Script test step and other places, it's better to write plain Groovy, as nmrao advised:

         

        def e = context.endpoint.replace("http","https")
        log.info e

         

        When you e.g. put ${myVariable} inside a request message, you work with a Groovy string, which is later parsed by SoapUI run-time and executed by Groovy (here's a link to Groovy documentation).

         

        Please check the code above if it works according to your expectations.

         

        Please note that you previously omitted the 'replace' method when writing:

         

        ${=context.endpoint('http','https')}

        That's why you got the No signature of method exception.

         

        Karel

         

  • KarelHusa's avatar
    KarelHusa
    Champion Level 3

    mmoser18, which kind of assertion do you use?

     

    I have tried the following code: 

     

     

    ${=context.endpoint.replace("http","https")}

     

     

    in the Contains assertion and the substitution works as expected, i.e. http is replaced by https.

     

    Karel

     

     

     

     

  • mmoser18's avatar
    mmoser18
    Frequent Contributor

    When I tried again today for some reason the original

    ${=context.endpoint.replace('http','https')}

     

    suddenly worked!

    Beats me, why this was not properly handled last time! Really weird!