Forum Discussion

pedro's avatar
pedro
Occasional Contributor
8 years ago

How to Assert values sent in Request Body at Rest response?

Hi I am trying to make an assertion of the request body I am sending in a PUT method on the response of a GET method in another step.

 

Request body sent in PUT method:

{
"storeId" : "0010",
"registerNumber" : "0012"
}

 

Json response in GET method:

{
    "PackageId": "188800120412201723609318",
          "storeId" : "0010",
          "registerNumber" : "0012"
          "lineId": "01"
          "createdTimestamp": "04-19-2017 3:22 PM Wed UTC"
 }

 

How can I map each node on the JSON (storeId , registerNumber ) sent in the request body at the JSON response to the same

 

Thanks! 

 

SOAPUI NG 1.9

 

Kind regards,

Pedro 

  • nmrao's avatar
    nmrao
    Champion Level 3

    Script Assertion can be used to achieve the same and below is script:

     

     

    assert context.rawRequest, 'Request is empty or null'
    
    assert context.response, 'Response is empty or null'
    
    //Closure to get the required data from json
    def getDataMap = { data, list ->
      def json = new groovy.json.JsonSlurper().parseText(data)
      json.inject([:]) {m, key, value -> if(key in list) m[key] = value;m }
    }
    
    def desiredKeys = ['storeId', 'registerNumber']
    
    def reqestMap = getDataMap(context.rawRequest, desiredKeys)
    def responseMap = getDataMap(context.response, desiredKeys)
    
    println "Request data : $reqestMap"
    println "Response data : $responseMap"
    
    assert reqestMap == responseMap, 'Response data is not matching with request data'

    The above one uses dynamic request and response.

     

    Here is the script that you can try online quickly for your fixed data and see it working.