cancel
Showing results for 
Search instead for 
Did you mean: 

read image from a GET response in Groovy script

SOLVED
Highlighted
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?

1 ACCEPTED SOLUTION

Accepted Solutions
Community Expert

Re: read image from a GET response in Groovy script

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.

3 REPLIES 3
Community Hero

Re: read image from a GET response in Groovy script

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


Regards,
Rao.
Community Expert

Re: read image from a GET response in Groovy script

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

How do I correctly read the response body?
Community Expert

Re: read image from a GET response in Groovy script

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.

New Here?
Join us and watch the welcome video:
6 / 7 API Testing Mistakes Video
Top Kudoed Authors