Reputation: 199
Whenever I am executing the testcases, I will get the property value in response as
myscore
Arun/ten2016-12-20
Here the key is myscore
and value isArun/ten2016-12-20
. The date will be changing dynamically as today date append with the text 'Arun/ten'
I tried by adding the below groovy script code but its not working,
today = new Date().format("yyyy-MM-dd")
log.info today
def s=log.info("Arun/ten" + today);
assert messageExchange.responseHeaders["my_client_update"] == ["s"]
I want this script to be passed.
The same i have passed in the script assertion like below,
assert messageExchange.responseHeaders["my_client_update"] == ["s"]
Kindly suggest the solution.
Upvotes: 1
Views: 253
Reputation: 21369
Your Script Assertion
is almost near except that one statement has trivial issue.
Here is what you would need the script to be, and note that below script is using simple regular expression for date as that is dynamic. Of course, you may also use in the same way as convenient to you.
/**
* Below is the Script Assertion
* Retrieves the specified header and asserts against the expected value
**/
//Change the header key as needed.
def requiredHeaderKey = 'my_client_update'
//Change the expected value for the above Header key
//Note that below one is using the regular expression to accommodate dynamic date in the header value
def expectedRequiredHeaderValue = "Arun/ten\\d{4}-\\d{2}-\\d{2}"
//You may not be required to change beyond this point of the script.
//Check if the response has headers
assert messageExchange.responseHeaders, "There are no headers in the response"
//Get & Check if the response has required Header and its value is not empty
def requiredHeaderValue = messageExchange.responseHeaders[requiredHeaderKey]
assert requiredHeaderValue, "Value of the response header ${requiredHeaderKey} is empty or null"
if (requiredHeaderValue instanceof List) {
log.info "Requested header value is ${requiredHeaderValue[0]}"
assert requiredHeaderValue[0] ==~ expectedRequiredHeaderValue, "Response header value is not matching with the expected value"
}
Upvotes: 1