Reading SOAP Request Attachment using Groovy
Hello,
I have a SOAP request with an attachment. I'm trying to read the attachment, generate it's SHA-256 hash, and put the value in the request before sending. I've found various snippets of each step in the community, but nothing seems to work.
This is what I have so far in my Groovy script:
import java.security.MessageDigest
def attachment = testRunner.testCase.testSteps['SOAP Request'].httpRequest.getAttachmentAt(0).data
def digest = MessageDigest.getInstance("SHA-256")
def sha256 = { var.java.security.MessageDigest.getInstance("SHA-256").digest(attachment).toString()}
The result I get is: "Script-result: Script54$_run_closure1@6fa2bce9"
Any help is appreciated.
Thanks
I figured it out. Let me first state what I wanted to do.
I wanted to get the SHA-256 value from the cached attachment in my SOAP Request and then place that value in the SOAP Request as a property. If the attachment changes it doesnt matter because the test will generate a new hash value from it.
To do this:
1. I created a Groovy Script test step to go get the attachment, generate the SHA-256 value and return it.
import java.security.MessageDigest import java.math.BigInteger def attachment = testRunner.testCase.testSteps['SOAP_Request'].httpRequest.getAttachmentAt(0).data String sha256 = org.apache.commons.codec.digest.DigestUtils.sha256Hex(attachment); assert sha256 return sha256 log.info(sha256)
2. I created a Property Transfer test step to move the "result" property value to a property I created at the Test Case level.
3. I then reference the custom Test Case property in my SOAP Request by using ${#TestCase#sha256} (where sha256 is the name I gave the custom proerty) in the corresponding XML element.