Ask a Question

read image from a GET response in Groovy script

SOLVED
SiKing
Community Expert

read image from a GET response in Groovy script

I am testing a service that uploads images.

I upload my image with a POST, and subsequently read it back with a GET. Looking at the GET response in the Outline tab, I see the correct metada: contentType: image/jpeg, contentLength: 86231. In the HTML tab it shows me my image. Everything agrees, including the size of the image.

Since I want everything automated, I want to compare the received image with the original image. I wrote a Groovy script like this:

import org.apache.commons.io.FileUtils
import java.security.MessageDigest


def md = MessageDigest.getInstance("MD5")

// original image
def file = new File(context.expand('${projectDir}') + '/data/lenna.jpg')
def fileBytes = FileUtils.readFileToByteArray(file)
def fileDigest = md.digest(fileBytes)

// received image
def image = context.expand('${Read image#Response}')
def imageBytes = image.getBytes()
def imageDigest = md.digest(imageBytes)

log.info fileBytes.size()
log.info imageBytes.size()

md.isEqual(fileDigest, imageDigest)

Unfortunately this fails. In the log, I see the byte counts as: 86231 and 158429 respectively. How do I correctly read the response body?

3 REPLIES 3
nmrao
Champion Level 2

Does the below link helps?
https://www.javainuse.com/java/ImageEquals


Regards,
Rao.
SiKing
Community Expert

That link explains how to compare images. My question was:

How do I correctly read the response body?
SiKing
Community Expert

This answer and browsing through the SoapUI APIs eventually led me to:

// received image
def testStep = testRunner.testCase.testSteps['Read image']
def image = testStep.testRequest.response
assert image.getContentType() == "image/jpeg"
assert image.getContentLength() == fileBytes.size()
def imageBytes = image.getRawResponseBody()
def imageDigest = md.digest(imageBytes)

The rest of the code is unchanged.

cancel
Showing results for 
Search instead for 
Did you mean: