Forum Discussion

kbourdel's avatar
kbourdel
Occasional Contributor
8 years ago

Script assertion using regex to get a value from the response header

Hello-

I have SoapUI NG & I'm attempting to extract one piece of info from the response header within an assertion script using regex, but I'm not getting the desired result.

Any guidance would be great!

 

The response header called 'WEBAPI_RESPONSE_DETAIL' contains the following:

{"ProcessingMilliseconds":1.34,"ResponseCount":1,"ProcessingMachine":"QAC1","ApplicationVersion":"2.1.766.10728","BuildNumber":"2.1.766.10728","EnvironmentName":"QA","MethodName":"/MemberStore/123/2.1/Routers/QualifiedQuotas"}

 

I'm only interested in the value for ProcessingMilliseconds (so in this example, 1.34). 

 

My assertion script is the following:

 

def Details = messageExchange.responseHeaders["WEBAPI_RESPONSE_DETAIL"]
log.info("Details: "+ Details)
def regEx1 = ~/.*ProcessingMilliseconds":([0-9\.]*).*/
def sla = Details[0].replaceAll(regEx1,'')
log.info("sla: " + sla)

 

When I run the assertion, I get nothing back.

 

Any ideas what I might be doing wrong?

 

2 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    Can you please check the below regex?

     

    (ProcessingMilliseconds"[\s?]*:[\s?]*[\d]+[\.][\d]*,)

     

    By the way, special characters needs to be escaped in the above string.

     

    def regEx = "(ProcessingMilliseconds\"[\\s?]*:[\\s?]*[\\d]+[\\.][\\d]*,)"

     

    The above can handle if there is space before and after ":" and no alphabets after decimal till "," is the next character.

     

    Here is simple positive case:

     

    def str = '''{"ProcessingMilliseconds":  11.34,"ResponseCount":1,"ProcessingMachine":"QAC1","ApplicationVersion":"2.1.766.10728","BuildNumber":"2.1.766.10728","EnvironmentName":"QA","MethodName":"/MemberStore/123/2.1/Routers/QualifiedQuotas"}'''
    def regEx = "(ProcessingMilliseconds\"[\\s?]*:[\\s?]*[\\d]+[\\.][\\d]*,)"
    def result =  (str =~ regEx)
    //Equal to 1, because there must be one match.
    assert result.count ==1

    Test for failure:

     

    • kbourdel's avatar
      kbourdel
      Occasional Contributor

      Thanks for your reply.

       

      The trick was escaping the special characters in the regex portion & reworking the code a bit

      def Detail = messageExchange.responseHeaders["WEBAPI_RESPONSE_DETAIL"]
      log.info("Detail: "+ Detail)
      
      def regEx = ".*ProcessingMilliseconds\":([0-9\\.]*).*"
      
      slaheader = ( Detail[0] =~ regEx )
      log.info ('slaheader ' + slaheader[0][1])